我需要在java中的类的方法中通过t1
和t2
。我只展示了相关的部分。在类方法中传递变量
public static void main(String[] args) {
int hr,min,secs;
Time t1;
t1 = new Time();
t1.hr = Integer.parseInt(JOptionPane.showInputDialog("Enter the time(hours)"));
Time t2;
t2 = new Time();
t2.hr = Integer.parseInt(JOptionPane.showInputDialog("Enter the time(hours)"));
}
的类如波纹管
class Time {
int hr;
int add2times(int t1, int t2) {
int result_hr = t1.hr + t2.hr;
}
}
我收到以下错误int result_hr = t1.hr + t2.hr;
int cannot be deference
如果't1'和't2'是'int's,那么它们不是'Time's,并且不会有'hr'字段。 – azurefrog
该方法的声明不应该像'public static int add2times(Time t1,Time t2)'?你希望't1'和't2'是'Time'对象,对吗? –
最少阅读http://docs.oracle.com/javase/tutorial/ –