欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

实训篇:第十一天

程序员文章站 2022-03-16 21:28:35
...

设计抽象类Door,它拥有所有门的共性方法open ()和close ()。设计防火接口FireSafe,该接口有一个抽象方法fireProof()。设计防弹接口BulletSafe,该接口有一个抽象方法bulletProof()。综合利用继承、抽象类、接口的知识设计出防火门FireProofDoor和防弹门BulletProofDoor两个类。设计Test类的main()方法,分别调用FireProofDoor和BulletProofDoor的所有方法。(实现各方法时做简单的打印即可)

package ch06_homework4_3;

public abstract class Door {
    public abstract void open();
    public abstract void close();
}
package ch06_homework4_3;

public interface BulletSafe {
    public abstract  void bulletProof();
}
package ch06_homework4_3;

public interface FireSafe{
    public abstract void fireProof();
}

 

package ch06_homework4_3;

public class FireProofDoor extends Door implements  FireSafe {

    @Override
    public void open() {
        System.out.println("FireProofDoor open");
    }

    @Override
    public void close() {
        System.out.println("FireProofDoor close");
    }

    @Override
    public void fireProof() {
        System.out.println("this is a FireProofDoor");
    }
}
package ch06_homework4_3;

public class BulletProofDoor extends Door implements BulletSafe {
    @Override
    public void open() {
        System.out.println("BulletProofDoor open");
    }

    @Override
    public void close() {
        System.out.println("BulletProofDoor close");
    }

    @Override
    public void bulletProof() {
        System.out.println("this is a BulletProofDoor");
    }
}
package ch06_homework4_3;

public class practice8 {
    public static void main(String[] args) {
        Door d1 = new FireProofDoor();
        Door d2 = new BulletProofDoor();

        FireProofDoor f = (FireProofDoor) d1;
        BulletProofDoor b = (BulletProofDoor) d2;

        f.fireProof();
        f.open();
        f.close();

        b.bulletProof();
        b.open();
        b.close();
    }
}

1定义一个抽象类Role,有姓名、年龄、性别等成员变量,要求:

  • 尽可能隐藏所有变量,再通过GetXXX()和SetXXX()方法对各变量进行读写;
  • 至少定义两个构造方法(一个无参的构造方法,一个有参的构造方法,有参的构造方法需要显示调用无参的构造方法);
  • Role类中要体现出this的几种用法。
  • 具有一个抽象的无返回值的play()方法;
  • 具有一个普通的say()方法;
  • 提供一个final sing()方法。
  • 各方法中适当地添加打印语句,标识出所在类和所在方法即可。

2从Role类派生出Employee类,要求:

  • 增加一个final成员变量id 
  • 增加一个静态成员变量company;
  • 增加了普通成员变量salary;
  • 通过GetXXX()和SetXXX()方法对新增加的变量进行读写;
  • 要有至少两个构造方法(一个构造方法只有一个参数id并调用父类无参的构造方法,另一个构造方法有多个属性值做参数并调用父类有参的构造方法);
  • 要体现出this和super的用法;
  • 要覆盖play()方法,并在play方法中调用父类的say()方法;
  • 各方法中适当地添加打印语句,标识出所在类和所在方法即可。

3在Test类的main()方法中:

  • 为Employee中的company赋值;
  • 调用Employee的两个构造方法实例化两个对象e1和e2
  • 分别打印e1和e2的属性信息
  • 调用e1的play方法
  • 调用e1的sing方法

注意的地方:

final可以用来修饰抽象类的普通方法,但不能修饰abstract方法

final变量如果没有赋初值是可以之后赋一次值的,之后再次改变就不允许了

package ch06_homework4_4;

public abstract class Role {
    private String name;
    private String age;
    private String gender;

    public Role() {
    }

    public Role(String name, String age, String gender) {
        this();
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public void say() {
        System.out.println("role say");
    }

    @Override
    public String toString() {
        return  "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", gender='" + gender + '\''
                ;
    }

    public final void sing() {
        System.out.println("role sing");
    }
    public abstract void play();
}
package ch06_homework4_4;

public class Employee extends Role{
    private final String id;
    private static String company;
    private String salary;

    public Employee(String id) {
        super();
        this.id = id;
    }

    public Employee(String name, String age, String gender, String id, String salary) {
        super(name, age, gender);
        this.id = id;
        this.salary = salary;
    }

    @Override
    public void play() {
        System.out.println("employee play");
    }

    public static String getCompany() {
        return company;
    }

    public static void setCompany(String company) {
        Employee.company = company;
    }

    public String getSalary() {
        return salary;
    }

    public void setSalary(String salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id='" + id + '\'' +
                ", salary='" + salary + '\'' +
                ", company='" + Employee.getCompany() + "\', " +
                super.toString()+
                '}';
    }
}
package ch06_homework4_4;

public class Test {
    public static void main(String[] args) {
        Employee.setCompany("abc");
        Employee e1 = new Employee("Tom", "12", "male", "1", "0");
        Employee e2 = new Employee("Jerry", "12", "male", "2", "0");

        System.out.println(e1.toString());
        System.out.println(e2.toString());

        e1.play();
        e2.sing();

    }

}

 

相关标签: 实训 实训