guangnian
2015-11-07 11:23
public class Animal { public int age; public String name; public void eat(){ System.out.println("动物具有吃东西的能力"); } public class Dog extends Animal{ } public static void main(String[] args) { Dog dog=new Dog(); dog.age=10; dog.name="xiaotian"; dog.eat(); } }
你写的这个代码读起来很不方便。
不要将子类定义在父类里面
public class Animal {
public int age;
public String name;
public void eat(){
System.out.println("动物具有吃东西的能力");
}
}
public class Dog extends Animal{ }
public class text{
public static void main(String[] args)
{
Dog dog=new Dog();
dog.age=10; dog.name="xiaotian";
dog.eat();
}
}
public void eat(name,age){ System.out.println(name+age+"岁具有吃东西的能力"); }
public static void main(String[] args) { Dog dog=new Dog(); dog.age=10; dog.name="xiaotian"; dog.eat(dog.name,dog.age); } }
或者
import ……Dog;
public class Animal { public void eat(Dog dog){ System.out.println(dog.name+dog.age+"岁具有吃东西的能力"); } public class Dog extends Animal{ } public static void main(String[] args) { Dog dog=new Dog(); dog.age=10; dog.name="xiaotian"; dog.eat(dog); } }
输出:xiaotian10岁具有吃东西的能力
Java入门第二季 升级版
530652 学习 · 6091 问题
相似问题