创建抽象类Animal ,创建Fish、brid类
Animal:
package com.bdqn.extend;
public abstract class Animal {
private int age;
private int age;
public int getAge() {
return age;
}
return age;
}
public void setAge(int age) {
this.age = age;
}
public Animal() {
this.age=3;
}
public Animal(int age) {
this.age=age;
}
public void info() {
System.out.println("今年"+this.age+"岁了!");
}
}
this.age = age;
}
public Animal() {
this.age=3;
}
public Animal(int age) {
this.age=age;
}
public void info() {
System.out.println("今年"+this.age+"岁了!");
}
}
Fish:
package com.bdqn.extend;
public class Fish extends Animal{
private int weight;
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public Fish(int weight,int age) {
super(age);
this.weight=weight;
show();
}
public void show() {
System.out.println("我是一只"+this.weight+"斤重的鱼!");
}
}
private int weight;
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public Fish(int weight,int age) {
super(age);
this.weight=weight;
show();
}
public void show() {
System.out.println("我是一只"+this.weight+"斤重的鱼!");
}
}
brid:
package com.bdqn.extend;
public class Brid extends Animal{
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Brid(String color,int age) {
super(age);
this.color=color;
show();
}
public void show() {
System.out.println("我是一只"+color+"的鸟!");
}
}
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Brid(String color,int age) {
super(age);
this.color=color;
show();
}
public void show() {
System.out.println("我是一只"+color+"的鸟!");
}
}
测试类:
package com.bdqn.extend;
public class AnimalTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Animal a=new Brid("红色", 4);
a.info();
System.out.println();
Animal as=new Fish(5, 2);
as.info();
}
// TODO Auto-generated method stub
Animal a=new Brid("红色", 4);
a.info();
System.out.println();
Animal as=new Fish(5, 2);
as.info();
}
}
兜兜家养了一只鸡、一只鸭
家禽类:
package com.bdqn.Animal;
public abstract class Poultry {
private String name;
private String type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Poultry() {
}
public Poultry(String name,String type) {
this.name=name;
this.type=type;
}
public void print() {
System.out.println("我叫"+this.name+",是一只"+this.type);
}
}
private String name;
private String type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Poultry() {
}
public Poultry(String name,String type) {
this.name=name;
this.type=type;
}
public void print() {
System.out.println("我叫"+this.name+",是一只"+this.type);
}
}
鸡类:
package com.bdqn.Animal;
public class Chicken extends Poultry {
private String eat;
private String love;
public String getEat() {
return eat;
}
public void setEat(String eat) {
this.eat = eat;
}
public String getLove() {
return love;
}
public void setLove(String love) {
this.love = love;
}
public Chicken(String name,String type,String eat,String love) {
super(name, type);
this.eat=eat;
this.love=love;
}
public void show() {
super.print();
System.out.println("我喜欢吃"+this.eat+"!");
System.out.println("我会"+this.love+"!");
}
}
private String eat;
private String love;
public String getEat() {
return eat;
}
public void setEat(String eat) {
this.eat = eat;
}
public String getLove() {
return love;
}
public void setLove(String love) {
this.love = love;
}
public Chicken(String name,String type,String eat,String love) {
super(name, type);
this.eat=eat;
this.love=love;
}
public void show() {
super.print();
System.out.println("我喜欢吃"+this.eat+"!");
System.out.println("我会"+this.love+"!");
}
}
鸭类:
package com.bdqn.Animal;
public class Duck extends Poultry{
private String eat;
private String love;
public String getEat() {
return eat;
}
public void setEat(String eat) {
this.eat = eat;
}
public String getLove() {
return love;
}
public void setLove(String love) {
this.love = love;
}
public Duck(String name,String type,String eat,String love) {
super(name, type);
this.eat=eat;
this.love=love;
}
public void show() {
super.print();
System.out.println("我喜欢吃"+this.eat+"!");
System.out.println("我会"+this.love+"!");
}
}
private String eat;
private String love;
public String getEat() {
return eat;
}
public void setEat(String eat) {
this.eat = eat;
}
public String getLove() {
return love;
}
public void setLove(String love) {
this.love = love;
}
public Duck(String name,String type,String eat,String love) {
super(name, type);
this.eat=eat;
this.love=love;
}
public void show() {
super.print();
System.out.println("我喜欢吃"+this.eat+"!");
System.out.println("我会"+this.love+"!");
}
}
测试类:
package com.bdqn.Animal;
public class PoultryTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Chicken a=new Chicken("喔喔", "芦花鸡","虫子", "打鸣" );
a.show();
System.out.println();
Duck d=new Duck( "嘎嘎", "斑嘴鸭","小鱼虾", "游泳");
d.show();
}
// TODO Auto-generated method stub
Chicken a=new Chicken("喔喔", "芦花鸡","虫子", "打鸣" );
a.show();
System.out.println();
Duck d=new Duck( "嘎嘎", "斑嘴鸭","小鱼虾", "游泳");
d.show();
}
}