【发布时间】:2012-03-01 20:35:33
【问题描述】:
你如何使用里面有代码的方法?
public void initialiseVariables(){
name=timestamp.substring(0, 20);
// numofdep = timestamp.substring(35, 37);
//noofovertime = timestamp.substring(38, 40);
if(timestamp.charAt(20)=='C')
city=true;
if(timestamp.charAt(21)=='U')
union=true;
identificationnumber=Integer.parseInt(timestamp.substring(22,26));//to get an integer from a string use the integer.parseint
numofdep = Integer.parseInt(timestamp.substring(35, 37));
noofovertime = Integer.parseInt(timestamp.substring(38, 40));
hoursworked=Integer.parseInt(timestamp.substring(27,29));
hourlyrate = Double.parseDouble( timestamp.substring(30, 34));
}
我不明白你在构造函数中放了什么。另外,您如何确定在参数中放入的内容?就像我朋友写的这个构造函数。
Employees(){
this.timestamp=timestamp;
initialiseVariables();
}
那么我该如何使用例如这条线,
name=timestamp.substring(0, 20);
在我的主要课程中?
【问题讨论】:
-
如果您不知道方法/函数/例程是什么,您真的需要参加课程或阅读有关基本编程的书籍。
-
恐怕你的问题没有多大意义。除非您改写您的问题以更清楚地了解您要问的内容,否则您不太可能得到非常有用的答案。例如我不明白您所说的“内部包含代码的方法”是什么意思,因为(几乎)所有方法都有代码。
-
我不太明白这个问题。但如果
timestamp是Employee类的成员,则只能通过Employee的实例从main引用它。请注意,main是静态的,因此即使main是Employee的方法,它也成立。 -
抱歉没有说清楚。
标签: java class inheritance methods constructor