返回列表 發帖
  1. [code]
  2. public class Ch74 {

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

  14. }

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

  28. class Dog extends Animal{
  29.         String color;
  30.         Dog(String name,int age,Double weight,String color){
  31.                 super(name,age,weight);
  32.                 this.color=color;
  33.         }
  34.         void makeSound(int x){
  35.                 for(int i=0; i<x; i++)
  36.                         System.out.print("汪~");
  37.                 System.out.println();
  38.         }
  39.         void showProfile(){
  40.                 System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤,毛色為"+color);
  41.         }
  42. }

  43. class Cat extends Animal{
  44.         Cat(String name,int age,Double weight){
  45.                 super(name,age,weight);
  46.         }
  47.         void makeSound(int x){
  48.                 for(int i=0; i<x; i++)
  49.                         System.out.print("喵~");
  50.                 System.out.println();
  51.         }
  52. }
複製代碼
[/code]
كخخخخخخخخخخخخخ

TOP

返回列表