如何在Java中从两个不同的类访问变量

huangapple go评论79阅读模式
英文:

How can I access variables from 2 different classes in Java

问题

我在Java中有3个不同的类。FirstClass 有2个 StringThirdClass 有1个 StringSecondClass 需要访问这些字符串。如何才能访问到所有这些字符串呢?

如果有2个类,我会做类似下面的操作:

    public class FirstClass {
        private String name;
        private String lname;
    
        public FirstClass(){
            name = "John";
            lname = "Watson";
            new SecondClass(name, lname);
        }
    }
    public class SecondClass{
         new SecondClass(String name, String lname){
              //做一些操作
          }
    }

如果我有一个 ThirdClass,另一个字符串从 SecondClass 中访问可行吗?

我搜索了很多,除了两个类的解决方案外,没有其他可行方法。我不想进行以下操作:

    public class FirstClass{
        private String name;
        private String lname;
    
        public FirstClass(){
            name = "John";
            lname = "Watson";
            new ThirdClass(name, lname);
        }
    }
    public class ThirdClass{
        private String location;
        public ThirdClass(String name, String lname){
            location = "Europe";
            new SecondClass(name, lname, location);
        }
    }
    public class SecondClass{
         new SecondClass(String name, String lname, String location){
              //做一些操作
          }
    }

我也不能把所有的字符串放在一个类中。有没有办法可以实现这个需求呢?

英文:

I have 3 different classes in Java. FirstClass that has 2 Strings, ThirdClass that has 1 String, and SecondClass that needs access to those Strings. How can I access all of these Strings?

If there were 2 classes I would have done something like the following

    public class FirstClass {
        private String name;
        private String lname;
    
        public FirstClass(){
            name = "John";
            lname = "Watson";
            new SecondClass(name, lname);
        }
    }
    public class SecondClass{
         new SecondClass(String name, String lname){
              //doSomething
          }
    }

How can I have a ThirdClass with another String accessible from SecondClass?

I've searched a lot and nothing came up other than the solution with 2 classes. I don't want to do the following

    public class FirstClass{
        private String name;
        private String lname;
    
        public FirstClass(){
            name = "John";
            lname = "Watson";
            new ThirdClass(name, lname);
        }
      }
    public class ThirdClass{
        private String location;
        public ThirdClass(String name, String lname){
            location = "Europe";
            new SecondClass(name, lname, location);
        }
    }
    public class SecondClass{
         new SecondClass(String name, String lname, String location){
              //doSomething
          }
    }

I also can't put all the Strings in one class. Is there a way that I can do it?

答案1

得分: 1

更好的解决方案将是@Yousaf的解释。引用他的话:

你需要在SecondClass中创建FirstClass和ThirdClass的实例,以便访问来自FirstClass和ThirdClass的私有实例字段,你可以在FirstClass和ThirdClass中创建getter和setter函数。

第一类

public class FirstClass{
    private String name;
    private String lname;

    public FirstClass(){
        name = "John";
        lname = "Watson";
    }
    
    public String getName() {
       return this.name;
    }

    public String getLName() {
       return this.lname;
    }
}

第三类

public class ThirdClass{
    private String location;
    public ThirdClass(String name, String lname){
        location = "Europe";
    }

    public String getLocation() {
       return this.location;
    }
}

第二类

public class SecondClass{
     private FirstClass firstClass;
     private ThirdClass thirdClass;
     new SecondClass(){
          firstClass = new FirstClass();
          thirdClass = new ThirdClass();

          String name = firstClass.getName();
          String LName = firstClass.getLName();
          String location = thirdClass.getLocation();
      }
}

请注意,我在代码的构造函数中创建了对象实例,但你不必这样做。你可以在主方法中创建它们,然后将它们作为参数传递给SecondClass,这也可以正常工作。

它看起来会像这样

主方法

public static void main(String[] args) {
   FirstClass firstClass = new FirstClass();
   ThirdClass thirdClass = new ThirdClass();
   SecondClass secondClass = new SecondClass(firstClass, thirdClass);
}

第二类

public class SecondClass{
     private FirstClass firstClass;
     private ThirdClass thirdClass;
     new SecondClass(FirstClass firstClass, ThirdClass thirdClass){
          this.firstClass = firstClass;
          this.thirdClass = thirdClass;
      }
}

或者你可以进行深拷贝。如果你想要隔离第二类,使得对第一和第三类数据的更改不会影响第二类

第二类

public class SecondClass{
    private String name;
    private String lname;
    private String location;
     new SecondClass(FirstClass firstClass, ThirdClass thirdClass){
          this.name = firstClass.getName();
          this.lname = firstClass.getLName();
          this.location = thirdClass.getLocation();
      }
}
英文:

A better solution will be how @Yousaf explained. Quoting him

> You need to create an instance of FirstClass and ThirdClass in SecondClass to access the private instance fields from FirstClass and SecondClass, you can create getter and setter functions in FirstClass and ThirdClass

First class

public class FirstClass{
    private String name;
    private String lname;

    public FirstClass(){
        name = "John";
        lname = "Watson";
    }
    
    public String getName() {
       return this.name;
    }

    public String getLName() {
       return this.lname;
    }
}

Third Class

public class ThirdClass{
    private String location;
    public ThirdClass(String name, String lname){
        location = "Europe";
    }

    public String getLocation() {
       return this.location;
    }
}

Second Class

public class SecondClass{
     private FirstClass firstClass;
     private ThirdClass thirdClass;
     new SecondClass(){
          firstClass = new FirstClass();
          thirdClass = new ThridClass();

          String name = firstClass.getName();
          String LName = firstClass.getLName();
          String location = thirdClass,getLocation();
      }
}

Note that I am creating instances of object in the constructor of the code but you don't have to do that. You can create then in the main method and pass them to SecondClass as a parameter, and that would work as well.

It would look something like this
Main Method

public static void main(String[] args) {
   FirstClass firstClass = new FirstClass();
   ThirdClass thirdClass = new ThridClass();
   SecondClass secondClass = new SecondClass(firstClass, thirdClass);
}

Second Class

public class SecondClass{
     private FirstClass firstClass;
     private ThirdClass thirdClass;
     new SecondClass(FirstClass firstClass, ThirdClass thirdClass){
          this.firstClass = firstClass;
          this.thirdClass = thirdClass;
      }
}

or you could just do a deep copy. If you want to isolate second class so changes to data from first and third won't affect the second

Second Class

public class SecondClass{
    private String name;
    private String lname;
    private String Location;
     new SecondClass(FirstClass firstClass, ThirdClass thirdClass){
          this.name = firstClass.getName();
          this.lname = firstClass.getLName();
          this.location = thirdClass.getLocation;
      }
}

答案2

得分: 0

class FirstClass {

    private String name;
    private String lname;

    public FirstClass() {
        name = "John";
        lname = "Watson";
    }

    public String getName() {
        return name;
    }

    public String getLName() {
        return lname;
    }
}

class SecondClass {

    // composition of classes - promotes loose coupling
    private FirstClass firstClass;
    private ThirdClass thirdClass;

    /**
     * Constructor 1 - takes only a FirstClass and 
     * delegates the assignment to Constructor 2 
     * A class can have more than one constructor 
     * so long as the signatures are unique
     * @param firstClass
     */
    public SecondClass(FirstClass firstClass) {
        this(firstClass, null);
    }

    /**
     * Constructor 2 - takes two inputs FirstClass AND ThirdClass
     * Any calls to get the methods are delegated to the classes
     * this promotes loose coupling
     * @param firstClass
     * @param thirdClass
     */
    public SecondClass(FirstClass firstClass, ThirdClass thirdClass) {
        this.firstClass = firstClass;
        this.thirdClass = thirdClass;
    }

    public String getName() {
        // delegate getName to firstClass
        return firstClass.getName();
    }

    public String getLName() {
        // delegate
        return firstClass.getLName();
    }

    public String getLocation() {
        // delegate
        return thirdClass.getLocation();
    }
}

class ThirdClass {

    private String location;

    public ThirdClass(String location) {
        location = "Europe";
    }

    public String getLocation() {
        return location;
    }
}
英文:

EDIT : removed first awful attempt...

I did some refactoring of the code and came up with the following:

class FirstClass {

    private String name;
    private String lname;

    public FirstClass() {
        name = "John";
        lname = "Watson";
    }

    public String getName() {
        return name;
    }

    public String getLName() {
        return lname;
    }
}

class SecondClass {

    // composition of classes - promotes loose coupling
    private FirstClass firstClass;
    private ThirdClass thirdClass;

    /**
     * Constructor 1 - takes only a FirstClass and 
     * delegates the assignment to Constructor 2 
     * A class can have more than one constructor 
     * so long as the signatures are unique
     * @param firstClass
     */
    public SecondClass(FirstClass firstClass) {
        this(firstClass, null);
    }

    /**
     * Constructor 2 - takes two inputs FirstClass AND ThirdClass
     * Any calls to get the methods are delegated to the classes
     * this promotes loose coupling
     * @param firstClass
     * @param thirdClass
     */
    public SecondClass(FirstClass firstClass, ThirdClass thirdClass) {
        this.firstClass = firstClass;
        this.thirdClass = thirdClass;
    }

    public String getName() {
        // delegate getName to firstClass
        return firstClass.getName();
    }

    public String getLName() {
        // delegate
        return firstClass.getLName();
    }

    public String getLocation() {
        // delegate
        return thirdClass.getLocation();
    }
}

class ThirdClass {

    private String location;

    public ThirdClass(String location) {
        location = "Europe";
    }

    public String getLocation() {
        return location;
    }
}

You need to include the object references and then wrap (chain) the getter calls to return the results of other getters.

huangapple
  • 本文由 发表于 2020年10月15日 03:47:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64360549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定