继承,重写,super,toString

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值