简体   繁体   English

更改内部类对象的外部类引用

[英]Changing outer class reference for an object of inner class

I want to know is there any way I can change outer class reference associated with non-static inner class. 我想知道有什么方法可以更改与非静态内部类关联的外部类引用。 eg In below code can I change outer class reference of i to point to o and not o1 ? 例如,在下面的代码中,我可以将i的外部类引用更改为指向o而不是o1吗?

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class outer {
    public class inner {
    };
}
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here


        outer o1 = new outer(), o = new outer();
        outer.inner i = o1.new inner();
        //i.outer.this = o;
    }
}

Thanks in Advance 提前致谢

No. Every inner class instance has an implicit reference to its containing outer class instance. 否。每个内部类实例对其包含的外部类实例都有一个隐式引用。 If you want to change the relationships between these classes you might be better off extracting the inner from the outer class and having them depend on each other explicitly as normal classes do. 如果要更改这些类之间的关系,最好从外部类中提取内部类,并使它们像普通类那样显式地相互依赖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM