返回列表 發帖

物件導向基礎概念 (三)

本帖最後由 tonyh 於 2019-1-28 20:30 編輯

定義一Dog類別, 包含建構子及兩個方法.
showProfile() 用來顯示基本資料, makeSound(int n) 用來發出聲音.

  1. public class Ch59
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         Dog d1=new Dog("憨憨",2,1.3,"紅棕色");
  6.         Dog d2=new Dog("球球",1,1.2,"白色");
  7.         d1.showProfile();
  8.         d1.makeSound(2);
  9.         d2.showProfile();
  10.         d2.makeSound(3);
  11.     }
  12. }
  13. class Dog
  14. {
  15.     String name, color;
  16.     int age;
  17.     double w;
  18.     Dog(String n, int a, double w, String c)
  19.     {
  20.         name=n;
  21.         age=a;
  22.         this.w=w;
  23.         color=c;
  24.     }
  25.     void showProfile()
  26.     {
  27.         System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
  28.     }
  29.     void makeSound(int n)
  30.     {
  31.         for(int i=1; i<=n; i++)
  32.             System.out.print("汪~");
  33.         System.out.println();
  34.     }
  35. }
複製代碼

  1. public class Ch59
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         Dog d1=new Dog("憨憨",2,1.3,"紅棕色");
  6.         Dog d2=new Dog("球球",1,1.2,"白色");
  7.         d1.showProfile();
  8.         d1.makeSound(2);
  9.         d2.showProfile();
  10.         d2.makeSound(3);
  11.     }
  12. }
  13. class Dog
  14. {
  15.     String name, color;
  16.     int age;
  17.     double w;
  18.     Dog(String n, int a, double w, String c)
  19.     {
  20.         name=n;
  21.         age=a;
  22.         this.w=w;
  23.         color=c;
  24.     }
  25.     void showProfile()
  26.     {
  27.         System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
  28.     }
  29.     void makeSound(int n)
  30.     {
  31.         for(int i=1; i<=n; i++)
  32.             System.out.print("汪~");
  33.         System.out.println();
  34.     }
  35. }
複製代碼

TOP

  1. public class Ch59
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         Dog d1=new Dog("憨憨",2,1.3,"紅棕色");
  6.         Dog d2=new Dog("球球",1,1.2,"白色");
  7.         d1.showProfile();
  8.         d1.makeSound(2);
  9.         d2.showProfile();
  10.         d2.makeSound(3);
  11.     }
  12. }
  13. class Dog
  14. {
  15.     String name, color;
  16.     int age;
  17.     double w;
  18.     Dog(String n, int a, double w, String c)
  19.     {
  20.         name=n;
  21.         age=a;
  22.         this.w=w;
  23.         color=c;
  24.     }
  25.     void showProfile()
  26.     {
  27.         System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
  28.     }
  29.     void makeSound(int n)
  30.     {
  31.         for(int i=1; i<=n; i++)
  32.             System.out.print("汪~");
  33.         System.out.println();
  34.     }
  35. }
複製代碼

TOP

  1. public class Ch59
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         Dog d1=new Dog("憨憨",2,1.3,"紅棕色");
  6.         Dog d2=new Dog("球球",1,1.2,"白色");
  7.         d1.showProfile();
  8.         d1.makeSound(2);
  9.         d2.showProfile();
  10.         d2.makeSound(3);
  11.     }
  12. }
  13. class Dog
  14. {
  15.     String name, color;
  16.     int age;
  17.     double w;
  18.     Dog(String n, int a, double w, String c)
  19.     {
  20.         name=n;
  21.         age=a;
  22.         this.w=w;
  23.         color=c;
  24.     }
  25.     void showProfile()
  26.     {
  27.         System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
  28.     }
  29.     void makeSound(int n)
  30.     {
  31.         for(int i=1; i<=n; i++)
  32.             System.out.print("汪~");
  33.         System.out.println();
  34.     }
  35. }
複製代碼

TOP

  1. public class Ch59
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         Dog d1=new Dog("憨憨",2,1.3,"紅棕色");
  6.         Dog d2=new Dog("球球",1,1.2,"白色");
  7.         d1.showProfile();
  8.         d1.makeSound(2);
  9.         d2.showProfile();
  10.         d2.makeSound(3);
  11.     }
  12. }
  13. class Dog
  14. {
  15.     String name, color;
  16.     int age;
  17.     double w;
  18.     Dog(String n, int a, double w, String c)
  19.     {
  20.         name=n;
  21.         age=a;
  22.         this.w=w;
  23.         color=c;
  24.     }
  25.     void showProfile()
  26.     {
  27.         System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
  28.     }
  29.     void makeSound(int n)
  30.     {
  31.         for(int i=1; i<=n; i++)
  32.             System.out.print("汪~");
  33.         System.out.println();
  34.     }
  35. }
複製代碼

TOP

  1. public class Ch59
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         Dog d1=new Dog("憨憨",2,1.3,"紅棕色");
  6.         Dog d2=new Dog("球球",1,1.2,"白色");
  7.         d1.showProfile();
  8.         d1.makeSound(2);
  9.         d2.showProfile();
  10.         d2.makeSound(3);
  11.     }
  12. }
  13. class Dog
  14. {
  15.     String name, color;
  16.     int age;
  17.     double w;
  18.     Dog(String n, int a, double w, String c)
  19.     {
  20.         name=n;
  21.         age=a;
  22.         this.w=w;
  23.         color=c;
  24.     }
  25.     void showProfile()
  26.     {
  27.         System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
  28.     }
  29.     void makeSound(int n)
  30.     {
  31.         for(int i=1; i<=n; i++)
  32.             System.out.print("汪~");
  33.         System.out.println();
  34.     }
  35. }
複製代碼

TOP

  1. public class Ch01
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         Dog d1=new Dog("安安",2,1.3,"紅棕色");
  6.         Dog d2=new Dog("球球",1,1.2,"白色");
  7.         d1.showProfile();
  8.         d1.makeSound(2);
  9.         d2.showProfile();
  10.         d2.makeSound(3);
  11.     }
  12. }
  13. class Dog
  14. {
  15.     String name, color;
  16.     int age;
  17.     double w;            
  18.     Dog(String n, int a, double w, String c)
  19.     {
  20.             name=n;
  21.             age=a;
  22.             this.w=w;
  23.             color=c;
  24.     }
  25.     void showProfile()
  26.     {
  27.             System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
  28.     }
  29.     void makeSound(int n)
  30.     {
  31.             for(int i=1; i<=n; i++)
  32.                     System.out.print("汪~");
  33.             System.out.println();
  34.     }
  35. }
複製代碼

TOP

返回列表