1.
package page2_123;
public abstract class Animal {
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public abstract void info();
public Animal(int age) {
this.age = age;
}
}
package page2_123;
public class Bird extends Animal {
private String color="红色";
public Bird(int age) {
super(age);
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void info() {
System.out.print("我是一只"+getColor()+"的鸟!"+"\n今年"+this.getAge()+"岁了!");
}
}
package page2_123;
public class Fish extends Animal{
private int weight=5;
public Fish(int age) {
super(age);
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
public void info() {
System.out.println("\n我是一只"+this.weight+"重的鱼!"+"\n今年"+this.getAge()+"岁了!");
}
}
package page2_123;
public class Test {
public static void main(String[] args) {
Bird bird=new Bird(4);
bird.info();
System.out.println();
Fish fish=new Fish(2);
fish.info();
}
}
2.
package page2_123;
public abstract class Animal2 {
private String name;
private String likeeat;
private String dosomething;
private String strain;
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
public String getDosomething() {
return dosomething;
}
public void setDosomething(String dosomething) {
this.dosomething = dosomething;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLikeeat() {
return likeeat;
}
public void setLikeeat(String likeeat) {
this.likeeat = likeeat;
}
public Animal2(String name,String likeeat,String dosomething,String strain) {
this.name=name;
this.likeeat=likeeat;
this.dosomething=dosomething;
this.strain=strain;
}
}
package page2_123;
public class Chicken extends Animal2{
public Chicken (String name,String likeeat,String dosomething,String strain) {
super(name,likeeat,dosomething,strain);
}
public void show() {
System.out.println("我叫"+this.getName()+","+"是一只"+this.getStrain()+"!");
System.out.println("我喜欢"+this.getLikeeat()+"!");
System.out.println("我会"+this.getDosomething()+"!");
}
}
package page2_123;
public class Duck extends Animal2 {
public Duck (String name,String likeeat,String dosomething,String strain) {
super(name,likeeat,dosomething,strain);
}
public void show() {
System.out.println("我叫"+this.getName()+","+"是一只"+this.getStrain()+"!");
System.out.println("我喜欢"+this.getLikeeat()+"!");
System.out.println("我会"+this.getDosomething()+"!");
}
}
package page2_123;
public class TestAnimal2 {
public static void main(String[] args) {
Chicken chicken=new Chicken("喔喔","虫子","打鸣","芦花鸡");
chicken.show();
System.out.println();
Duck duck=new Duck("嘎嘎","小鱼虾","游泳","斑嘴鸭");
duck.show();
}
}