定义一个抽象的"Role"类,有姓名,年龄,性别等成员变量

定义一个抽象的"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("1.无参构造");
    }
    public Role(String n,int a){
        this.name=n;
        this.age=a;
        System.out.println("2.有参构造");
    }
    public void setName(String n){
        this.name=n;
    }
    public String getName(){
        return name;
    }
    public void setAge(int a) {
        this.age=a;
    }
    public int getAge(){
        return age;
    }
    public void setSex(String s){
        this.sex=s;
    }
    public String getSex(){
        return sex;
    }
    public void getInfo(){
        System.out.println("name:"+this.name+" age:"+this.age+" sex:"+this.sex);
    } 
    abstract void play();
}
class Employee extends Role{
    static int ID;
    private int salary;
    public Employee(){
        System.out.println("3.无参构造");
    }
    public Employee(int i){

        this.ID=i;
        System.out.println("4.有参构造");
    }
    public void setSalery(int s){
        this.salary=s;
    }
    public int getSalery(){
        return salary;
    }
    public void setID(int i){
        this.ID=i;
    }
    public int getID(){
        return ID;
    }
    public void play(){
        System.out.println("我在飞!");
    }
    final public void sing(){
        System.out.println("我在唱歌!");
    }
    public void getInfo(){
        super.getInfo();
        System.out.println("ID:"+this.ID+" salary:"+this.salary);
    } 
}
class Manager extends Employee{
    final public void vehicle(){
        System.out.println("最后一个");
    }
}
public class Test{
    public static void main(String[] args){
        Manager ma=new Manager();
        Employee em=new Employee(521);
        em.setAge(18);
        em.setName("Dyson");
        em.setSex("man");
        em.setSalery(10000000);
        em.getInfo();
        em.play();
        em.sing();
        //构造方法执行顺序在普通方法之前
        ma.vehicle();
    }
}

猜你喜欢

转载自blog.csdn.net/ds19980228/article/details/83348875