就像说我有两个类,Class1
和Class2
,并且我想在使用来自的字段(例如:)if
时发表声明。Class2
int option
Class1
我尝试创建一个对象:
Class1 object = new Class1();
in Class2
,然后编写if
语句:
if(object.option == 2)
但它没有用。
import java.util.Scanner;
public class Class1 {
public static void main(String[] args) {
Class2 obj = new Class2();
int option;
Scanner input = new Scanner(System.in);
System.out.print("Enter an option of 1, 2 or 3");
option = input.nextInt();
obj.input();
}
}
public class Class2 {
public void input(){
Class1 object = new Class1();
if(object.option == 1){
System.out.print("You've selected the option 1");
}
else if(object.option == 2){
System.out.print("You've selected the option 2");
}
else if (object.option == 3){
System.out.println("You've selected the option 3");
}
}
}
我收到编译错误:选项无法解析为字段