0
注意:我發現有關受保護字段或某些類的成員的披露(獲取)值的多個信息,我的差異是它需要獲取實例化對象...Java:從對象靜態類使用鑄造披露受保護的值
你好,我想透露的信息與先前實例化靜態內部類(對象先前創建的)...
static public class Outer {
static class Inner {
protected int number = 0;
protected int times = 2;
public Inner(int number) {
this.number = number;
}
}
}
Outer.Inner oi = new Outer.Inner(8);
我想打印實例化類的價值,因爲我不知道怎麼被實例化(什麼理由通過了)。
怎麼樣?
我在想什麼......
class Another extends Outer.Inner {
Another(int j) {
super(j);
}
public void submit() {
System.out.println("the constructor argument was:" + number);
}
}
在其他類中,我自己的項目中,我使用「OI」,但我需要顯示其信息(我不想改變原始類)並找到創建對象的位置。
有沒有解決方案來完成這個?
Another a = (Another)oi;
a.submit(); // I want to print the original argument.
這是爲了說明一個複雜的類和toString(),我得到類似的例子:@ 277c68a5 –
你可以做這樣的事情,並得到你想要的(而不是什麼@ 277:): @Override public String toString(){ return「number = {」+ number +「}; Times = {」+ times +「}」; } 你可以閱讀有關重寫toString()在這裏:http://javarevisited.blogspot.com/2012/09/override-tostring-method-java-tips-example-code.html#ixzz4iaysj3wS –