2015-10-07 56 views
1

编辑前的问题:我如何委托类B中的类A的太多成员变量的初始化过程,并仍然使用类A中的值? 注:(类B和C的存在的类A的内部)如何在方法外部访问类A的成员变量,该变量已在其他类中实例化?

的主要目的是减少什么它在此信息[1]是说我下面存在于类A 太多成员变量:https://stackoverflow.com/a/16994754/2018343

代码会是什么样是 更新:

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 

我期待委派太多的变量,其他子类和主T的初始化过程hing是在主类的后续代码行中初始化变量值的用法。 寻求你的帮助!

+0

的'if'条款没有任何方法里面...(这就是为什么你会收到编译错误“令牌上的语法错误”)。此外,你为什么需要这个“代表团”?你会从中得到什么? – alfasin

+0

我们不能直接使用它们而没有将它们包装在一个方法中?因为我需要在我的代码中使用它进行验证。 –

+0

看起来你在学会走路之前就试图跑步。在开始处理嵌套类和其他“奇特”事物之前,先从更基本的事情开始。祝你好运! – alfasin

回答

1

您可以使用Builder pattern使初始化更加用户友好。

一个从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); 
     } 

    } 
} 

以及如何使用它:

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

谢谢很多人!我没有声望upvote .. –

+0

@DineshRavi无后顾之忧,很高兴我能够帮助! – alfasin

相关问题