简体   繁体   English

我们可以覆盖Java中的构造函数,并且构造函数可以是私有的吗?

[英]Can we override a constructor in Java and can a constructor be private?

I would appreciate an explanation for these questions: 对于这些问题的解释,我将不胜感激:

  1. Can we Override a constructor in Java? 我们可以Override Java中的构造函数吗?
  2. Can a Constructor be private? Constructor可以私有吗?

No, you can't override a constructor. 不,您不能覆盖构造函数。 They're not inherited. 它们不是继承的。 However, each subclass constructor has to chain either to another constructor within the subclass or to a constructor in the superclass. 然而,每个子类的构造具有链要么在子类中的另一构造, 在超类的构造函数。 So for example: 因此,例如:

public class Superclass
{
    public Superclass(int x) {}

    public Superclass(String y) {}
}

public class Subclass extends Superclass
{
    public Subclass()
    {
        super(5); // chain to Superclass(int) constructor
    }
}

The implication of constructors not being inherited is that you can't do this: 构造函数不被继承的含义是您不能这样做:

// Invalid
Subclass x = new Subclass("hello");

As for your second question, yes, a constructor can be private. 关于第二个问题,是的,构造函数可以是私有的。 It can still be called within the class, or any enclosing class. 仍可以在类或任何封闭类中调用它。 This is common for things like singletons: 这对于单例来说很常见:

public class Singleton
{
    private static final Singleton instance = new Singleton();

    private Singleton()
    {
        // Prevent instantiation from the outside world (assuming this isn't
        // a nested class)
    }

    public static Singleton getInstance() {
        return instance;
    }
}

Private constructors are also used to prevent any instantiation, if you have a utility class which just has static methods. 如果您有一个只有静态方法的实用程序类,则私有构造函数也可用于防止任何实例化。

Constructor is meant for a class. 构造函数用于一类。 It cant be overridden under any circumstances. 在任何情况下都不能覆盖它。 Its like wanting to change Ferrari's factory from BMW's factory (which isn't practical). 就像想要将法拉利的工厂从宝马的工厂变更(这不切实际)一样。 Surely you can overload to get the functionality you need. 当然,您可以重载以获得所需的功能。

Yes Constructor can be private. 是构造函数可以是私有的。 By making it private you are not letting the outside world to create an object of it directly through constructor, But singleton pattern uses a public static method to call the constructor of the class and object can be created. 通过将其设为私有,您不会让外界直接通过构造函数创建该对象,但是单例模式使用公共静态方法来调用该类的构造函数,并且可以创建对象。

  1. No we can't use a constructor out of class because sub class is treat constructor as a method.. without return type. 不,我们不能在类外使用构造函数,因为子类将构造函数视为没有返回类型的方法。
  2. You can use it as a private but if a constructor of a class is private then you cannot make the obj of the respected class into another class. 您可以将其用作私有,但是如果一个类的构造函数是私有的,则无法将受尊重的类的obj转换为另一个类。

You can use callback: 您可以使用回调:

In parent class: 在家长班:

protected void onCreate(){}

SomeClass(){
//Constructor code
onCreate()
}

in child: 在儿童中:

@Override
protected void onCreate(){
//Child constructor code
}

1) NO! 1)不! A constructor belongs to the class in which it is declared. 构造函数属于在其中声明其的类。 A sub class is a different class and must have its own constructor. 子类是不同的类,并且必须具有自己的构造函数。 So, constructors simply can't be overridden. 因此,构造函数根本无法被覆盖。

2) Yes, that's done usually in case of singletons. 2)是的,通常在单例情况下完成。

不,我们不能超越承包商,为实施Singleton Pattren,我们应该有一个私人承包商。

1) Is this just homework question, or do you try to reach something? 1)这只是作业问题,还是您尝试达成目标? Can you show what you try to reach with an overriding constructor? 您能否展示您尝试使用最重要的构造函数达到的目标?

Since the parent constructor is called first, you may modify the base class to your needs in your constructor. 由于首先调用了父构造函数,因此您可以根据需要在构造函数中修改基类。 Of course, just as far as the access to base attributes isn't private. 当然,就基本属性的访问而言,它不是私有的。 If you extend a class but don't like their might-be-private attributes, deriving from it was an error. 如果您扩展一个类但不喜欢它们的可能私有属性,则从该类派生是错误的。

2) Can a constructor be private? 2)构造函数可以是私有的吗?

Yes, but do you know what it is good for? 是的,但是您知道这有什么用吗?

  1. In a derived class you can create a new constructor with the same signature but that is not really overriding since, when initializing the class, the parent class's constructor is still called before the new one. 在派生类中,您可以创建一个具有相同签名的新构造函数,但这并不是真正的替代,因为在初始化该类时,仍会在新类之前调用​​父类的构造函数。

  2. a class's constructor can be private or protected and of course be public. 类的构造函数可以是私有的,也可以是受保护的,当然也可以是公共的。 but if it is protected or private how would you initiate the class? 但是如果它是受保护的或私有的,您将如何上课? ( actually you could with a static function in that class...) (实际上,您可以在该类中使用静态函数...)

两者都不是正确的,因为当一个类继承其他类时,它会自动调用其父类,因此我们不能覆盖它们并使它们成为final是有道理的。

You can not override a constructor, however you can make them any access level modifier such as public, private or default. 您不能覆盖构造函数,但是可以使它们成为任何访问级别修饰符,例如public,private或default。 You would want a private constructor for things like a singleton or for a class that's mostly made of static methods and such (ie Java's Math class) 您可能需要一个私有构造函数来处理诸如单例或主要由静态方法之类组成的类(例如Java的Math类)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM