2017.12.1T19_B2_4zuoye

package com.whxiong.work04;

public abstract class Animal {
 private int age;
 public Animal(int age){
  this.age=age;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public abstract void info();
}

****************************************


public final class Bird extends Animal {
 private String color;

 public Bird(int age, String color) {
  super(age);
  this.color = color;
 }

 public String getColor() {
  return color;
 }

 public void setColor(String color) {
  this.color = color;
 }

 public void info() {
  System.out.println("我是一只" + color + "的鸟! 今年" + this.getAge() + "岁了!");

 **************************************************

package com.whxiong.work04;

public final class Chicken extends Poultry {
 private String type;

 public Chicken(String intrest, String name, String eat, String type) {
  super(intrest, name, eat);
  this.type = type;
 }

 public String getType() {
  return type;
 }

 public void setType(String type) {
  this.type = type;
 }

 public void print() {
  System.out
    .println("我叫" + this.getName() + ",是一只" + this.type + "! 我喜欢吃"
      + this.getEat() + "! 我会" + this.getIntrest() + "!");

 }***********************************************

package com.whxiong.work04;

public final class Duck extends Poultry {
 private String type;

 public Duck(String intrest, String name, String eat, String type) {
  super(intrest, name, eat);
  this.type = type;
 }

 public String getType() {
  return type;
 }

 public void setType(String type) {
  this.type = type;
 }

 public void print() {
  System.out
    .println("我叫" + this.getName() + ",是一只" + this.type + "! 我喜欢吃"
      + this.getEat() + "! 我会" + this.getIntrest() + "!");

********************************************************

package com.whxiong.work04;

public final class Fish extends Animal {
 private double weight;

 public Fish(int age, double weight) {
  super(age);
  this.weight = weight;
 }

 public double getWeight() {
  return weight;
 }

 public void setWeight(double weight) {
  this.weight = weight;
 }

 public void info() {
  System.out.println("我是一只" + weight + "斤重的鱼! 今年" + this.getAge()
    + "岁了!");

 **

package com.whxiong.work04;

public abstract class Poultry { // poultry>>>>家禽
 private String intrest;
 private String name;
 private String eat;

 public Poultry(String intrest, String name, String eat) {
  this.intrest = intrest;
  this.name = name;
  this.eat = eat;
 }

 public abstract void print();

 public String getIntrest() {
  return intrest;
 }

 public void setIntrest(String intrest) {
  this.intrest = intrest;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getEat() {
  return eat;
 }

 public void setEat(String eat) {
  this.eat = eat;
 }
}

***********************************************

package com.whxiong.work04;

import java.util.Scanner;

public class Work04 {

 /**
  * @param args
  */
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  System.out.println("课后习题第五题");
  System.out.println("***********************");
  String color;
  int ageBrid, ageFish;
  double weight;
  System.out.print("请输入鸟的颜色:");
  color = input.next();
  System.out.print("请输入鸟的年龄:");
  ageBrid = input.nextInt();
  System.out.print("请输入鱼的重量:");
  weight = input.nextDouble();
  System.out.print("请输入鱼的鱼龄:");
  ageFish = input.nextInt();
  System.out.println();
  Bird bird = new Bird(ageBrid, color);
  bird.info();
  System.out.println();
  Fish fish = new Fish(ageFish, weight);
  fish.info();

  System.out.println();
  System.out.println("课后习题第六题");
  System.out.println("***********************");
  Chicken chicken = new Chicken("打鸣", "喔喔", "虫子", "芦花鸡");
  chicken.print();
  System.out.println();
  Duck duck = new Duck("游泳", "嘎嘎", "小鱼虾", "斑嘴鸭");
 

******************************************

原文地址:https://www.cnblogs.com/xiaoxiao1016/p/8021624.html