【发布时间】: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上。 -
我猜你是对的。难怪我一直在尝试调用该方法,但似乎找不到构造函数。