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

发布于 2024-08-18 05:25:09 字数 53 浏览 3 评论 0原文

我想知道抽象类的构造函数有什么作用;既然我们不实例化抽象类,为什么我们需要这样的构造函数呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

赠佳期 2024-08-25 05:25:09

有时,所有继承类都需要设置实例变量的一些公共初始化。当您扩展抽象类时,您会实例化它,并且该具体类具有一个构造函数,该构造函数将向抽象类的构造函数提供参数。

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.

临走之时 2024-08-25 05:25:09

它们仍然可以由从该类继承的类的构造函数调用,从而使代码重构能够很好地利用抽象类中的构造函数。

They can still be invoked by constructors of classes that inherit from that one, making code refactoring a good use for having a constructor in the abstract class.

能怎样 2024-08-25 05:25:09

如果抽象类中有未初始化的最终字段,则需要在构造函数中初始化它们。

例如,

abstract class A {
    final int x;
}

如果没有分配给 x 的构造函数,则无法编译。

If you have uninitialised final fields in an abstract class, you'll need to initialise them in a constructor.

E.g.

abstract class A {
    final int x;
}

will not compile without a constructor that assigns to x.

属性 2024-08-25 05:25:09

如果您的类没有声明构造函数,javac 将为您创建一个无参数、不执行任何操作的构造函数。然后,当你的子类初始化时,它将调用生成的无操作构造函数,一切都很好。

但是,如果您的类声明了任何构造函数,javac 将不会为您创建一个构造函数。在这种情况下,子类构造函数需要显式调用父类的构造函数。否则,正如上面的答案所提到的,您将无法初始化父类的成员。

If your class doesn't declare a constructor, javac will make a no-arg, do-nothing constructor for you. Then, when your subclass is initialized, it will call the generated no-op constructor and life is good.

However, if your class declares any constructor, javac will NOT make one for you. 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.

多像笑话 2024-08-25 05:25:09

抽象类的构造函数由子类使用(使用 super(params) 从子类构造函数调用)。

您应该使这些构造函数受保护以明确这一点。

Constructors for abstract classes are used by subclasses (invoked from subclass constructors using super(params) ).

You should make these constructors protected to make that clear.

桃扇骨 2024-08-25 05:25:09

您不实例化抽象类,但在实例化子类时会调用构造函数。

用途可以是初始化公共属性,即。

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();
    }

}

输出

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

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]
九公里浅绿 2024-08-25 05:25:09

消除重复的常识/行为。

例如汽车:您所有的汽车都将由车身、四个轮子和一个发动机构成。因此,您可以通过调用 Body()、Wheel(int x)、Engine() 等函数在抽象 Car 类的构造函数中完成这部分构造。每个特定的汽车类都有自己的 Body()、Wheel() 和 Engine() 实现 - 但它们都将执行相同的步骤来构造汽车,因此无需在每个汽车类中重复这些步骤类。在这种情况下,您可以在祖先中实现该常见行为。

De-duplicating common knowledge/behaviour.

E.g. 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(). 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. In this case you implement that common behaviour in the ancestor.

半寸时光 2024-08-25 05:25:09

我同意,构造函数是在假设存在实例的情况下创建的。如果您有很多通用代码,您可以考虑创建一个构造函数,但最好将其放在 init() 方法中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文