面向对象第四章课后作业

				面向对象第四章课后作业
1设计bird,fish类,都继承自抽象类animal,实现其抽象方法info()。
package com.excellence.action;

public abstract class Animal {
	String color;
	String weight;
	int age;

	public abstract void printinfo();

}
package com.excellence.action;

public class Bird extends Animal {

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

	@Override
	public void printinfo() {
		System.out.println("我是一只" + color + "的鸟!");
		System.out.println("今年" + age + "岁了!");

	}

}
package com.excellence.action;

public class Fish extends Animal {
	Fish(String weight, int age) {
		super.weight = weight;
		super.age = age;
	}

	@Override
	public void printinfo() {

		System.out.println("我是一只" + weight + "重的鱼!");
		System.out.println("今年" + age + "岁了!");

	}

}
package com.excellence.action;

public class testanimal {
	public static void main(String[] args) {
		Bird bird = new Bird("红色", 4);
		Fish fish = new Fish("5斤", 2);
		bird.printinfo();
		fish.printinfo();
	}

}

2.兜兜家养了两只家禽,一只鸡和一只鸭。请用面向对象的封装,继承的特性进行描述。
package com.excellence.action;

public class Poultry {
	private String name;
	private String pouname;
	private String eat;

	public String getPouname() {
		return pouname;
	}

	public void setPouname(String pouname) {
		this.pouname = pouname;
	}

	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;
	}

	public void print() {
		System.out.println("我叫" + name + ",是一只" + pouname + "!");
		System.out.println("我喜欢吃" + eat + "!");
	}

}
package com.excellence.action;

public class Chicken extends Poultry {
	private String call;

	public String getCall() {
		return call;
	}

	public void setCall(String call) {
		this.call = call;
	}

	Chicken(String call) {
		super.setName("喔喔");
		super.setPouname("芦花鸡");
		super.setEat("虫子");
		this.call = call;
	}

	public void print() {
		super.print();
		System.out.println("我会" + call + "!");
	}

}
package com.excellence.action;

public class Duck extends Poultry {
	private String motion;

	private String getMotion() {
		return motion;
	}

	private void setMotion(String motion) {
		this.motion = motion;
	}

	Duck(String motion) {
		super.setName("嘎嘎");
		super.setPouname("斑嘴呀");
		super.setEat("小鱼虾");
		this.motion = motion;
	}

	public void print() {
		super.print();
		System.out.println("我会" + motion + "!");
	}

}
package com.excellence.action;

public class Testpoultry {
	public static void main(String[] args) {
		Chicken chicken = new Chicken("打鸣");
		Duck duck = new Duck("游泳");
		duck.print();
		chicken.print();
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值