public class testconstructor{
public static void main(String args[])
{
Employee employee=new Employee("李平");
employee.employeeSex="男";
employee.setEmployeeSalary(1200);
System.out.println("员工的基本信息:"+employee.toString()+employee.getEmployeeSalary());
CommonEmployee commonEmployee=new CommonEmployee("李霄云",400);
commonEmployee.employeeSex="女";
System.out.println("员工的基本信息:"+commonEmployee.toString());
}
}
class Employee
{
private String employeeName;
int employeeNo;
String employeeSex;
private double employeeSalary;
static int employeeNextNo=10000;
static double mini_salary=600;
Employee(){
employeeNo=employeeNextNo++;
System.out.println("调用父类Employee的构造函数。");
}
Employee(String name)
{
System.out.println("调用父类Employee的有参构造函数。");
employeeNo=employeeNextNo++;
employeeName=name;
}
/*public void setEmployeeName(String name){
employeeName=name;
}
public void setEmployeeSex(String sex){
employeeSex=sex;
}
*/
public double getMini_salary()
{
return mini_salary;
}
public int getEmployeeNo()
{
return employeeNo;
}
public double getEmployeeSalary()
{
return employeeSalary;
}
public void setEmployeeSalary(double salary)
{
employeeSalary=salary+mini_salary;
}
public String toString()
{
String s;
s="编号:"+employeeNo+"姓名:"+employeeName;
if(employeeSex.compareTo("男")==0)
s=s+"sex is man";
else
s=s+"sex is women";
return(s+":工资");
}
}
class CommonEmployee extends Employee
{
private double bonus;
CommonEmployee()
{
System.out.println("调用子类CommandEmployee的构造函数");
}
CommonEmployee(String name,double bonus){
super(name);
this.bonus=bonus;
System.out.println("diaoyong zilei CommonEmployee de youcan gouzao hanshu.");
}
public double getBonus(double newBonus)
{
return bonus;
}
public void setBonus(double newBonus)
{
bonus=newBonus;
}
public double getEmployeeSalary()
{
double salary=getMini_salary();
return bonus + salary;
}
public String toString()
{
String s;
s=super.toString();
return(s+":salary:"+getEmployeeSalary()+" ");
}
} |
|