![](/img/trans.png)
[英]Android Studio : How to keep variables from one java file to another
[英]how to access variables of one class from another in android studio
嘿伙計們,我想在class2的方法A()中使用class 1的變量我嘗試了以下
class1 obj = new class1();
obj.A();
但它拋出一個錯誤,說無法解析符號A()
只需將您的變量聲明為公開的:
public int variable1;
public int variable2;
Class1() {
this.variable1 = 400;
this.variable2 = 600;
}
Class1 obj = new Class1();
obj.variable1;
或者只是將它們設為私有並添加吸氣劑:
private int variable1;
private int variable2;
Class1() {
this.variable1 = 400;
this.variable2 = 600;
}
public int getVariable1() {
return this.variable1;
}
public int getVariable2() {
return this.variable2;
}
然后
Class1 obj = new Class1();
obj.getVariable1()
您必須返回該變量,如下所示:
class Class1 {
public int A() {
int a = 5 + 6;
return a;
}
}
您需要添加此返回語句: return a; 在 A() function 的末尾返回值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.