【问题标题】:How to pass methods in the child class to the Main class in Java如何将子类中的方法传递给Java中的Main类
【发布时间】:2019-01-22 05:32:57
【问题描述】:

我的代码有问题。我有3个班级,主班,父班,子班。

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

我创建了一个名为 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
     */

}

【问题讨论】:

  • 你能显示错误信息吗?
  • 您的设计需要改进。 Employee 应该是一个实体类,其中包含有关 one 员工的信息。 Scanner、员工列表和 addEmployee() 方法属于别处,可能在 EmployeeInformation 上。
  • 我猜你是对的。难怪我一直在尝试调用该方法,但似乎找不到构造函数。

标签: java console-application


【解决方案1】:

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

Employee myemp = new Employee(Name);

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

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

以及下面的构造函数到你的 Person 类

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

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

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

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

Employee(){}

【讨论】:

  • 我明白了,如果我要创建一个空对象。会不会像下面这样 public Employee() { this.name = name;这个.ID = ID; this.address = 地址; this.gender = 性别; }
  • 我认为变量(name、id、gender、address)是继承自其父类person。那么我需要将它们包含在员工构造函数中吗?
  • 我仍然对contsructor感到困惑。如我错了请纠正我。在员工中创建的构造函数然后当我在主类中不满足时,它取决于该子类的构造函数中的内容。我的理解正确吗?
  • 是的,需要在子类中继承父构造函数的参数。正如我在答案中提到的,如果您在父类(Person)中创建一个只有名称参数的构造函数,那么您可以在您的子类(Employee)中创建一个只有一个参数的构造函数。
  • 如果你想创建一个空的构造函数,它看起来像 Employee(){} 并且在超类中像 Person(){}
【解决方案2】:

将代码Employee myemp = new Employee(Name);更改为Employee myemp = new Employee(Name , address, gender, employmentType, empID);向Employee构造函数提供地址、性别等。

(或)

创建一个参数。 Person 和 Employee 类中的构造函数。

public Employee(String name){
  super(name);
  this.name = name;
}

【讨论】:

    【解决方案3】:

    pass objects-vars 有一个例子,也许它可以帮助你。

    但是在标题上,您不传递方法(调用它们并获得结果),而是将对象或变量传递给其他类(实例)。

    【讨论】:

      猜你喜欢
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      • 2012-08-21
      • 2011-03-27
      • 2013-02-16
      • 1970-01-01
      • 2021-04-12
      相关资源
      最近更新 更多