问题
What is the difference between these two ways of instantiating new objects of a class as follows:
Test t1=new Test();
Test t2=new Test(){ };
When I tried the following code, I could see that both objects could access the method foo()
, but t2 cannot access the variable x
(variable x
cannot be resolved):
public class Test
{
int x=0;
public void foo(){ }
public static void main (String args[])
{
Test t1=new Test();
Test t2=new Test(){ };
t1.x=10;
t2.x=20;
t1.foo();
t2.foo();
System.out.println(t1.x+" "t2.x);
}
}
回答1:
Test t2=new Test();
will create the object of Test class.
But Test t2=new Test(){ };
will create a object of subclass of test (i.e. anonymous inner class in this case).
you can provide implementation for any method over there like
Test t2=new Test(){
public void foo(){ System.out.println("This is foo");}
};
so that when foo()
method called from object t2
it will print This is foo
.
Addition
Compile time error in your code is due to missing concatination operator
System.out.println(t1.x+" "+t2.x);
###
回答2:
The runtime type of both the references would be different. Try:
System.out.println(t1.getClass()); // class Test
System.out.println(t2.getClass()); // class Test$1
You will see different output. Reason being, the new Test() { }
expression creates instance of an anonymous subclass of Test
. So, Test$1
is a subclass of Test
.
Now, the reason you're getting that error is, you're missing a +
sign:
System.out.println(t1.x + " " + t2.x);
^
You can find more details on this post, and this post
回答3:
test t1=new test();
This will create a new instance of class test
test t2=new test(){ };
This is an anonymous inner class which extends class test
回答4:
Test t1=new Test();
Here, you are creating an instance of Test
class and assigned it to t1
Test t2=new Test(){ };
Here, You have created an anonymous sub class of Test
and instantiate it and assigned to t2
And, you did a mistake here in the following line, corrected it, you missed +
System.out.println(t1.x + " " + t2.x);
回答5:
a)
Test t1=new Test();
By doing this you create an Object of class Test by calling default constructor
b)
Test t2=new Test(){ };
And by doing this you are creating an object of a class that extends Test class, this class has no name and hence it is called "Anonymous Inner Class" eg.
Test t2=new Test(){
// this is the body of the anonymous(un-named) class
//you can overide the method foo() here
// you can write more methods here but you will not be able to call them
// for example
public void doSomething(){}
};
doSomething() is not accessible outside, as t2 i.e reference(pointer) to this object(object of anonymous inner class that extends Test) understands only foo() method as it is reference of parent class
doSomething() can be called only if you do this
Test t2= new Test(){
public void foo()
{
doSomething();
}
public void doSomething(){
System.out.println("Do Something");
}
};
i.e. explicitly call doSomething() in foo() and foo() is accessible outside
t2.foo();
Note: Please write name of class properly, the first letter of class must be capital like
public class Test{}
When you start writing huge chunks of code it will help you and others as it makes your code Readable.
回答6:
you miss the + operator in below line try this
System.out.println(t1.x+" "t2.x);
use this
System.out.println(t1.x+" "+t2.x);
回答7:
Test t2=new Test();`
will create the object of Test class.
Test t2=new Test(){ };
will create a object of subclass of test (i.e. anonymous inner class in this case).
Test t2=new Test(){
public void foo(){ System.out.println("foo");}
};
when foo() method called from object t2, it will print foo.
来源:https://stackoverflow.com/questions/22164036/difference-between-new-test-and-new-test