java 抽象类构造函数访问修饰符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/260744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:40:52  来源:igfitidea点击:

Abstract class constructor access modifier

java

提问by Richard Walton

An abstract class can only be used as a base class which is extended by some other class, right? The constructor(s) of an abstract class can have the usual access modifiers (public, protected, and private (for internal use)). Which of protectedand publicis the correct access modifier to use, since the abstract type seems to indicate that technically a public constructor will act very much protected? Should I just use protected on all my constructors?

抽象类只能作为基类使用,由其他类扩展,对吗?抽象类的构造函数可以具有通常的访问修饰符(public、protected 和 private(供内部使用))。哪个protectedpublic是正确的访问修饰符,因为抽象类型似乎表明从技术上讲公共构造函数将受到很大的保护?我应该在所有构造函数上使用 protected 吗?

采纳答案by Jordan Stewart

since the abstract type seems to indicate that technically a public constructor will act very much protected

This is not correct. An abstract class cannot be directly instatiated by calling its constructor, however, any concrete implementation will inherit the abstract class' methods and visibility

So the abstract class can certainly have public constructors.

因为抽象类型似乎表明从技术上讲,公共构造函数将受到很大的保护

这是不正确的。抽象类不能通过调用其构造函数直接实例化,但是,任何具体实现都会继承抽象类的方法和可见性

所以抽象类当然可以有公共构造函数。

Actually, the abstract class's constructor can only be called from the implementation's constructor, so there is no difference between it being public or protected. E.g.:

实际上,抽象类的构造函数只能从实现的构造函数中调用,所以它是公共的还是受保护的没有区别。例如:

public class Scratch
{
    public static abstract class A
    {
        public A( int i ) {}
    }

    public static class B extends A
    {
        private B() { super(0); };
    }
}

回答by IAmCodeMonkey

If this behavior is true, and I'm not sure it is, you should always use the most restricted scope available for your application to function. So in that case, I would recommend using protected.

如果此行为属实,而且我不确定是否属实,那么您应该始终使用应用程序可用的最受限制的范围来运行。所以在这种情况下,我建议使用 protected。

回答by Nrj

since the abstract type seems to indicate that technically a public constructor will act very much protected

因为抽象类型似乎表明从技术上讲,公共构造函数将受到很大的保护

Umm... for abstract classes this constructor scope [public or protected] is not of much difference since the instantiation is not allowed [even if public]. Since it is meant to be invoked by the subclass, it can invoke either public or protected constructor seamlessly.

嗯...对于抽象类,这个构造函数范围 [public 或 protected] 没有太大区别,因为实例化是不允许的 [即使是 public]。由于它旨在由子类调用,因此它可以无缝调用公共或受保护的构造函数。

Its completely on choice what to use. I generally prefer public as it is in most of the cases.

它完全取决于选择使用什么。我通常更喜欢公开,因为在大多数情况下。

回答by JaredPar

At the very least, an abstract class should have a protected constructor. It's not strictly necessary since it's not possible to use the constructor anyway but it makes the contract explicit.

至少,抽象类应该有一个受保护的构造函数。这不是绝对必要的,因为无论如何都不可能使用构造函数,但它使合同明确。

Another option is to make the constructor private. This is only a good idea though if all of the implementations of the class are private inner classes. A rare but useful example.

另一种选择是将构造函数设为私有。如果类的所有实现都是私有内部类,这只是一个好主意。一个罕见但有用的例子。