下面程序哪里錯(cuò)呢?(小白一個(gè),別介)
?public class Animal { public int age; public String name; public void eat(){ System.out.println("動(dòng)物具有吃東西的能力"); } public class Dog extends Animal{ } public static void main(String[] args) { Dog dog=new Dog(); dog.age=10; dog.name="xiaotian"; dog.eat(); } }
2015-12-14
你寫的這個(gè)代碼讀起來很不方便。
2015-11-27
不要將子類定義在父類里面
?public class Animal {
? ? ? ? ?public int age;?
? ? ? ? ?public String name;?
? ? ? ? ?public void eat(){?
? ? ? ? ? ? System.out.println("動(dòng)物具有吃東西的能力");?
? ? ? ?}?
}
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();?
? ? ? ? ? ? ?}?
}
2015-11-09
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歲具有吃東西的能力