面向对象第四章课后作业
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();
}
}
面向对象第四章课后作业
最新推荐文章于 2021-12-02 18:00:00 发布