class Animal{ public String name ; Animal(String name){ this.name=name; } } class Cat{ public String furColour; Cat(String n,String c ){ name=n; furColour=c; } } class Dog{
public String eyesColour; Dog(String n,String c){ name=n; eyesColour=c; } } public class Test1{ public static void main(String args []){ Test test=new Test(); Animal a=new Animal("aName");
Test test=new Test(); 这个类是定义在其他文件中? 不过, Test test=new Test(); 这句代码表示创建类Test的一个对象test, 仅此而已. 在Java中, Test test;只是定义了一个对象的引用, 这时并没有生成类的对象, test = new Test(); 这句代码才真正的生成一个Test的对象.
这与C++不同, 在C++中, Test test;就已经生成了一个Test的对象test. 而Test *test = new Test();是为指针生成对象.