What's the correct way to “share” variables between methods in different classes?

给你一囗甜甜゛ 提交于 2021-02-17 05:25:11

问题


I'm trying to share variables between methods in different classes, but I don't know if I'm doing this in the correct way. Basically when I wanna use the variables on method2 I have to "transport" them throught method1 to method2 from the Class A, just take a look at the example because I don't know how to explain this properly.

Is this the correct way to do it? Because sometimes I do this over an over through methods and it looks ugly.

Example:

public class A {
    int var1, var2, var3;        
    B b = new B();        
    b.method1(var1, var2, var3);
}

public class B {
    public void method1(int var1, int var2, int var3){
       //doSomething
        method2(var2, var3);
    }

    public void method2(int var2, int var3){ 
        //doSomething
    } 
}

Btw, is there any community where code reviews are done? I'm pretty new to code and I'm afraid that I'm creating code that isn't effective.

Thanks for the help! :)


回答1:


You should read about encapsulation.

Passing 3 variables encapsulated in 1 object with appriopriate accessors sounds like a better option to me (at least the code looks a bit cleaner).

Also, think of creating a utility class with static methods if it makes sense of course - sometimes you do not need class member fields at all because there is no state to this class (Math class is an example) and static methods that return the result of some calculation/transformation is a better option.

On a side note I can recommend you considering "Program to an interfaces" principle. You can read the relevant section right on the top of this page.




回答2:


Use getters and setters to get variable of Class A from B as following..

public class A {

    private int var1, var2, var3;

    public int getVar1(){
        return var1;
    }

    public void setVar1(int var1){
        this.var1 = var1;
    }

    public int getVar2(){
        return var2;
    }

    public void setVar2(int var2){
        this.var2 = var2;
    }

    public int getVar3(){
        return var3;
    }

    public void setVar3(int var3){
        this.var3 = var3;
    }
}

public class B{

    // Use var 1 from class A as following
    A a = new A();
    int x = a.getVar1(); //x has the value of Var1 now
    a.setVar1(2); // var1 in class A has value 2 now.
}

Use interfaces rather than directly call a method of another class.

public class A {
    private InterfaceB interfaceB;

    interfaceB.method1(var1, var2, var3);
}

public interface InterfaceB{
    public void method1(int var1, int var2, int var3);
    public void method2(int var2, int var3);
}

public class B implements InterfaceB{

    @Override
    public void method1(int var1, int var2, int var3){
        //doSomething
        method2(var2, var3);
    }

    @Override
    public void method2(int var2, int var3){ 
        //doSomething
    }
}



回答3:


In B class, you declare a instance of A class, variables in A class is public. when you can use variable in A class.




回答4:


Here is my 2 cents...

public class Sample {
    //Property of the class
    private int valueA;

    //method to do some operation
    //that relies explicitly on a 
    //property of the class
    public void doSomething(){
        //valueA is over 9000!?
        int valueA = valueA + 9000;
    }

    //Method that does something that does not explicitly
    //rely on a property of the class
    //could be called from this or another class
    public int doSomeOperationWithSomething(int something){
        return something++;
    }
}

Another alternative would be to create a static "utility" class for your methods

public class Utils{
    public static int doMagic(int var){
        return var * var;
    }
}

used like this,

int num = Utils.doMagic(9);

These come about when you have some code that does that one useful thing, but you just can't figure out where to put it.

More importantly, you will want to maintain proper "encapsulation" (Read about that) in your code. This means limiting access to variables by other classes and allowing access to only what is needed.

public class Website {
    //No one should ever be able to
    //access this variable directly
    //So we set it a private
    private String article;

    //A reader should be able to get the aricle
    public String getArticle(){
        return article;
    }

    //The reader should never be able to set
    //an aticle on the website only read it
    //You can leave this part out or
    //set the method to private as i did.
    private void setArticle(String article){
        this.article = article;
    }
}

public class Reader {
    //Reference to website
    private Website website;

    public Reader(){

        ...

        //the user can read an article
        website.getArticle();

        // but this is not available to them
        website.setArticle("Some text"); // results in ERROR
    }
}


来源:https://stackoverflow.com/questions/32071388/whats-the-correct-way-to-share-variables-between-methods-in-different-classes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!