为什么构造函数不能是最终的 [英] Why constructors cannot be final

查看:150
本文介绍了为什么构造函数不能是最终的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么构造函数在Java中不是final,static或abstract?

Why can't constructors be final, static, or abstract in Java?

例如,你能向我解释为什么这是无效的吗? >

For instance, can you explain to me why this is not valid?

public class K {

    abstract public K() {
        // ...
    }
}


推荐答案

一个方法 final 意味着:你不想让任何类覆盖它。但是构造函数(根据Java语言规范)不能被覆盖,所以它是干净的。

When you set a method as final it means: "You don't want any class override it." But the constructor (according to the Java Language Specification) can't be overridden, so it is clean.

当你设置一个方法 abstract 这意味着:方法没有一个body,它应该在子类中实现。但是当使用 new 关键字时,隐式调用构造函数,所以它不能缺少正文。

When you set a method as abstract it means: "The method doesn't have a body and it should be implemented in a child class." But the constructor is called implicitly when the new keyword is used so it can't lack a body.

你设置一个方法为 static 意味着:该方法属于类,而不是特定的对象。但是隐式调用构造函数来初始化一个对象,所以没有任何目的拥有一个静态构造函数。

When you set a method as static it means: "The method belongs to the class, not a particular object." But the constructor is implicitly called to initialize an object, so there is no purpose in having a static constructor.

这篇关于为什么构造函数不能是最终的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆