简体   繁体   English

如何在方法外部访问已在其他类中实例化的类A的成员变量?

[英]How can I access the member variable of class A outside the method, that has been instantiated in other class?

Question before editing: How can I delegate the initialization process of too many member variable of class A inside class B and C and still use the value inside class A? 编辑之前的问题:如何在类B和C内委派太多类A的成员变量的初始化过程,而仍使用类A内的值? Note: (class B and C is present inside class A) 注意:(B和C类存在于A类内部)

The main objective is to reduce too many member variables present in class A. I am following whats it is said in this post [1]: https://stackoverflow.com/a/16994754/2018343 主要目的是减少A类中存在的太多成员变量。我正在关注本文[1]中所说的内容: https : //stackoverflow.com/a/16994754/2018343

code would be like is UPDATED: 代码就像是UPDATED:

public class A{ // start of class A
    public int a;
    int b;
    public int c;
    int d;
    int x;
    int g;

    B bObject; //instance of class B
    C cObject; //instance of class B

    A(){ /**Constructor*/
        bObject = new B(3,4);
        cObject = new C(5,6);
    } /*
       *There is an error in eclipse after this closing bracket
       *"Syntax error on token "}", { expected after this token" 
       */

    /**
     * My end goal: I need to Use the initialized variables after the constructor 
     */
    public void yui(){
    if(true){ // variables a and c  
//      System.out.println("A is greater");
    x=a;
    g=c;
    }

    } /**
      * Syntax error on token "}", { expected after this token  */


    if(x<g){ // variables x and g  
        System.out.println("A is greater");
    }

    class B{ //Note: This class is inside class A
        B(int val1, int val2){
            a=val1;
            b=val2;
        }
    }

    class C{ // //Note: This class is inside class A
        C(int val3,int val4){
            c= val3;
            d= val4;
        }
    }

    public static void main(String[] args){
        A a = new A();
        a.yui();

    }

} // end of class A

I am really looking for delegating the initialization process of too many variables to other child class and main thing is use that initialized variable value in the subsequent lines of code in master class. 我确实正在寻找将太多变量的初始化过程委托给其他子类的方法, 主要是在主类的后续代码行中使用该已初始化变量的值。 Seek your help! 寻求您的帮助!

You can use the Builder pattern to make the initialization more user friendly. 您可以使用Builder模式来使初始化更加用户友好。

A nice example that was taken from here : 一个很好的例子是从这里得到的

public class User {
    private final String firstName; // required
    private final String lastName; // required
    private final int age; // optional
    private final String phone; // optional
    private final String address; // optional

    private User(UserBuilder builder) {
        this.firstName = builder.firstName;
        this.lastName = builder.lastName;
        this.age = builder.age;
        this.phone = builder.phone;
        this.address = builder.address;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public int getAge() {
        return age;
    }

    public String getPhone() {
        return phone;
    }

    public String getAddress() {
        return address;
    }

    public static class UserBuilder {
        private final String firstName;
        private final String lastName;
        private int age;
        private String phone;
        private String address;

        public UserBuilder(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public UserBuilder age(int age) {
            this.age = age;
            return this;
        }

        public UserBuilder phone(String phone) {
            this.phone = phone;
            return this;
        }

        public UserBuilder address(String address) {
            this.address = address;
            return this;
        }

        public User build() {
            return new User(this);
        }

    }
}

and how to use it: 以及如何使用它:

public User getUser() {
    return new
            User.UserBuilder("Jhon", "Doe")
            .age(30)
            .phone("1234567")
            .address("Fake address 1234")
            .build();
}

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

相关问题 如何执行已上传的类的方法? - How can I execute a method of a class that has been uploaded? 如果我已将其声明为Java中类的成员变量,是否可以访问另一个方法中一个方法的变量值? - Can i access the value of a variable of one method in another method if i have declared it as a member variable of the class in java? 无法访问实例化类的受保护方法 - Can't Access Protected Method of Instantiated Class 可以在静态方法中实例化的匿名内部类是否可以访问包含类的实例成员? - Can Anonymous inner class instantiated within a static method has access to instance members of containing class? java中其他class的方法如何访问变量 - How to access the variable in the method of other class in java 如何将一个类变量访问另一个 - How can i access one class variable into other 如何使用已作为变量传递的类中的方法 - How to Use Method from a Class that has been Passed in as a Variable 如何访问从Java中抽象类的子类实例化的对象? - How do I access the object that's been instantiated from the abstract class's subclass in java? 如何在课外访问私有数据成员? - How to access private data member outside the class? 如何直接访问m1()方法中的A类变量? - How can i access Class A variable in m1() method directly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM