两段代码- public class OutClassTest {
- public static void main(String[] args) {
- Out o = new Out();
- Out.Inner i = o.getInner();
- o = i.getOut();
- o.print();
- }
- }
- class Out{
-
- void print(){
- System.out.println("hello");
- }
-
- Inner getInner(){
- return new Inner();
- }
-
- class Inner{
- Out getOut(){//生成对外部类对象的引用
- return new Out();
- }
- }
- }
复制代码- public class OutClassTest {
- public static void main(String[] args) {
- Out o = new Out();
- Out.Inner i = o.getInner();
- o = i.getOut();
- o.print();
- }
- }
- class Out{
-
- void print(){
- System.out.println("hello");
- }
-
- Inner getInner(){
- return new Inner();
- }
-
- class Inner{
- Out getOut(){//生成对外部类对象的引用
- return Out.this;
- }
- }
- }
复制代码 请问 new Out() 和 Out.this 那种才是引用外部对象的正确方式? 这两个有啥区别? |