第四章


创建抽象类Animal ,创建Fish、brid类

Animal:
package com.bdqn.extend;
public abstract class Animal {
 private int age;
 
 public int getAge() {
  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+"岁了!");
 }
}





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+"斤重的鱼!");
 }
}




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+"的鸟!");
 }
 
}



测试类:
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();
  
 }
}







兜兜家养了一只鸡、一只鸭
家禽类:
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);
  
 }
}





鸡类:
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+"!");
 }
}






鸭类:
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+"!");
 }
}







测试类:
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();
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值