java - Java中如何将子类中的方法传递给主类

标签 java console-application

我的代码有一些问题。我有3个类(class):主类、家长类、 child 类。

我对构造函数部分有点困惑。我所做的就是拥有一个父构造函数和子构造函数。

我创建了一个名为 addEmployee 的方法 然后尝试将此方法传递给主类
我想做的是将子类中的方法“addEmployee Method 传递给我的主类,但当我尝试在主类中实例化新对象时似乎出现错误。

谁能帮我解决这个问题?

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


MAIN CLASS
public class EmployeeInformation {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //
        Scanner sc = new Scanner (System.in);
System.out.println("");
String Name = sc.next();

        Employee myemp = new Employee(Name);
    myemp.getName();

    myemp.addEmployee();
    String name = sc.next();


    }


}

PARENT CLASS
package employeeinformation;
public class Person {
    private String name;
    private String ID;
    private String address;
    private String gender;
public Person(String Name , String ID, String address, String gender)
{
    this.name = Name;
    this.ID = ID;
    this.address = address;
    this.gender = gender;
}

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the ID
     */
    public String getID() {
        return ID;
    }

    /**
     * @param ID the ID to set
     */
    public void setID(String ID) {
        this.ID = ID;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the gender
     */
    public String getGender() {
        return gender;
    }

    /**
     * @param gender the gender to set
     */
    public void setGender(String gender) {
        this.gender = gender;
    }

}

CHILD CLASS

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package employeeinformation;

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author salomon
 */
public class Employee extends Person {
Scanner sc = new Scanner (System.in);
    private String employmentType;
    private String empID;
    ArrayList<Employee> el = new ArrayList<Employee>();


 Employee(String Name, String ID, String address, String gender, String employmentType, String empID) {
        super(Name, ID, address, gender);
        setEmpID(empID);
        setEmploymentType(employmentType);

    }



    /**
     * @return the employmentType
     */
    public String getEmploymentType() {
        return employmentType;
    }

    /**
     * @param employmentType the employmentType to set
     */
    public void setEmploymentType(String employmentType) {
        this.employmentType = employmentType;
    }

    /**
     * @return the accesscard
     */


 public void addEmployee()å
{
 System.out.println("Enter your Full Name");
 String name = sc.next();
 System.out.println("Enter your NRIC/ID");
 String ID = sc.next();
 System.out.println("Enter your Address");
 String address = sc.next();
 System.out.println("Enter your Gender");
 String gender = sc.next();
 System.out.println("Enter your Employment Type");
 String employmentType = sc.next();
 System.out.println("Enter your Employment ID");
 String employmentID = sc.next();


Employee empobj;
empobj = new Employee(name, ID, address, gender, employmentType, getEmpID());
el.add(empobj);
System.out.println(el);
}

 @Override
    public String toString() {
        return "Employee Details " + getName() + getAddress() + getID() + getGender(); //To change body of generated methods, choose Tools | Templates.
    }

    /**
     * @return the empID
     */
    public String getEmpID() {
        return empID;
    }

    /**
     * @param empID the empID to set
     */
    public void setEmpID(String empID) {
        this.empID = empID;
    }

    /**
     * @param empID the empID to set
     */

}

最佳答案

在您的 main 方法中,您正在使用下面的构造函数来创建一个新对象。

Employee myemp = new Employee(Name);

但是你的 Employee 类中没有这样的构造函数。尝试将以下构造函数添加到您的 Employee 类

 Employee(String name){
    super(name);
    setName(name);
}

以及 Person 类的以下构造函数

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

请注意,您只能使用在对象类中声明的构造函数。否则,您可以尝试创建一个空对象,并在主方法中使用 setter 将属性设置为各个元素,如下所示。

Employee myemp = new Employee();
myemp.setName(name);

为此,您需要在 Employee 类中有一个空构造函数,如下所示

Employee(){}

关于java - Java中如何将子类中的方法传递给主类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54301824/

相关文章:

java - 需要多少内存位置?

java - 扩展远程与 UnicastRemoteObject

java - 如何分割这个字符串?

swift - 范围结构可以用作 Swift 中的用户输入吗?

windows - 如何在控制台中检测当前是否启用 Windows 10 缓冲区包装模式

c++ - 如何停止控制台应用程序中的闪烁?

java - 在多个资源请求的情况下恢复更改的最佳做法是什么?

java - 我可以出于测试目的修改字段的值吗?

c++ - Qt开发lib和跨平台截屏是真的吗?

.net - 使用 LoggerFactory 登录 Application Insights for .NET Core 2.1 控制台应用程序