java编写salary函数_编写一个Java程序,在程序中包含一个Employee类,Employee类包含name、age、salary三个成员变量...

2023-05-16

编写一个Java程序,在程序中包含一个Employee类,Employee类包含name、age、salary三个成员变量,Employee类中有4个构造方法,分别为无参的、带一个参数用来对name属性初始化的、带两个参数用来对name和age属性初始化的以及带三个参数用来对三个属性进行初始化的,有一个setAge方法设置age属性值,getAge方法返回age值。编写主类创建该类的三个对象,并把它们的属性输出。

class Employee

{

//三个私有变量

private String name;

private int age;

private int salary;

//下面是4个构造函数

public Employee()

{

}

public Employee(String name)

{

this.name = name;

}

public Employee(String name ,int age)

{

this.name = name;

this.age = age;

}

public Employee(String name ,int age,int salary)

{

this.name = name;

this.age = age;

this.salary = salary;

}

//set get器

public void setName(String name)

{

this.name = name;

}

public String getName()

{

return this.name;

}

public void setAge()

{

this.age = age;

}

public int getAge()

{

return this.age;

}

public void setSalary()

{

this.salary = salary;

}

public void getSalary()

{

return this.salary;

}

}

//这个和文件名一致 也就是说,你必须创建一个printEmployee.java的文件。然后才可以编译它

public class printEmployee

{

public static void main(String args[])

{

Employee e = new Employee("wanjunfu",25,5000);

System.out.println(e.getName);

System.out.println(e.getAge);

System.out.println(e.getSalary);

//也可以怎么弄。

System.out.println("=============================================");

Employee e1 = new Employee();

e1.setAge(25);

e1.setName("wanjunfu");

e1.setSalary(5000);

System.out.println(e.getName);

System.out.println(e.getAge);

System.out.println(e.getSalary);

}

}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

java编写salary函数_编写一个Java程序,在程序中包含一个Employee类,Employee类包含name、age、salary三个成员变量... 的相关文章

随机推荐