如何在外部 class 内部创建对象成员后访问内部 class 对象成员
How to access inner class object members once it has been created inside outer class
想象一下下面提到的场景-
public class TestMain {
private int outerData = 30;
class InnerFirst{
private int data1 = 40;
public int getData() {
return data1;
}
public void setData(int data) {
this.data1 = data;
}
void msg(){
System.out.println("Inner data is : "+data1+" and outer data is : "+outerData);
}
}
class InnerSecond{
private int data2 = 40;
public int getData() {
return data2;
}
public void setData(int data) {
this.data2 = data;
}
void msg(){
System.out.println("Inner data is : "+data2+" and outer data is : "+outerData);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestMain newClass = new TestMain();
TestMain.InnerFirst newInnerClass = newClass.new InnerFirst();
newInnerClass.setData(50);
newInnerClass.msg();
}
}
我的意图是将 newClass 对象设置到会话中并在需要时访问它。但是我怎样才能访问 data1 (内部 class 实例变量)呢?
据我所知,newInnerClass 对象(InnerFirst 的对象)是在 newClass 实例(外部 class)中创建的。我的问题是,如何从外部 class 对象引用访问内部 class 实例成员?
您始终可以将这些子 class 的实例变量保留在您的父 class 中,并且当您创建子 class 的对象时,然后在您的子 [=15] 的构造函数中=]你可以初始化相同的class的实例变量。像这样:对于第二个 class 你可以自己做同样的事情:你可以使用这个实例变量从 sub-class 中获取每个字段。
public class TestMain {
private InnerFirst first;
private InnerSecond second;
private int outerData = 30;
class InnerFirst{
private InnerFirst() {
first = this;
}
private int data1 = 40;
public int getData() {
return data1;
}
public void setData(int data) {
this.data1 = data;
}
void msg(){
System.out.println("Inner data is : "+data1+" and outer data is : "+outerData);
}
}
class InnerSecond{
private InnerSecond() {
second = this;
}
private int data2 = 40;
public int getData() {
return data2;
}
public void setData(int data) {
this.data2 = data;
}
void msg(){
System.out.println("Inner data is : "+data2+" and outer data is : "+outerData);
}
}
private InnerFirst getFirst() {
return first;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestMain newClass = new TestMain();
TestMain.InnerFirst newInnerClass = newClass.new InnerFirst();
System.out.println(newClass.getFirst().getData());
newInnerClass.setData(50);
newInnerClass.msg();
}
}
想象一下下面提到的场景-
public class TestMain {
private int outerData = 30;
class InnerFirst{
private int data1 = 40;
public int getData() {
return data1;
}
public void setData(int data) {
this.data1 = data;
}
void msg(){
System.out.println("Inner data is : "+data1+" and outer data is : "+outerData);
}
}
class InnerSecond{
private int data2 = 40;
public int getData() {
return data2;
}
public void setData(int data) {
this.data2 = data;
}
void msg(){
System.out.println("Inner data is : "+data2+" and outer data is : "+outerData);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestMain newClass = new TestMain();
TestMain.InnerFirst newInnerClass = newClass.new InnerFirst();
newInnerClass.setData(50);
newInnerClass.msg();
}
}
我的意图是将 newClass 对象设置到会话中并在需要时访问它。但是我怎样才能访问 data1 (内部 class 实例变量)呢? 据我所知,newInnerClass 对象(InnerFirst 的对象)是在 newClass 实例(外部 class)中创建的。我的问题是,如何从外部 class 对象引用访问内部 class 实例成员?
您始终可以将这些子 class 的实例变量保留在您的父 class 中,并且当您创建子 class 的对象时,然后在您的子 [=15] 的构造函数中=]你可以初始化相同的class的实例变量。像这样:对于第二个 class 你可以自己做同样的事情:你可以使用这个实例变量从 sub-class 中获取每个字段。
public class TestMain {
private InnerFirst first;
private InnerSecond second;
private int outerData = 30;
class InnerFirst{
private InnerFirst() {
first = this;
}
private int data1 = 40;
public int getData() {
return data1;
}
public void setData(int data) {
this.data1 = data;
}
void msg(){
System.out.println("Inner data is : "+data1+" and outer data is : "+outerData);
}
}
class InnerSecond{
private InnerSecond() {
second = this;
}
private int data2 = 40;
public int getData() {
return data2;
}
public void setData(int data) {
this.data2 = data;
}
void msg(){
System.out.println("Inner data is : "+data2+" and outer data is : "+outerData);
}
}
private InnerFirst getFirst() {
return first;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestMain newClass = new TestMain();
TestMain.InnerFirst newInnerClass = newClass.new InnerFirst();
System.out.println(newClass.getFirst().getData());
newInnerClass.setData(50);
newInnerClass.msg();
}
}