简体   繁体   English

我有一个外部类的对象。 如何从中获取内部类的对象?

[英]I have an object of an outer class. How do I get the object of the inner class from it?

public class Class1 {

  public Class2 getClass2() {
  //How can I implement this method?
  }

  public class Class2 {
  //...
  }
}

I just can't get it done, even though it should only be one line of code... 即使只是一行代码,我也无法完成它...

You should not do this. 您不应该这样做。 The inner class should be used in the outer class and not elsewhere. 内部类应在外部类中使用,而不能在其他地方使用。 If you need an instance of the inner class then it should be not an inner class. 如果需要内部类的实例,则它不应是内部类。

Just create a getter method in the outer class like this. 像这样在外部类中创建一个getter方法。

public class Class1 {

  public Class2 getClass2() {
  //How can I implement this method?
  }

  public Class2 getClass2() {
    return new Class2();
  }

  public class Class2 {
  //...
  }
}

I think you are getting the concept of inner classes wrong. 我认为您弄错了内部类的概念。 In your example, you have a public inner class. 在您的示例中,您有一个公共内部类。 That doesn't mean that your Class1 has exactly one object of Class2 . 这并不意味着您的Class1恰好具有Class2一个对象。 You can still create as many objects of Class2 as you like. 您仍然可以根据需要创建任意数量的Class2对象。 If you want to do this outside of Class1 use new Class1.Class2() . 如果要在Class1之外执行此操作,请使用new Class1.Class2() While this is possible, it would be bad design as galovics correctly mentioned in his answer. 尽管这是可能的,但正如galovics在他的回答中正确提到的那样,这将是一个糟糕的设计。

If what you are trying to achieve is having one object reference to a Class2 object inside Class1 , use a field of type Class2 in Class1 and a matching getter method: 如果你正在努力实现的是具有一个对象引用Class2内对象Class1 ,使用类型的字段Class2Class1和匹配的getter方法:

public class Class1 {

    private Class2 class2instance = new Class2();

    public Class2 getClass2() {
        return class2instance;
    }

    private class Class2 {
        //...
    }
}
public class Class1 {
    private Class2 class2 = new Class2();

    public Class2 getClass2() {
        return class2;
    }

    public class Class2 {
    //...
    }
}

暂无
暂无

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

相关问题 我如何从内部类对象访问外部类的方法和全局变量,以及如果我不能拥有内部类的目的 - How do I access the methods and Global variables of outer class from an inner class object, and if I cannot what is purpose of having an Inner class 如何使用外部类中的类的Graphics对象 - How do i use the Graphics object of a class from an Outer class 从内部类对象获取外部类对象 - Get the outer class object from an inner class object 如何在外部类构造函数中创建内部类的实例 - How do I create an instance of a inner class in the outer class constructor 如何将对外部类的引用传递给内部类中的方法? (或者如何将“ this”传递给内部类?) - How do I pass a reference to the outer class to a method in an inner class? ( Or how do I pass “this” to an inner class? ) 如何使用外部非静态类的对象访问静态内部类方法? - How can I access static inner class method using object of outer non static class? 从当前外部类对象实例化内部类对象 - Instantiate inner class object from current outer class object 从内部类对象获取外部类对象 - Getting hold of the outer class object from the inner class object 如何从一个类中的JtextField输入到另一个类中的ArrayList的输入。 爪哇 - How do I get input from a JtextField in one class to an ArrayList in another class. Java 如果匿名内部类有两个外部类,如何获取外部类的引用? - How can I get the outer class's reference if the anonymous Inner Class has two outer class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM