简体   繁体   English

在Java中为抽象类创建构造函数有什么用?

[英]What is the use of creating a constructor for an abstract class in Java?

I would like to know what purpose a constructor for an abstract class serves; 我想知道抽象类的构造函数的用途是什么; as we do not instantiate abstract classes, why would we ever need such a constructor? 因为我们没有实例化抽象类,为什么我们需要这样的构造函数?

there will be times when you have some common initialization of instance variables that all the inheriting classes need to set up. 有时您会对所有继承类需要设置的实例变量进行一些常见的初始化。 You do instantiate an abstract class when you extend it and that concrete class has a constructor that will either supply the parameters to the constructor of the abstract class. 您在扩展它时实例化一个抽象类,并且该具体类具有一个构造函数,该构造函数将向抽象类的构造函数提供参数。

它们仍然可以由继承自那个的类的构造函数调用,使得代码重构很好地用于在抽象类中使用构造函数。

If you have uninitialised final fields in an abstract class, you'll need to initialise them in a constructor. 如果在抽象类中有未初始化的最终字段,则需要在构造函数中初始化它们。

Eg 例如

abstract class A {
    final int x;
}

will not compile without a constructor that assigns to x . 没有分配给x的构造函数将无法编译。

If your class doesn't declare a constructor, javac will make a no-arg, do-nothing constructor for you. 如果你的类没有声明构造函数,javac将为你创建一个no-arg,do-nothing构造函数。 Then, when your subclass is initialized, it will call the generated no-op constructor and life is good. 然后,当你的子类被初始化时,它将调用生成的no-op构造函数,并且生命是好的。

However, if your class declares any constructor, javac will NOT make one for you. 但是,如果你的类声明了任何构造函数,javac将不会为你创建一个。 In that case, the subclass constructor needs to explicitly call the constructor of the parent class. 在这种情况下,子类构造函数需要显式调用父类的构造函数。 Otherwise, you'll fail to initialize members of the parent class, as the above answer mentions. 否则,您将无法初始化父类的成员,如上面的答案所提到的那样。

Constructors for abstract classes are used by subclasses (invoked from subclass constructors using super(params) ). 抽象类的构造函数由子类使用super(params)使用super(params)从子类构造函数调用)。

You should make these constructors protected to make that clear. 您应该使这些构造函数protected以使其清晰。

You don't instantiate abstract classes but the constructor is invoked when a subclass is instantiated. 您不实例化抽象类,但在实例化子类时调用构造函数。

The use could be to initialize common attributes ie. 用途可能是初始化常见属性,即。

import java.util.List;
import java.util.ArrayList;
abstract class BaseClass {
    protected List list; // visible from subclasses

    public BaseClass() {
        System.out.println("to abstract...");
        // common initialization to all subclasses
        list = new ArrayList();
        list.add("a");
        list.add("a");
        list.add("a");
    }
}

class ConcreteClass extends BaseClass {
    public ConcreteClass(){
        // The list is initialized already
        System.out.println("now it is concrete and the list is: = "+ this.list );


    }
}

class TestAbstractClass {
    public static void main( String [] args ) {
        BaseClass instance = new ConcreteClass();
    }

}

Output 产量

$ java TestAbstractClass
to abstract...
now it is concrete and the list is: = [a, a, a]

De-duplicating common knowledge/behaviour. 重复复制常识/行为。

Eg a Car: all your cars will be constructed out of a body and four wheels and an engine. 例如汽车:所有汽车都将由车身,四个车轮和一个发动机构成。 So you do this part of the construction in the constructor for the abstract Car class by calling functions like Body(), Wheel(int x), Engine(). 因此,您可以通过调用Body(),Wheel(int x),Engine()等函数在抽象Car类的构造函数中完成构造的这一部分。 Each particular car class will have their own implementation of Body(), Wheel() and Engine() - but they all will do the same steps to construct the car from them, so there is no need to duplicate those steps in each of those classes. 每个特定的汽车类都有自己的Body(),Wheel()和Engine()实现 - 但它们都将采用相同的步骤从它们构建汽车,因此不需要在每个汽车中复制这些步骤类。 In this case you implement that common behaviour in the ancestor. 在这种情况下,您在祖先中实现了这种常见行为。

I agree, Constructors are created assuming there will be instances. 我同意,构造函数是在假设存在实例的情况下创建的。 If you have lot of common code you can think of creating a Constructor but it is much better to put it in a init() method. 如果您有很多常用代码,可以考虑创建一个构造函数,但将它放在init()方法中要好得多。

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

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