返回列表 發帖

繼承 (一)

本帖最後由 tonyh 於 2019-9-2 18:29 編輯

所謂繼承(Inheritance)是一種類別之間的關係,可以利用現有類別衍生出新的類別,新的類別可以與現有類別分享共同的屬性、方法與資料結構等,在物件導向程式設計中利用繼承,可以達到程式碼重複使用的優點。

繼承功能可以在建立新類別時,修改、添加或繼承現有類別的定義。建立一種類別後,如果須要再建立許多大同小異的類別,就可以利用繼承特性,分別繼承這個現有的類別,並且將差異部分加以修改或添加。

被繼承的類別稱為父類別或基礎類別(Base Class),而經由繼承產生的類別,則稱為子類別或衍生類別(Derived Class),子類別不僅可以繼承父類別的特性(屬性與方法),也可以修改或添加特性。

C++支援多重繼承(一個類別擁有多個父類別),但Java不直接支援多重繼承,類似的機制則必須透過介面來完成。

歸納上述,關於繼承的四大特性如下:

1.子類別可以繼承父類別的特性。
2.子類別可以添加新的特性。
3.子類別可以修改並重新定義自父類別繼承下來的特性。
4.子類別繼承父類別時,不需要複製父類別的程式碼,造成程式碼重複。




  1. public class Ch68 {

  2.         public static void main(String[] args) {
  3.             Dog d1=new Dog("憨憨",2,1.28);
  4.             Dog d2=new Dog("球球",1,1.35);
  5.             Cat c1=new Cat("咪咪",3,0.95);
  6.             d1.showProfile();
  7.             d2.showProfile();
  8.             c1.showProfile();
  9.         }

  10. }

  11. class Animal{
  12.        
  13.         String name;
  14.         int age;
  15.         double weight;
  16.        
  17.         Animal(String n, int a, double w)
  18.         {
  19.                 name=n;
  20.                 age=a;
  21.                 weight=w;
  22.         }
  23.        
  24.         void showProfile()
  25.         {
  26.                 System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.");
  27.         }       
  28. }

  29. class Dog extends Animal
  30. {
  31.         Dog(String n, int a, double w)
  32.         {
  33.                 super(n,a,w);
  34.         }
  35. }

  36. class Cat extends Animal
  37. {
  38.         Cat(String n, int a, double w)
  39.         {
  40.                 super(n,a,w);
  41.         }
  42. }
複製代碼

  1. public class Ch68 {

  2.         public static void main(String[] args) {
  3.                 Dog d1=new Dog("憨憨",2,1.28);
  4.                 Dog d2=new Dog("球球",1,1.35);
  5.                 Cat c1=new Cat("咪咪",3,0.95);
  6.                 d1.showProfile();
  7.                 d2.showProfile();
  8.                 c1.showProfile();
  9.         }
  10.        
  11. }

  12. class Animal
  13. {
  14.         String name;
  15.         int age;
  16.         double weight;
  17.         Animal(String n, int a, double w)
  18.         {
  19.                 name=n;
  20.                 age=a;
  21.                 weight=w;
  22.         }
  23.         void showProfile()
  24.         {
  25.                 System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.");
  26.         }
  27. }

  28. class Dog extends Animal
  29. {
  30.         Dog(String n, int a, double w)
  31.         {
  32.                 super(n,a,w);
  33.         }
  34. }

  35. class Cat extends Animal
  36. {
  37.         Cat(String n, int a, double w)
  38.         {
  39.                 super(n,a,w);
  40.         }
  41. }
複製代碼

TOP

  1. public class Ch68 {
  2.         public static void main(String[] args) {
  3.                 Dog dd=new Dog("阿肥",1,1.28);
  4.                 Dog db=new Dog("皮球",2,1.35);
  5.                 Cat cc=new Cat("骷髏",3,0.95);
  6.                 dd.show();
  7.                 db.show();
  8.                 cc.show();
  9.         }
  10. }
  11. class Animal{
  12.         String name;
  13.         int age;
  14.         double weight;
  15.         Animal(String n,int a,double w){
  16.                 name=n;
  17.                 age=a;
  18.                 weight=w;
  19.         }
  20.     void show(){
  21.             System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.");
  22.     }       
  23. }
  24. class Dog extends Animal{
  25.         Dog(String n,int a,double w){
  26.                 super(n,a,w);
  27.         }
  28. }
  29. class Cat extends Animal{
  30.         Cat(String n,int a,double w){
  31.                 super(n,a,w);
  32.         }
  33. }
複製代碼

TOP

  1. public class Ch68 {
  2.         public static void main(String[] args) {
  3.                 Dog d1=new Dog("旺旺",8,45);
  4.                 Dog d2=new Dog("球球",9,40);
  5.                 Cat c1=new Cat("咪咪",2,10);
  6.                 d1.viewProfile();
  7.                 d2.viewProfile();
  8.                 c1.viewProfile();
  9.         }

  10. }

  11. class Animal{
  12.         int age;
  13.         String name;
  14.         double weight;
  15.         Animal(String n,int a, int w){
  16.                 name=n;
  17.                 age=a;
  18.                 weight=w;
  19.         }
  20.         public void viewProfile(){
  21.                 System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.");
  22.         }
  23. }

  24. class Dog extends Animal{
  25.         Dog(String n,int a, int w){
  26.                 super(n,a,w);
  27.         }
  28. }
  29. class Cat extends Animal{
  30.         Cat(String n,int a, int w){
  31.                 super(n,a,w);
  32.         }
  33. }
複製代碼
ABCDEFGHIJKLMNOPQRSTUVWXYZ

TOP

  1. public class Ch68 {

  2.         public static void main(String[] args) {
  3.             Dog d1=new Dog("憨憨",2,1.28);
  4.             Dog d2=new Dog("球球",1,1.35);
  5.             Cat c1=new Cat("咪咪",3,0.95);
  6.             d1.showProfile();
  7.             d2.showProfile();
  8.             c1.showProfile();
  9.         }

  10. }

  11. class Animal{
  12.         
  13.         String name;
  14.         int age;
  15.         double weight;
  16.         
  17.         Animal(String n, int a, double w)
  18.         {
  19.                 name=n;
  20.                 age=a;
  21.                 weight=w;
  22.         }
  23.         
  24.         void showProfile()
  25.         {
  26.                 System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.");
  27.         }        
  28. }

  29. class Dog extends Animal
  30. {
  31.         Dog(String n, int a, double w)
  32.         {
  33.                 super(n,a,w);
  34.         }
  35. }

  36. class Cat extends Animal
  37. {
  38.         Cat(String n, int a, double w)
  39.         {
  40.                 super(n,a,w);
  41.         }
  42. }
複製代碼

TOP

  1. public class Ch68 {

  2.         public static void main(String[] args) {
  3.             Dog d1=new Dog("憨憨",2,1.28);
  4.             Dog d2=new Dog("球球",1,1.35);
  5.             Cat c1=new Cat("咪咪",3,0.95);
  6.             d1.showProfile();
  7.             d2.showProfile();
  8.             c1.showProfile();
  9.         }

  10. }

  11. class Animal{
  12.         
  13.         String name;
  14.         int age;
  15.         double weight;
  16.         
  17.         Animal(String n, int a, double w)
  18.         {
  19.                 name=n;
  20.                 age=a;
  21.                 weight=w;
  22.         }
  23.         
  24.         void showProfile()
  25.         {
  26.                 System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.");
  27.         }        
  28. }

  29. class Dog extends Animal
  30. {
  31.         Dog(String n, int a, double w)
  32.         {
  33.                 super(n,a,w);
  34.         }
  35. }

  36. class Cat extends Animal
  37. {
  38.         Cat(String n, int a, double w)
  39.         {
  40.                 super(n,a,w);
  41.         }
  42. }
複製代碼
EEEEEEEEEEEEELLLLLLLLLLLLLIIIIIIIIIIIII

TOP

  1. public class Ch68 {

  2.         public static void main(String[] args) {
  3.             Dog d1=new Dog("憨憨",2,1.28);
  4.             Dog d2=new Dog("球球",1,1.35);
  5.             Cat c1=new Cat("咪咪",3,0.95);
  6.             d1.showProfile();
  7.             d2.showProfile();
  8.             c1.showProfile();
  9.         }

  10. }

  11. class Animal{
  12.         
  13.         String name;
  14.         int age;
  15.         double weight;
  16.         
  17.         Animal(String n, int a, double w)
  18.         {
  19.                 name=n;
  20.                 age=a;
  21.                 weight=w;
  22.         }
  23.         
  24.         void showProfile()
  25.         {
  26.                 System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.");
  27.         }        
  28. }

  29. class Dog extends Animal
  30. {
  31.         Dog(String n, int a, double w)
  32.         {
  33.                 super(n,a,w);
  34.         }
  35. }

  36. class Cat extends Animal
  37. {
  38.         Cat(String n, int a, double w)
  39.         {
  40.                 super(n,a,w);
  41.         }
  42. }
複製代碼

TOP

返回列表