简体   繁体   English

没有默认构造函数的Java类

[英]Java class without default constructor

I know if default constructor isn't declared and my class don't have any other constructor Java generates a default constructor for me automatically. 我知道是否未声明默认构造函数,并且我的课程没有其他构造函数,Java会自动为我生成一个默认构造函数。 If I declare some constructor, I need declare the default constructor manually. 如果声明一些构造函数,则需要手动声明默认构造函数。

Why is this case lac4 contains default values in attributes which not have been initilized in constructor and I don't have default constructor ("ataque", "vidaAtual" and "vidaMaxima" contains 0)? 为什么在这种情况下lac4包含未在构造函数中 初始化的属性中的默认值, 而我没有默认构造函数 (“ ataque”,“ vidaAtual”和“ vidaMaxima”包含0)? I know that Java initializes attributes with default values but for my this occurs only in the default constructor. 我知道Java会使用默认值初始化属性,但是对我来说,这仅发生在默认构造函数中。

public class CartaLacaio {

    private int ID;
    private String nome;
    private int ataque;
    private int vidaAtual;
    private int vidaMaxima;
    private int custoMana;

    public CartaLacaio(int ID, String nome, int ataque, int vida, int mana) {
        this.ID = ID;
        this.nome = nome;
        this.ataque = ataque;
        this.vidaAtual = vida;
        this.vidaMaxima = vida;
        this.custoMana = mana;
    }

    public CartaLacaio(int ID, String nome, int custoMana){
        this.ID = ID;
        this.nome = nome;
        this.custoMana = custoMana;
    }

    public CartaLacaio(CartaLacaio origem){

    }

   //Getters and setters

} }

My main 我的主要

public static void main(String[] args) {
        CartaLacaio lac1 = new CartaLacaio(1, "Frodo Bolseiro", 2, 1, 1);
        CartaLacaio lac2 = new CartaLacaio(2, "Aragorn", 5, 7, 6);
        CartaLacaio lac3 = new CartaLacaio(3, "Legolas", 8, 4, 6);
        CartaLacaio lac4 = new CartaLacaio(4, "Teste nome", 3);
    }

...but for my this occurs only in the default constructor. ...但是对于我来说,这仅发生在默认构造函数中。

No, that happens when the instance is created, before any constructor is called. 不,在创建实例之前,即在调用任何构造函数之前,都会发生这种情况。


Just FWIW, if you had initializers on those fields, eg: 如果您在这些字段上具有初始化程序,则只需FWIW,例如:

public class Example {
    private int a = 42;

    // ...
}

...the code to set those initializers would be inserted automatically into the beginning of every constructor in the class by the compiler. ...用于设置这些初始值设定项的代码将由编译器自动插入到该类中每个构造函数的开头。


I know if default constructor isn't declared and my class don't have any other constructor Java generates a default constructor for me automatically. 我知道是否未声明默认构造函数,并且我的课程没有其他构造函数,Java会自动为我生成一个默认构造函数。 If I declare some constructor, I need declare the default constructor manually. 如果声明一些构造函数,则需要手动声明默认构造函数。

You're confusing two separate things. 您在混淆两件事。

The default constructor is the constructor the compiler will generate for you if you don't provide any constructors. 默认构造函数是如果您不提供任何构造函数,则编译器将为您生成的构造函数。

A zero-parameters constructor is a constructor that accepts no parameters. 零参数构造函数是不接受任何参数的构造函数。

The default constructor is a zero-parameters constructor. 默认构造函数是零参数构造函数。 If you provide a zero-parameters constructor explicitly, it's not a default constructor. 如果显式提供零参数构造函数,则它不是默认构造函数。 "Default" means just that: The default , if no other is provided. “默认”仅表示: 默认值 ,如果未提供其他值。

You are wrong in your statement 你的陈述是错误的

I know that Java initializes attributes with default values but for my this occurs only in the default constructor. 我知道Java会使用默认值初始化属性,但是对我来说,这仅发生在默认构造函数中。

Java automatically initializes attributes with default values also for overloaded constructor or a non-default constructor Java也会为重载的构造函数或非默认构造函数自动使用默认值初始化属性

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

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