[英]Changing outer class reference for an object of inner class
我想知道有什么方法可以更改與非靜態內部類關聯的外部類引用。 例如,在下面的代碼中,我可以將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;
}
}
提前致謝
否。每個內部類實例對其包含的外部類實例都有一個隱式引用。 如果要更改這些類之間的關系,最好從外部類中提取內部類,並使它們像普通類那樣顯式地相互依賴。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.