找到你要的答案

Q:how to get the variable attribute of different activities/fragments with same variable name?

Q:如何获得具有相同变量名的不同活动/片段的变量属性?

But if I got different activities/fragments with same variable name like:

FragmentA.java:

    public static int cat_id = 1;

    ...


    public static int getCategory_ID() {

    return cat_id;

    }

FragmentB.java:

    public static int cat_id = 2;

    ...


    public static int getCategory_ID() {

    return cat_id;

    }

Parser.java: ...

how to get the category_id of each fragments to set them in if statement like:

    int k_id = -1;

    ...


    k_id = current.getInt(...);


    if (k_id == cat_id) {

    do ... 

    }

但如果我有不同的活动/片段相同的变量名称:

FragmentA.java:

    public static int cat_id = 1;

    ...


    public static int getCategory_ID() {

    return cat_id;

    }

FragmentB.java:

    public static int cat_id = 2;

    ...


    public static int getCategory_ID() {

    return cat_id;

    }

Parser.java: ...

如何让每个片段的category_id设置他们在if语句一样:

    int k_id = -1;

    ...


    k_id = current.getInt(...);


    if (k_id == cat_id) {

    do ... 

    }
answer1: 回答1:

those are static methods so you can use them by class name

FragmentB.getCategory_ID()

FragmentA.getCategory_ID()

这些都是静态的方法,所以你可以使用类名

FragmentB.getCategory_ID()

FragmentA.getCategory_ID()
answer2: 回答2:

You can get them using class name as:

if(k_id==FragmentA.getCategory_ID())

OR

if(k_id==FragmentB.getCategory_ID())

您可以使用类名作为:

if(k_id==FragmentA.getCategory_ID())

if(k_id==FragmentB.getCategory_ID())
java  android  variables  if-statement  static