标题: 嗯,自己看 [打印本页] 作者: 云沾衣。 时间: 2015-9-5 00:07 标题: 嗯,自己看 class Animals{
//属性:姓名和年龄
private int age;
private String name;
//无参构造
public Animals(){}
//有参
public Animals(int age,String name){
this.age = age;
this.name = name;
}
//年龄
public void setAge(int a){
if(a>0 && a<30){
age = a;
}
else
System.out.println("你养了一只妖怪!");
}
public int getAge(){
return this.age;
}
//名纸
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
//跑步
public void run(){
System.out.println("每天边跑边卖萌V");
}
//吃
public void eat(String food){
System.out.println("吃完"+food+"感觉自己萌萌哒");
}
}
class Dog extends Animals{}
class Cat extends Animals{}
class AnimalsTest{
public static void main(String[] args){
Dog d = new Dog();
d.setAge(5);
d.setName("大白");
System.out.println(d.getName()+"今年"+d.getAge()+"岁了");
d.run();
d.eat("骨头");
System.out.println("_______________");
Cat c = new Cat();
c.setAge(3);
c.setName("小白");
System.out.println(c.getName()+"今年"+c.getAge()+"岁了");
c.run();
c.eat("鲨鱼");
}
}