调用从另一个类创建的对象

时间:2020-11-29 20:11:50

I need ClassB to get the object created from ClassA, which is u. How do I do that? ClassA sets the value using setSomething() from Utility class, while ClassB should get the value set by ClassA using the getSomething() of the same object(u)

我需要ClassB才能获得从ClassA创建的对象,这就是你。我怎么做? ClassA使用Utility类中的setSomething()设置值,而ClassB应使用同一对象的getSomething()获取ClassA设置的值(u)

public class ClassA
{
   Utility u = new Utility()
   u.setSomething("David");
}

public class ClassB
{
   //How do I get the 'u' Utility object from ClassA
}

public class Utility
{
private String fullName;

   public void setSomething(String name)
   {
      this.fullName = name
   }

   public String getSomething()
   {
      return fullName;
   }

}

2 个解决方案

#1


1  

Straightforward approach without patterns and simple classes.

直接的方法没有模式和简单的类。

public class ClassA {
   private Utility u = new Utility()

   public ClassA() {
       u.setSomething("David");
   }

   public Utility getU() {
        return u;
   }
}

 public class ClassB {
     private ClassA classA = new ClassA();

     public ClassB() {
         System.out.println(classA.getU().getSomething());
     }

 }

public class Utility {
   private String fullName;

   public void setSomething(String name) {
      this.fullName = name;
   }

   public String getSomething() {
      return fullName;
   }
}

public static void main(String[] args) {
    ClassB b = new ClassB();
}

Should print out 'David';

应打印出“大卫”;

#2


1  

Note that there isn't ONE way to do things. There can be one way to do things BETTER. It all depends on your design and needs, this is just an example:

请注意,没有一种方法可以做。可以有一种方法可以做得更好。这完全取决于您的设计和需求,这只是一个例子:

public class ClassA{
   Utility u = new Utility()
   public Utility getU(){
       return u;
   }
   public void buildB(){
       ClassB classB = new ClassB(this);
   }
}

public class ClassB{
   ClassA classA;
   Utility u;
   public ClassB(ClassA classA){
       this.classA = classA;
       u = classA.getU();
   }
}

#1


1  

Straightforward approach without patterns and simple classes.

直接的方法没有模式和简单的类。

public class ClassA {
   private Utility u = new Utility()

   public ClassA() {
       u.setSomething("David");
   }

   public Utility getU() {
        return u;
   }
}

 public class ClassB {
     private ClassA classA = new ClassA();

     public ClassB() {
         System.out.println(classA.getU().getSomething());
     }

 }

public class Utility {
   private String fullName;

   public void setSomething(String name) {
      this.fullName = name;
   }

   public String getSomething() {
      return fullName;
   }
}

public static void main(String[] args) {
    ClassB b = new ClassB();
}

Should print out 'David';

应打印出“大卫”;

#2


1  

Note that there isn't ONE way to do things. There can be one way to do things BETTER. It all depends on your design and needs, this is just an example:

请注意,没有一种方法可以做。可以有一种方法可以做得更好。这完全取决于您的设计和需求,这只是一个例子:

public class ClassA{
   Utility u = new Utility()
   public Utility getU(){
       return u;
   }
   public void buildB(){
       ClassB classB = new ClassB(this);
   }
}

public class ClassB{
   ClassA classA;
   Utility u;
   public ClassB(ClassA classA){
       this.classA = classA;
       u = classA.getU();
   }
}