题目:
设计动物类Animal,要求如下:
(1)protected的成员变量包括名称name、年龄age、性别sex、腿的数量legNum、体重weight;
(2)定义空构造方法,定义能够初始化所有成员变量的构造方法;
(3)setter和getter方法;
(4)功能方法包括:protected方法eating(String food);重写Object类的toString()方法返回Animal对象的所有成员变量。
Pig类继承了Animal,Pig类的要求如下:
(1)成员变量有长度length,高度height和颜色color;
(2)定义构造方法能够初始化所有成员变量;
(3)setter和getter方法;
(4)功能方法包括:重写toString()方法返回Pig对象的所有成员变量;重写eating(String food)方法,food只能是Pig可以吃的食物;定义成员方法walking()表示Pig可以行走。
Chicken类继承Animal,Chicken类的要求如下:
(1)成员变量有鸡冠颜色combColor;
(2)定义构造方法能够初始化所有成员变量;
(3)省略setter和getter方法;
(4)功能方法有:重写toString()方法返回Chicken对象的所有成员变量,重写eating(String food)方法输出吃的动作和食物,定义成员方法flying()表示鸡可以飞。
定义测试类,完成如下任务:
(1)创建猪对象佩奇peiqi,输出peiqi的基本信息,给peiqi喂白菜吃,peiqi在行走;
(2)创建鸡对象杏花鸡xhj,输出xhj的基信息,给xhj喂虫子,xhj在飞。
class Animal{
protected String Name;
protected int Age;
protected String Sex;
protected int LegNum;
protected int Weight;
Animal(String Name, int Age, String Sex, int LegNum, int Weight){
this.Name = Name;
this.Age = Age;
this.Sex = Sex;
this.LegNum = LegNum;
this.Weight = Weight;
};
void setName(String Name) {
this.Name = Name;
}
String getName() {
return Name;
}
void setAge(int Age) {
this.Age = Age;
}
int getAge() {
return Age;
}
void setSex(String Sex) {
this.Sex = Sex;
}
String getSex() {
return Sex;
}
void setLegNum(int LegNum) {
this.LegNum = LegNum;
}
int getLegNum() {
return LegNum;
}
void setWeight(int Weight){
this.Weight = Weight;
}
int getWeight() {
return Weight;
}
protected void Eating(String Food) {
System.out.println("给"+Name+"喂"+Food+"吃");
}
public String toString() {
return Name+","+Age+","+Sex+","+LegNum+","+Weight;
}
}
class Pig extends Animal{
int Length;
int Height;
String Color;
Pig(String Name,int Age,String Sex,int LegNum,int Weight,int Length,int Height,String Color) {
super(Name,Age,Sex,LegNum,Weight);
this.Length = Length;
this.Height = Height;
this.Color = Color;
}
void setLength(int Length) {
this.Length = Length;
}
void setHeight(int Height) {
this.Height = Height;
}
void steColor(String Color) {
this.Color = Color;
}
int getLength() {
return Length;
}
int getHeight() {
return Height;
}
String setColor() {
return Color;
}
protected void eating(String Food) {
System.out.println("给猪喂"+Food);
}
void walking() {
System.out.println(Name+"在行走");
}
}
class Chicken extends Animal{
String CombColor;
Chicken(String Name,int Age,String Sex,int LegNum,int Weight,String CombColor){
super(Name,Age,Sex,LegNum,Weight);
this.CombColor = CombColor;
}
void setCombColor(String CombColor) {
this.CombColor = CombColor;
}
String getCombColor() {
return CombColor;
}
protected void eating(String Food) {
System.out.println(Name+"啄"+Food);
}
void flying() {
System.out.println("鸡可以飞");
}
}
public class TextAnimal {
public static void main(String args[]) {
Animal peiqi;
peiqi = new Pig("peiqi",2,"母",4,50,50,50,"pink");
Animal xhj;
xhj = new Chicken("xhj",3,"公",2,1,"red");
System.out.println("猪姓名:"+peiqi.Name);
System.out.println("猪年龄"+peiqi.Age);
System.out.println("猪性别"+peiqi.Sex);
System.out.println("猪腿数"+peiqi.LegNum);
System.out.println("猪体重"+peiqi.Weight);
System.out.println("猪长"+((Pig)peiqi).Length);
System.out.println("猪宽"+((Pig)peiqi).Height);
System.out.println("猪颜色"+((Pig)peiqi).Color);
peiqi.Eating("白菜");
((Pig)peiqi).walking();
System.out.println("鸡姓名:"+xhj.Name);
System.out.println("鸡年龄"+xhj.Age);
System.out.println("鸡性别"+xhj.Sex);
System.out.println("鸡腿数"+xhj.LegNum);
System.out.println("鸡体重"+xhj.Weight);
System.out.println("鸡颜色"+((Chicken)xhj).CombColor);
peiqi.Eating("谷物");
((Chicken)xhj).flying();
}
}
其中遇到了一些问题,比如,题目要求的setter和getter我到最后也没能弄明白。以及空的构造方法,在这里被坑了很多次,至少这个问题在最后(算是)解决了。