返回列表 發帖
  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.                 d1.viewProfile();
  10.         d1.makeSound(2);
  11.         d2.viewProfile();
  12.         d2.makeSound(3);
  13.         c1.viewProfile();
  14.         c1.makeSound(5);
  15.         }

  16. }

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

  30. class Dog extends Animal{
  31.         Dog(String n,int a, int w){
  32.                 super(n,a,w);
  33.         }
  34.         void makeSound(int x){
  35.                 for(int i=0; i<x; i++){
  36.                         System.out.print("汪~");
  37.                 }
  38.                 System.out.println("");
  39.         }
  40. }
  41. class Cat extends Animal{
  42.         Cat(String n,int a, int w){
  43.                 super(n,a,w);
  44.         }
  45.         void makeSound(int x){
  46.                 for(int i=0; i<x; i++){
  47.                         System.out.print("喵~");
  48.                 }
  49.                 System.out.println("");
  50.         }
  51. }
複製代碼
ABCDEFGHIJKLMNOPQRSTUVWXYZ

TOP

返回列表