• 吴裕雄--天生自然 JAVA开发学习:多态


    Parent p = new Child();
    public class Test {
        public static void main(String[] args) {
          show(new Cat());  // 以 Cat 对象调用 show 方法
          show(new Dog());  // 以 Dog 对象调用 show 方法
                    
          Animal a = new Cat();  // 向上转型  
          a.eat();               // 调用的是 Cat 的 eat
          Cat c = (Cat)a;        // 向下转型  
          c.work();        // 调用的是 Cat 的 work
      }  
                
        public static void show(Animal a)  {
          a.eat();  
            // 类型判断
            if (a instanceof Cat)  {  // 猫做的事情 
                Cat c = (Cat)a;  
                c.work();  
            } else if (a instanceof Dog) { // 狗做的事情 
                Dog c = (Dog)a;  
                c.work();  
            }  
        }  
    }
     
    abstract class Animal {  
        abstract void eat();  
    }  
      
    class Cat extends Animal {  
        public void eat() {  
            System.out.println("吃鱼");  
        }  
        public void work() {  
            System.out.println("抓老鼠");  
        }  
    }  
      
    class Dog extends Animal {  
        public void eat() {  
            System.out.println("吃骨头");  
        }  
        public void work() {  
            System.out.println("看家");  
        }  
    }
    public class Employee {
       private String name;
       private String address;
       private int number;
       public Employee(String name, String address, int number) {
          System.out.println("Employee 构造函数");
          this.name = name;
          this.address = address;
          this.number = number;
       }
       public void mailCheck() {
          System.out.println("邮寄支票给: " + this.name
           + " " + this.address);
       }
       public String toString() {
          return name + " " + address + " " + number;
       }
       public String getName() {
          return name;
       }
       public String getAddress() {
          return address;
       }
       public void setAddress(String newAddress) {
          address = newAddress;
       }
       public int getNumber() {
         return number;
       }
    }
    public class Salary extends Employee
    {
       private double salary; // 全年工资
       public Salary(String name, String address, int number, double salary) {
           super(name, address, number);
           setSalary(salary);
       }
       public void mailCheck() {
           System.out.println("Salary 类的 mailCheck 方法 ");
           System.out.println("邮寄支票给:" + getName()
           + " ,工资为:" + salary);
       }
       public double getSalary() {
           return salary;
       }
       public void setSalary(double newSalary) {
           if(newSalary >= 0.0) {
              salary = newSalary;
           }
       }
       public double computePay() {
          System.out.println("计算工资,付给:" + getName());
          return salary/52;
       }
    }
    public class VirtualDemo {
       public static void main(String [] args) {
          Salary s = new Salary("员工 A", "北京", 3, 3600.00);
          Employee e = new Salary("员工 B", "上海", 2, 2400.00);
          System.out.println("使用 Salary 的引用调用 mailCheck -- ");
          s.mailCheck();
          System.out.println("
    使用 Employee 的引用调用 mailCheck--");
          e.mailCheck();
        }
    }
  • 相关阅读:
    openstack trove实例状态转换条件--Mitaka版本
    trove module使用说明
    openstack trove mongodb配置项
    openstack trove weekly meeting时间即将更改
    openstack trove redis配置项
    trove database功能总结
    openstack trove 数据库镜像构建列表
    openstack trove主要贡献公司-Tesora被Stratoscale收购
    openstack trove,使pylint忽略错误
    十件你需要知道的事,关于openstack-trove(翻译)
  • 原文地址:https://www.cnblogs.com/tszr/p/10966624.html
Copyright © 2020-2023  润新知