- package practice;
- public class practice01 {
- public static void main(String[] args) {
- Dog d1= new Dog("憨憨",2,1.28,"棕色");
- Dog d3=new Dog("球球",1,1.35,"白色");
- Cat d2= new Cat("咪咪",3,0.95);
- d1.show();
- d1.makeSound(2);
- d3.show();
- d3.makeSound(3);
- d2.show();
- d2.makeSound(5);
- }
- }
- class Function{
- String name;
- int age;
- double weight;
- Function(String a,int b,double c)
- {
- name=a;
- age=b;
- weight=c;
- }
- void show()
- {
- System.out.println(name+"今年"+age+"歲 "+weight+"公斤");
- }
- }
- class Dog extends Function
- {
- String color;
- Dog(String n,int a,double w,String c)
- {
- super(n,a,w);
- color=c;
- }
- void makeSound(int x)
- {
- for(int i=1; i<=x; i++)
- System.out.print("汪~");
- System.out.println();
- }
- void show()
- {
- System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤,毛色為"+color+".");
- }
- }
- class Cat extends Function
- {
- Cat(String n, int a, double w)
- {
- super(n,a,w);
- }
- void makeSound(int x)
- {
- for(int i=1; i<=x; i++)
- System.out.print("喵~");
- System.out.println();
- }
- }
複製代碼 |