Wu Yuxiong - natural born JAVA development of learning: Polymorphism

Parent p = new Child();
public  class the Test {
     public  static  void main (String [] args) { 
      show ( new new Cat ());   // to show Cat object to invoke the method 
      show ( new new Dog ());   // to show Dog object method call 
                
      Animal A = new new Cat ();   // upcast   
      a.eat ();                // call is a Cat EAT 
      Cat C = (Cat) a;         // downward transition   
      c.work ();         // call is a Cat Work 
  }   
            
    public  static  void Show (Animal A) {
      a.eat ();   
        // type judging 
        IF (A the instanceof Cat) {   // cats do 
            Cat C = (Cat) A;   
            c.work ();   
        } the else  IF (A the instanceof Dog) { // dog doing things 
            Dog C = (Dog) a;   
            c.work ();   
        }   
    }   
} 
 
abstract  class Animal {  
     abstract  void EAT ();   
}   
  
class Cat the extends Animal {  
     public  voidEAT () {  
        System.out.println ( "fish" );   
    }   
    public  void Work () {   
        System.out.println ( "and mouse" );   
    }   
}   
  
class Dog the extends Animal {  
     public  void EAT () {   
        System.out.println ( "eat bones" );   
    }   
    public  void Work () {   
        System.out.println ( "housekeeping" );   
    }   
}
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 the Salary the extends the Employee 
{ 
   Private  Double the salary; // annual salary 
   public the Salary (String name, String address, int Number, Double the salary) {
        Super (name, address, Number); 
       setSalary (the salary); 
   } 
   public  void MailCheck ( ) { 
       System.out.println ( "salary MailCheck class method" ); 
       System.out.println ( "delivery checks to:" + getName ()
        + ", wage:" + the 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) { 
      the Salary S = new new the Salary ( "employee A", "Beijing",. 3, 3600.00 ); 
      the Employee E = new new the Salary ( "employee B", "Shanghai", 2, 2400.00 ); 
      System.out.println ( "Salary call reference using MailCheck -" ); 
      s.mailCheck (); 
      System.out.println ( "\ n-rEFERENCE call using Employee mailCheck--" ); 
      E .mailCheck (); 
    } 
}

 

Guess you like

Origin www.cnblogs.com/tszr/p/10966624.html