1)要求尽可能隐藏所有变量(能够私有就私有,能够保护就不要公有),
再通过GetXXX()和SetXXX()方法对各变量进行读写。具有一个抽象的play()方法,
该方法不返回任何值,同时至少定义两个构造方法。Role类中要体现出this的几种用法。
2)从Role类派生出一个"Employee"类,该类具有Role类的所有成员(构造方法除外),
并扩展salary成员变量,同时增加一个静态成员变量“职工编号(ID)”。
同样要有至少两个构造方法,要体现出this和super的几种用法,还要求覆盖play()方法,
并提供一个final sing()方法。
3)“Manager"类继承"Employee"类,有一个final成员变量"vehicle”
在main()方法中制造Manager和Employee对象,并测试这些对象的方法。
定义一个抽象的"Role"类,有姓名,年龄,性别等成员变量
1)要求尽可能隐藏所有变量(能够私有就私有,能够保护就不要公有),
再通过GetXXX()和SetXXX()方法对各变量进行读写。具有一个抽象的play()方法,
该方法不返回任何值,同时至少定义两个构造方法。Role类中要体现出this的几种用法。
2)从Role类派生出一个"Employee"类,该类具有Role类的所有成员(构造方法除外),
并扩展salary成员变量,同时增加一个静态成员变量“职工编号(ID)”。
同样要有至少两个构造方法,要体现出this和super的几种用法,还要求覆盖play()方法,
并提供一个final sing()方法。
3)"Manager"类继承"Employee"类,有一个final成员变量"vehicle"
在main()方法中制造Manager和Employee对象,并测试这些对象的方法。
*/
abstract class Role
{
private String name;
private int age;
private String sex;
public Role()
{
System.out.println("******Role构造方法1*****");
}
public Role(String n)
{
System.out.println("*******Role构造方法2*****");
System.out.println(n + this);
}
public void setName(String name) {
this.name = name;//this调用本类属性
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSex() {
return sex;
}
public void getPersonInfo()
{
System.out.println("姓名:"+name+" 年龄: "+age + " 性别:"+sex);
}
public abstract void play();
}
class Employee extends Role
{
private int salary;
static int ID;
public Employee()
{
System.out.println("******Employee构造方法1*****");
}
public Employee(int x)
{
this();//this调用本类方法
System.out.println("******Employee构造方法2*****");
this.salary = x;
System.out.println("salary is " +salary);
}
public void Role(String n,int a )
{
this.ID = a;
System.out.println("*******子类覆写Role构造方法2*****");
System.out.println(n + this);//this表当前对象
}
final void sing()
{
//super.(n);
System.out.println("sing a song");
}
public void play()
{
System.out.println("play football");
}
}
class Manager extends Employee
{
public final String vehicle = "can not exchange";
}
public class Text {
public static void main(String[] args)
{
Employee per = new Employee(1000);
Manager per1 = new Manager();
per.setName("张三");
per.setAge(33);
per.setSex("男");
per.getPersonInfo();
per.Role("six",11111111);
per.play();
per.sing();
}
}