设计动物类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()表示鸡可以飞。
定义测试类,完成如下任务:
package test1; public class Animal { protected String name; protected int age; protected String sex; protected int legNum; protected double weight; Animal(){ }//空 public Animal(String name,int age,String sex,int legNum,double weight){ this.name = name; this.age = age; this.sex = sex; this.legNum = legNum; this.weight = weight; } protected String eating (String food) { return food; } public String toString() { return "name="+name+","+"age="+age+","+"sex="+sex+","+"legNum"+legNum+","+"weight="+weight; } public String getname() { return name; } public int getage() { return age; } public String getsex(){ return sex; } public int getleg() { return legNum; } public double geiweight() { return weight; } public void setname(String name) { this.name = name; } public void setweight(double weight) { this.weight = weight; } public void setsex(String sex) { this.sex = sex; } public void setage(int age) { this.age = age; } public void setleg(int legNum) { this.legNum = legNum; } }
package test1; import test1.Animal; public class Pig extends Animal{ public double length; public double height; public String color; Pig(){ } Pig(String name,int age,String sex,int legNum,double weight,double length,double height,String color){ this.name = name; this.age = age; this.sex = sex; this.legNum = legNum; this.weight = weight; this.length = length; this.height = height; this.color = color; } public String toString() { return "name="+name+","+"age="+age+","+"sex="+sex+","+"legNum"+legNum+","+"weight="+weight+"length="+length +","+"height="+height+","+"color="+color; } protected String eating (String food) { return "给"+name+"猪吃:"+food; } public String walking() { return name+"在行走"; } public String getcolor() { return color; } public double getheight() { return height; } public double getlength() { return length; } public void setcolor(String color) { this.color = color; } public void setlength(double length) { this.length = length; } public void setheight(double height) { this.height = height; } }
package test1; public class Chicken extends Animal { public String combColor; Chicken(){ } public Chicken(String name,int age,String sex,int legNum,double weight,String combColor){ this.name = name; this.age = age; this.sex = sex; this.legNum = legNum; this.weight = weight; this.combColor = combColor; } public String toString() { return "name="+name+","+"age="+age+","+"sex="+sex+","+"legNum"+legNum+","+"weight="+weight +","+"combColor="+combColor; } protected String eating (String food) { return "鸡啄食"+food; } public String flying() { return "鸡可以飞"; } }
package test1; public class test1 { public static void main(String args[]) { Pig pig = new Pig("peiqi",20,"女",2,200.7,100.5,120,"粉红色"); Chicken chicken = new Chicken("xhj",12,"雄性",2,3,"红色"); String p1 = pig.toString();//创建对象 String p2 = pig.eating("白菜"); String p3 = pig.walking(); System.out.println(p1); System.out.println(p2); System.out.println(p3); String c1 = chicken.toString();//创建对象 String c2 = chicken.eating("虫子"); String c3 = chicken.flying(); System.out.println(c1); System.out.println(c2); System.out.println(c3); } }