从另一个类中的受保护方法获取变量值

时间:2015-07-21 22:52:37

标签: java android protected

全新的Android和Java ......

我在protected中有一个Class2方法,如下所示:

protected void onListClick(List 2, View view, int position, long id)
{

.....

int Count = cursor.GetInt(countIndex);

........
}

我需要从Count访问Class2的值。

Class1(尝试从Class2访问值)中的

我有int getValue = Class2.Count;

为什么这不起作用?

1 个答案:

答案 0 :(得分:1)

Count是一个局部变量,因此其他类无法访问它。您可以改为创建Class2的静态类成员。

class Class2 {
    static int Count;
    ...
    protected void onListClick(List 2/*invalid name*/, View view, int position, long id)
    {
        ...
        Count = cursor.GetInt(countIndex);
        ...
    }
}

对未来的建议:使用camelCase命名所有变量和方法,绝不使用数字或特殊字符。代码中的一些不正确的示例:int Count,List 2GetInt()