将类设为 Final 和将类构造函数设为 private 有什么区别
最终类和将类构造函数设置为私有之间到底有什么区别?
我知道两者都不能被子类化(如果我错了,请纠正我)。他们有什么区别吗?
What exactly is the difference between a final class and having a class constructor as private.
I know both can't be subclassed(correct me if i am wrong). Is their any difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
最后一堂课不能延长。它可以防止这种情况,
这对于像字符串这样的东西很有用 - 你不会希望有人能够覆盖字符串(最常用的对象之一)的逻辑,并且能够,哦,我不知道,添加网络和将您使用的所有字符串发回。如果你可以扩展 String,这是可能的。
私有构造函数不能在类外部调用。
通常这最终会像这样工作:(java.lang.Math 是一个很好的例子)
或者它最终会像这样工作 // Integer 实际上这样做
当你扩展一个类时,默认情况下你的构造函数会尝试调用默认值(无参数) ) 构造函数。如果它是私有的,那么在扩展它时必须显式调用非私有构造函数。如果您没有可调用的非私有构造函数,您将无法扩展它。 感谢您指出这一点的评论。 :-)
A final class cannot be extended. It prevents this
This is useful for things like String - you wouldn't want someone to be able to overwrite the logic of String, one of the most commonly used Objects, and be able to, oh I don't know, add networking and send all the strings back you use. It's possible to do if you can extend String.
A private constructor cannot be called outside the class.
Often this ends up working like this: (java.lang.Math is a good example)
Or it ends up working like this // Integer does this actually
When you extend a class, your constructor by default attempts to call the default (no argument) constructor. If that is private, then you must explicitly call a non-private constructor when you extend it. If you have no non-private constructors to call you won't be able to extend it. Thanks for comments for pointing this out. :-)
实例化
私有构造函数或者隐式 super() 调用都会失败。
看看 Math 类,它有一个私有构造函数,无法实例化,但它有许多非常有用的静态方法
Instantiation
a private constructor either as the implicit super() call would fail.
Look at the Math class it has a private constructor, can't be instantiated yet it has many static methods which are very helpful
如果你不希望你的类被子类化,那么你可以使用final。如果您不希望其他类实例化您的类,而是希望控制对象的构造和维护方式,则可以使用私有构造函数。
If you don't want your class to be sub-classed, then you use final. If you don't want other classes to instantiate your class, but rather you want to control how the object is constructed and maintained, you use private constructor.
仅拥有私有构造函数比拥有最终类更强。
详细:
A 类中仅包含私有构造函数会对对象创建产生强烈影响。通常您会使用工厂方法。您仍然可以使用技巧创建 A 的实例,而无需使用工厂方法,例如clone() 或反射调用私有构造函数。但是子类化是不可能的,因为子类的构造函数需要能够调用
super()
。这只能在 A 内的嵌套类中实现。仅具有私有构造函数通常是有意义的,例如通过工厂方法控制实例化,例如对于单例(请参阅有效的 Java 第 3 条)。但即使在这种情况下,我也看不出有什么理由不写“最终类”,如果只是为了文档,以便读者立即明白子类化是不允许的。
Having only private constructors is stronger than having a final class.
In detail:
Having only private constructors in a class A strongly influences object creation. Usually you are then using a factory method. You can still create instances of A without a factory method using tricks, such as clone() or invoking the private constructor reflectively. But subclassing is not possible, because a subclass' constructor needs to be able to call
super()
. That would only be possible within a nested class inside A.Having only private constructors often makes sense, e.g. to control instantiations via factory methods, e.g. for singeltons (see Effective Java item 3). But even in that case, I don't see a reason to not writing "final class", if just for documentation, so that readers immediately grasp that subclassing is not allowed.
最终类/方法永远不能被继承/覆盖,并且会抛出编译错误,而具有私有构造函数的类也会抛出编译“没有可用于您的类的默认构造函数”,并且一旦您添加非最终构造函数并调用从你的孩子班级开始,不会有错误。
私有构造函数在创建单例类(一个类只能存在一个实例)的情况下很有用,您可以将构造函数设为私有并使用一个变量来存储单个实例,该变量将通过公共静态方法公开。
对于前。
例如java中的String类就是final类。
A final class/method can never be inherited/overridden and will throw compilation error where as A class with private constructor will also throw compilation that'There is no default constructor is available for your class' and once u add a non final constructor and call it from your child class, There will be no error.
A private constructor is useful in case of creating singleton class(Only one instance of a class can exist),Where you make your constructor private and a variable to store single instance,which will be exposed with public static method.
for ex.
And for example String class in java is a final class.
private
成员可以通过内部类访问。prints
最终类不能扩展,但其构造函数可以是公共的。私有构造函数可以从另一个构造函数调用或由内部类使用。
private
members can be accessed by inner classes.prints
A final class cannot be extended, but its constructor can be public. A private constructor can be called from another constructor or used by an inner class.