package txt1;
public class Animal {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Animal(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Animal() {
super();
this.name = "凯莎";
this.age = 13;
}
public void eat(){
System.out.println("吃饭");
}
public void sleep(){
System.out.println("睡觉");
}
@Override
public String toString() {
return "Animal [name=" + name + ", age=" + age + "]";
}
}
package txt1;
public class dog extends Animal {
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
/**
* @param name
* @param age
* @param color
*/
public dog(String name, int age, String color) {
super(name, age);
this.color = color;
}
/**
* @param name
* @param age
*/
public dog() {
super();
}
public void eat(){
System.out.println("狗吃肉");
}
public void sleep(){
System.out.println("狗窝着睡");
}
@Override
public String toString() {
return "dog [name=" + super.getName() + ", age= " + super.getAge() + " color=" + color + "]";
}
}
package txt1;
public class cat extends Animal{
private String zoulu;
public String getZoulu() {
return zoulu;
}
public void setZoulu(String zoulu) {
this.zoulu = zoulu;
}
/**
* @param name
* @param age
* @param zoulu
*/
public cat(String name, int age, String zoulu) {
super(name, age);
this.zoulu = zoulu;
}
public void ver(){
System.out.println("猫用"+this.zoulu+"条腿走路");
}
public void eat(){
System.out.println("猫吃鱼");
}
public void sleep(){
System.out.println("猫趴着睡");
}
@Override
public String toString() {
return "cat [name=" + super.getName() + ", age= "+super.getAge() +"cat [zoulu=" + zoulu + "]";
}
}
package txt1;
public class animalTest {
public static void main(String[] args) {
Animal a1=new Animal();
String s1=a1.toString();
a1.eat();
a1.sleep();
System.out.println(s1);
System.out.println("--------------------------");
Animal a2=new Animal("酷奇",55);
String s2=a2.toString();
System.out.println(s2);
System.out.println("--------------------------");
dog d1=new dog();
d1.eat();
d1.sleep();
String q1=d1.toString();
System.out.println(q1);
System.out.println("--------------------------");
dog d2=new dog("压力三大",41,"red");
d2.eat();
d2.sleep();
String q2=d2.toString();
System.out.println(q2);
System.out.println("--------------------------");
Animal c1=new cat("哈利",10,"4");
c1.eat();
c1.sleep();
String w1=c1.toString();
System.out.println(w1);
System.out.println("--------------------------");
cat c2=new cat("11",21,"3");
c2.ver();
String w2=c2.toString();
System.out.println(w2);
}
}