简体   繁体   English

在Java中获取私有类的对象

[英]Getting an object of a private class in java

I am not so proficient in java, and have a small question. 我不太精通Java,有一个小问题。

A lot of times I see the following code: 很多时候,我看到以下代码:

public class A
{
   private class B {

       B() {
       }

       get() {
       return this;
       }
   }

   public B getB() {
      return new B().get();
   }    
}

My question is, what is the difference if getB() just returns new B() instead of new B.get() Is it just good software engineering when you do return B().get(), or is there some deeper reasoning? 我的问题是,如果getB()仅返回新的B()而不是新的B.get(),有什么区别?当您返回B()。get()时,它是否只是一种好的软件工程,还是有一些更深层次的推理方法? ?

The return this returns current instance of B . return this返回值返回B当前实例。 In your case new B().get(); 在您的情况下, new B().get(); returns new instance of B (created right now). 返回B新实例(立即创建)。

So return new B().get(); 因此return new B().get(); and new B() do the same and equivalent. new B()相同且等效。

The get() method or I would say getInstance() method we can use in Singleton pattern, like: 我们可以在Singleton模式中使用的get()方法或我想说的getInstance()方法,例如:

public class B {

 private static B instance = null; 

  public static B getInstance(){
   if(instance == null){
       instance = new B();
    }

    return instance;
  }    
} 

So no matter how many times we call getInstance() , it returns the same instance 因此,无论我们调用getInstance()多少次,它都会返回相同的实例

基本上返回“ this”的方法是没有用的-应该调用该方法的代码已经引用了该对象

There's no difference. 没有区别 Because when you create a new B() JVM will allocate a new addres for the object (eg: 100001F), and when you call new B().get() it will return the same address (100001F). 因为当您创建一个new B() JVM将为该对象分配一个新的地址(例如:100001F),并且当您调用new B().get() ,它将返回相同的地址(100001F)。 If you just return new B() , it will return the same address (100001F). 如果仅返回new B() ,它将返回相同的地址(100001F)。

My particular opinion: return new B() is the best option, because it allocate the object and return the address, instead of allocate and later invoque get() method. 我的特别意见: return new B()是最好的选择,因为它分配对象并返回地址,而不是分配和稍后调用get()方法。

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

相关问题 在Android / java中调用类的私有对象的方法 - Call a method of a private object of a class in Android/java 如何访问在Java中使用它的唯一类的私有嵌套类对象 - How to access a private nested class object of a sole class that uses it in java Java - 私有内部类的对象,作为外部类构造函数的参数 - Java - object of a private inner class being as an argument to an outer class' constructor 如何返回私有内部object class object java - How to return object of private inner class object java 面向对象的编程,getter不会从另一个类获取私有变量 - Object oriented programming, getter is not getting private variable from another class java-private内部类实例未获取垃圾 - java-private inner class instances not getting garbage collected Java Lambda引用封闭对象:替换为私有静态类? - Java Lambda Referencing Enclosing Object: Replace with Private Static Class? 调用类中定义的私有对象的方法(Java反射) - Invoke a method of a private object defined in a class (Java reflection) 对象锁定私有类成员 - 最佳实践? (JAVA) - Object locking private class members - best practice? (Java) 使用Java反射访问类中的私有对象实例属性 - Access private object instance attribute in class using java reflection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM