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 –