构造函数,Setter和Getters也是辅助方法

时间:2016-04-01 22:21:26

标签: java

我很难理解构造函数是什么,而我的老师不是很有帮助。

我已经包含了部分代码,那就是员工类。我也有一个主要课程。我不想把它全部包括在内,因为我已经完成了这项任务,假设我使用的是构造函数并且不想分享我的工作。

我希望有人能指出我是否使用构造函数?我知道这听起来很傻,但我不知道构造函数是什么。我应该使用3不包括默认值。我写了这个程序的2个版本,一个带有扫描仪和数组,另一个带有预定义的数组。如果我没有使用构造函数,请告知它们是什么。如果我有请指出他们在哪里,我可以看到。

我非常感谢你们的帮助,非常感谢你们。

还有什么是辅助方法?

public class Employee{ 

    String firstname = ""; 
    String lastname = ""; 
    String phone = ""; 
    String address = "";
    String id = "";
    String title = "";
    String salary = "";



    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSalary() {
        return salary;
    }

    public void setSalary(String salary) {
        this.salary = salary;
    }

    //-----------------------------------------------------------------------           
    //ToString Method
    public String toString(){
        return "First Name:" + this.firstname +  "\n" + 
               "Last Name:" + this.lastname + "\n" + 
               "Phone #: " + this.phone +  "\n" +
               "Address: " + this.address +  "\n" + 
               "ID: " + this.id + "\n" + 
               "Title: " + this.title + "\n" + 
               "Salary: " + this.salary;
    }
    //-----------------------------------------------------------------------       
    //Equals method
    public boolean equals (Employee other){
        return phone.equals(other.phone);
    }
   //------------------------------------------------------------------------       
      //Methods to change 

    public void changePhone(String newPhone) {
        phone = newPhone;
    }
    public void changeId(String newId) {
        id = newId;
    }
    public void changeTitle(String newTitle) {
        title = newTitle;
    }
}       

6 个答案:

答案 0 :(得分:3)

构造函数是一个与该类相同的方法。您没有任何名为Employee的方法,因此您没有任何构造函数。

您可以拥有多个具有不同参数的构造函数。因为这是Employee类构造函数看起来像:

public class Employee {
    // Default constructor, no arguments.
    public Employee() {
        ...
    }

    // Two argument constructor.
    public Employee(String arg1, int arg2) {
        ...
    }
}

构造函数的目的是初始化一个对象。对于这个类,你可以创建一个无参数构造函数,将所有字段设置为空白,以及一个多参数,它接受名字,姓氏等,并将字段初始化为这些值。

注意构造函数也没有返回类型。你不写public void Employee(),它只是public Employee()。构造函数的工作方式类似于返回void的函数,但您不会编写void。它是构造函数的特殊语法的一部分。

使用new时会调用构造函数。 new的参数将传递给适当的构造函数。例如,您可以在创建两个不同的员工对象时调用上面的两个构造函数:

// Call the default constructor.
Employee jack = new Employee();

// Call the two argument constructor.
Employee jill = new Employee("Jill", 42);

辅助方法是一个非正式术语。这是一种由其他代码调用的方法,通过卸载一些工作来“帮助”代码。

让我们假装你有一个方法按顺序执行步骤A; B; C; D; A; B; C;。那有点重复,不是吗?您可以将步骤A; B; C;移动到辅助方法 - 将其命名为H - 然后将第一个方法重写为H(); D; H();。这将是一个帮助方法。

答案 1 :(得分:1)

你没有使用我能看到的任何构造函数。 构造是在创建new对象时调用的对象函数。您可以使用它来设置默认值或参数。

class Car
{
private String carName;

public Car(String name)
{
this.carName = name;
}


public void setCarName(String name)
{
this.carName = name;
}

public String getCarName()
{
return this.carName;
}

}

答案 2 :(得分:1)

先生,您使用的是getter / setter,但没有使用构造函数。

将此添加到您的班级(与所有字段一起更换等等:P):

public Employee(String firstname, String lastname, etc. etc.){
this.firstname = firstname;
this.lastname = lastname;
//same thing for other fields
}

答案 3 :(得分:1)

构造函数与您的Class具有相同的名称,但没有返回类型。

public class Employee{ 

    String firstname = ""; 
    String lastname = ""; 
    String phone = ""; 
    String address = "";
    String id = "";
    String title = "";
    String salary = "";

    public Employee(){
        // default constructor
    }

    public Employee(String firstName, String lastName){
        // call another constructor
        this(firstName, lastName, this.title);
    }

    public Employee(String firstName, String lastName, String title){
        this.firstName = firstName;
        this.lastName = lastName;
        this.title = title;
    }
}

Constructers用于创建new个关键字的对象。

Employee e1 = new Employee(); // no parameters, use all initialized values;
Employee e2 = new Employee("Bill", "Gatz"); // 2 parameters, assign firstname and last name

答案 4 :(得分:1)

在你的代码中没有构造函数。

构造函数是一个在实例化的类时调用的函数(即当您使用关键字new时)

构造函数允许的唯一语法是

public class Employee{

public Employee(){
    //constructor
}

public Employee(String arg){
    //overloaded constructor that takes a string as argument
}

没有返回类型,它必须与父类具有相同的名称(您可能还有私有构造函数,但很少找到它们)

只要你写

,就会调用第一个构造函数
Employee employee = new Employee();

和第二个给你的参数(在这个例子中是字符串)

Employee employee = new Employee("Employee name");

Java会根据您提供的参数自动选择要调用的构造函数

答案 5 :(得分:0)

当创建对象时,Java首先调用构造函数,因为您的字段变量被标记为private,所以必须首先使用值创建Object。

那就是构造函数的用途。它们与类相同,它们没有返回类型

据我所知,你班上没有构造函数。