繼承

extends

這裏寫圖片描述

super

這裏寫圖片描述

子類構造器

這裏寫圖片描述

多態

public class Employee {

   private String name;
   private double salary;
   private Date hireDay;

   public Employee(String n, double s, int year, int month, int day)
   {
      name = n;
      salary = s;
      GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
      hireDay = calendar.getTime();
   }

   public String getName()
   {
      return name;
   }

   public double getSalary()
   {
      return salary;
   }

   public Date getHireDay()
   {
      return hireDay;
   }

   public void raiseSalary(double byPercent)
   {
      double raise = salary * byPercent / 100;
      salary += raise;
   }
}


public class Manager extends Employee {

   private double bonus;

   public Manager(String n, double s, int year, int month, int day)
   {
      super(n, s, year, month, day);
      bonus = 0;
   }

   public double getSalary()
   {
      double baseSalary = super.getSalary();
      return baseSalary + bonus;
   }

   public void setBonus(double b)
   {
      bonus = b;
   }
}


public class ManagerTest {

   public static void main(String[] args) {
      Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
      boss.setBonus(5000);

      Employee[] staff = new Employee[3];

      staff[0] = boss;
      staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
      staff[2] = new Employee("Tommy Tester", 40000, 1990, 3, 15);

      for (Employee e : staff)
         System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
   }
}

這裏寫圖片描述

繼承層次
這裏寫圖片描述

理解方法調用
這裏寫圖片描述
這裏寫圖片描述

經典的練習題

public class A {

    public String show(D obj) {
        return ("A and D");
    }

    public String show(A obj) {
        return ("A and A");
    }
}

public class B extends A{

    public String show(B obj) {
        return ("B and B");
    }

    public String show(A obj) {
        return ("B and A");
    }
}

public class C extends B{

}

public class D extends B{

}

public class Test {

    public static void main(String[] args) {
        A a1 = new A();
        A a2 = new B();
        B b = new B();
        C c = new C();
        D d = new D();

        System.out.println("1--" + a1.show(b)); //b向上轉型爲A 調用的是A類的show(A obj)  A and A
        System.out.println("2--" + a1.show(c)); //c向上轉型爲A 調用的是A類的show(A obj)  A and A
        System.out.println("3--" + a1.show(d)); //調用的是A類方法的show(D obj)          A and D

        System.out.println("4--" + a2.show(b)); //b向上轉型爲A 調用的是B類的show(A obj)  B and A 
        System.out.println("5--" + a2.show(c)); //b向上轉型爲A 調用的是B類的show(A obj)  B and A
        System.out.println("6--" + a2.show(d)); //調用的是A類方法的show(D obj)          A and D

        System.out.println("7--" + b.show(b)); //調用的是B類的方法show(B obj)           B and B
        System.out.println("8--" + b.show(c)); //調用的是B類的方法show(B obj)           B and B
        System.out.println("9--" + b.show(d)); //調用的是A類的方法show(D obj)           A and D

    }

}
    這裏a2是引用變量,爲A類型,它引用的是B對象,因此按照上面那句話的意思是說有B來決定調用誰的方法,所以a2.show(b)應該要調用B中
的show(B obj),產生的結果應該是“B and B”,但是爲什麼會與前面的運行結果產生差異呢?這裏我們忽略了後面那句話“但是這兒被調
用的方法必須是在超類中定義過的”,那麼show(B obj)在A類中存在嗎?根本就不存在!所以這句話在這裏不適用?那麼難道是這句話錯誤了?
非也!其實這句話還隱含這這句話:它仍然要按照繼承鏈中調用方法的優先級來確認。所以它纔會在A類中找到show(A obj),同時由於B重寫
了該方法所以纔會調用B類中的方法,否則就會調用A類中的方法。

    所以多態機制遵循的原則概括爲:當超類對象引用變量引用子類對象時,被引用對象的類型而不是引用變量的類型決定了調用誰的成員方法,但是這
個被調用的方法必須是在超類中定義過的,也就是說被子類覆蓋的方法,但是它仍然要根據繼承鏈中方法調用的優先級來確認方法,該優先級爲:
this.show(O)、super.show(O)、this.show((super)O)、super.show((super)O)。

強制類型轉換
這裏寫圖片描述

抽象類

在使用抽象類時需要注意幾點:
1、抽象類不能被實例化,實例化的工作應該交由它的子類來完成,它只需要有一個引用即可。
2、抽象方法必須由子類來進行重寫。
3、只要包含一個抽象方法的抽象類,該方法必須要定義成抽象類,不管是否還包含有其他方法。
4、抽象類中可以包含具體的方法,當然也可以不包含抽象方法。
5、子類中的抽象方法不能與父類的抽象方法同名。
6、abstract不能與final並列修飾同一個類。
7、abstract 不能與private、static、final或native並列修飾同一個方法。

這裏寫圖片描述

public abstract class Person {

   public abstract String getDescription();
   private String name;

   public Person(String n)
   {
      name = n;
   }

   public String getName()
   {
      return name;
   }
}

public class Employee extends Person {

   private double salary;
   private Date hireDay;

   public Employee(String n, double s, int year, int month, int day)
   {
      super(n);
      salary = s;
      GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
      hireDay = calendar.getTime();
   }

   public double getSalary()
   {
      return salary;
   }

   public Date getHireDay()
   {
      return hireDay;
   }

   public String getDescription()
   {
      return String.format("an employee with a salary of $%.2f", salary);
   }

   public void raiseSalary(double byPercent)
   {
      double raise = salary * byPercent / 100;
      salary += raise;
   }
}

public class Student extends Person {

   private String major;

   public Student(String n, String m)
   {
      // pass n to superclass constructor
      super(n);
      major = m;
   }

   public String getDescription()
   {
      return "a student majoring in " + major;
   }

public class PersonTest {

   public static void main(String[] args) {
      Person[] people = new Person[2];

      // fill the people array with Student and Employee objects
      people[0] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
      people[1] = new Student("Maria Morris", "computer science");

      // print out names and descriptions of all Person objects
      for (Person p : people)
         System.out.println(p.getName() + ", " + p.getDescription());
   }
}

這裏寫圖片描述

受保護的訪問
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章