无参数构造函数的目的是什么? [英] What is the purpose of a no-arg constructor?

查看:57
本文介绍了无参数构造函数的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解下面的大多数代码.我只是不了解Employee和Person的无参数构造函数的目的,因为删除它们时会得到相同的结果.我是Java的新手,所以如果这是一个愚蠢的问题,我深表歉意.这是我的一个课程的代码段.任何帮助将不胜感激.

I understand most of the code below. I just don't understand the purpose of the no-arg constructors of Employee and Person since I get the same results when I delete them. I am new to java so I apologize if this is a dumb question. This is a code snippet for one of my classes. Any help will be appreciated.

public class TestEmployee {
    public static void main(String[] args) {        
        // Define some employees
        Employee president  = new Employee("Lucy", "President", 100000);        
        System.out.println(president);  


        Employee cto  = new Employee("Vincent", "Chief Tech Officer", 70000);       
        System.out.println(cto);    
    }
}

class Employee extends Person{
    // ADD YOUR CODE HERE!!!
    // Nothing above needs to change.
    private String jobTitle="Unknown";
    private int salary = 0;

    public Employee() {

    }

    public Employee(String name, String jobTitle, int salary) {
        super(name);
        this.jobTitle = jobTitle;
        this.salary = salary;
    }

    @Override
    public String toString() {
        return this.getName() + " is the " + jobTitle + " and makes $" + salary + " a year.\n";
    }
    // You need data fields, a constructor or two, and a to-string method
}

// IF YOU ALREADY HAVE THE PERSON CLASS IN YOUR WORKSPACE
// YOU CAN DELETE THIS. OTHERWISE YOU'LL GET AN ERROR:
// 'The type Person is already defined.'
class Person {
    private String name = "Default Name";

    // No-arg Constructor
    public Person () {
    }

    public Person (String name){
        this.name = name;
    }

    // Getter method
    public String getName() {
        return name;
    }

    // Setter method
    public void setName(String newName) {
        if (!newName.equals("")) {
            name = newName;
        }
        else
            System.out.println("Can't change name. Empty names aren't allowed!");
    }

    public String toString() {
        return "Name: " + name + "\n";
    }
}

推荐答案

1.由于您只是Java的初学者,所以这是一个完全正确的问题(尽管我建议在发布问题之前对此主题进行一些研究).

1.Since you are just a beginner in Java,this is a perfectly valid question(although, i recommend doing some research on the topic before posting questions).

2.现在,提一个问题.

2.Now, coming to the question.

(a)您要引用的no-arg构造函数是对默认构造函数"的显式定义替代.如果程序员未明确定义构造函数,则编译器(javac)会自动将默认构造函数定义为 ClassName(){} .此构造函数在代码中不可见,但是编译完成后,它存在于Java字节码中.

(a)The no-arg constructor you are referring to is a explicitly defined substitute to the "default constructor".If the programmer doesn't explicitly define a constructor,then the compiler(javac) automatically defines a default constructor as ClassName(){}.This constructor is not visible in the code,however after the compilation is done,it is present in the java bytecode.

(b)在您的情况下,有一个显式定义的no-arg构造函数,原因是您已显式定义一个参数化的构造函数 public Employee(字符串名称,字符串jobTitle,内部薪水)公众人物(字符串名称)如果在程序中显式定义了带参数的构造函数,则Java编译器不会插入隐式默认构造函数.因此,如果希望在不传递任何初始值的情况下创建该类的对象,则 Employee President = new Employee(); 此语句将引发错误.<代码>未找到默认构造函数;默认值为0.嵌套异常是java.lang.NoSuchMethodException:.因此,为no-arg构造函数提供了明确的定义来支持此类对象的创建,用户可以先为对象分配堆空间,然后使用设置方法或其他一些机制.

(b)In your case, there is a explicitly defined no-arg constructor.The reason being that you have explicitly defined a parameterised constructor public Employee(String name, String jobTitle, int salary) public Person (String name) If parameterised constructors are defined explicitly in a program the Java compiler doesn't insert the implicit default constructor.So if you wish to make the object of the class without passing any initial values Employee president = new Employee(); This statement would throw an errorNo default constructor found; nested exception is java.lang.NoSuchMethodException:.Thus there are explicit definations for the no-arg constructors to support such object creation,where a user may allocate heap space for a object first,and then initialise it later using the setter methods or some other mechanisms.

这篇关于无参数构造函数的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆