简体   繁体   English

如果构造函数没有构建对象,那么用来制作对象的是什么?

[英]If constructor not build the object what is used to make the object?

According to this Can an abstract class have a constructor? 据此, 抽象类可以具有构造函数吗? the constructor does not build the object and it is used to initialize fields so what is used to build the object? 构造函数不构建对象,而是用于初始化字段的,那么用于构建对象的是什么? As in abstract class the constructor exists and it's not allowed to make an instance from that class and only could inherit it. 抽象类一样 ,构造函数也存在,并且不允许从该类中创建实例,而只能继承它。
so the question is 所以问题是

What really builds the object in java ?? 真正在Java中构建对象的是什么?

An instance of an object in Java is created every time you use the new keyword. 每次您使用new关键字时,都会在Java中创建一个对象的实例。 For example, 例如,

MyClass myObject1 = new MyClass("John", 1, 1000.20);
MyClass myObject2 = new MyClass("Mary", 2, 200);
MyClass myObject3 = new MyClass();

However, you can only instantiate a concrete class and never a abstract class. 但是,您只能实例化具体类,而不能实例化抽象类。 So a question arises, what's the point in having an abstract class? 因此出现一个问题,拥有抽象类有什么意义?

You have a abstract class to provide a common set of functionalities which can be shared by all its subclasses , eg, attributes, fields, etc. 您有一个抽象类来提供一组通用的功能,该功能可以由其所有子类 (例如属性,字段等)共享。

And of course, an abstract can have a constructor. 当然,抽象可以具有构造函数。 If you don't provide a constructor explicitly, it will have a default constructor . 如果您未明确提供构造函数,则它将具有默认的构造函数 Whenever one of its concrete subclasses is instantiated, the constructor of the subclass will call the super constructor before doing any type of work in its own constructor. 每当实例化其具体的子类之一时,该子类的构造函数将在其自身的构造函数中执行任何类型的工作之前调用超级构造函数。 For example, 例如,

public abstract class MyAbstractClass {
   private String name;

   private int age;

   public MyAbstractClass() {
       name = "bozo";
       age = 5;
   }

  public MyAbstractClass(String name, int age) {
      this.name = name;
      this.age = age;
  }
  ...
}

public class MyClass extends MyAbstractClass {
    private double income;

    public MyClass() {
        //calls super() implicitly.
    }

    public MyClass(String name, int age, double income) {
        //calls super explicitly
        super(name, age);
        this.income = income;
    }
}

Constructor is mandatory for any class. 构造函数对于任何类都是必需的。

All classes (abstract like concrete) have to declare a constructor. 所有类(像混凝土一样抽象)都必须声明一个构造函数。
It may be a generated no arg constructor or a explicitly declared constructor but anyway, the constructor presence is mandatory. 它可能是未生成的arg构造函数,也可能是显式声明的构造函数,但是无论如何,构造函数的存在是强制性的。
Why ? 为什么呢
Because for any class that inherits from a class (that is : all classes but Object ), the first statement of any invoked constructor is invoking the direct parent constructor, that may at its turn invoke its parent constructor, and so on. 因为对于从类继承的任何类(即,除Object所有类),任何调用的构造函数的第一条语句都在调用直接父构造函数,而后者又可能调用其父构造函数,依此类推。

Use the new operator to create an instance 使用new运算符创建实例

As you want to create an instance of a specific class, you have to invoke the constructor of its class by prefixing it with the new operator : 当您要创建特定类的实例时,必须通过在类的构造函数前添加new运算符来调用该类的构造函数:

new MyObject();

But it makes sense only for concrete classes. 但这仅对具体的类有意义。

Indeed, abstract classes cannot be instantiated 确实,抽象类无法实例化

So, the constructor of the abstract class will be used but only as you create an instance of a child class (whatever its level in the hierarchy) of this abstract class (what I explained in the first point). 因此,将仅使用抽象类的构造函数,但仅当您创建该抽象类的子类(无论其层次结构中的级别)的实例时(我在第一点中已经解释过)。

Suppose Child is concrete and Parent is abstract, you have to invoke the parent constructor of Child in the Child constructor : 假设Child是具体的而Parent是抽象的,则必须Child构造函数中调用Child的父构造函数:

 Child(){super();}

But you will never invoke new Parent() as it is not legal to instantiate an abstract class. 但是您永远不会调用new Parent()因为实例化抽象类是不合法的。

The new Operator does build the Object indeed. 新的操作员确实可以构建对象。

The reason why an abstract class always has a constructor (explicit or implicit) is because it is called from it's not abstract subclasses. 抽象类始终具有构造函数(显式或隐式)的原因是因为它不是抽象子类而被调用。

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

相关问题 构造方法对象用作方法类型 - Constructor object used as method type 有没有一种优雅的方法可以根据用于创建该对象的构造函数来更改类的对象可以使用的方法/变量? - Is there an elegant way to change what methods/variables an object of a class can use based on the constructor used to create this object? 对象类构造函数中会发生什么 - What will happen in Object class constructor 构造函数链中的对象类型是什么? - What will be the type of Object in Constructor chaining? 如何知道在创建对象时使用了哪个构造函数? - how to know which constructor was used in creating an object? 什么时候关闭HSSFWorkbook的构造函数中使用的InputStream对象? - When to close the InputStream object used in the constructor of HSSFWorkbook? 如何模拟对象构造函数中使用的函数(使用Mockito)? - How to mock a function used in the object constructor (with Mockito)? 创建子 Object,未使用子构造函数 OOP - Creating Child Object, Child Constructor Not Used OOP Java中的对象初始值设定项和构造函数有什么区别? - what is the difference between an object initializer and a constructor in java? Java中调用对象类的构造函数的目的是什么? - What is the purpose of calling the constructor of the object class in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM