【Java】设计动物类Animal
题⽬:
设计动物类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(){
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(){
protected void eating(String Food){
System.out.println(Name+"啄"+Food);
}
void flying(){
System.out.println("鸡可以飞");
java的tostring方法
}
}
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我到最后也没能弄明⽩。以及空的构造⽅法,在这⾥被坑了很多次,⾄少这个问题在最后(算是)解决了。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。