本文发表在 rolia.net 枫下论坛以下是ReadFile类.
import java.io.*;
import java.lang.*;
public class ReadFile {
int i=1;
public Customer cust;
String strLine;
/** Creates a new instance of ReadFileTest */
public ReadFile() {
try{
cust=new Customer();
BufferedReader in=new BufferedReader(new FileReader("/home/hua/a-work/project-simu/bc2"));
while ((strLine=in.readLine())!=null){
//get the arrival time
String str1=strLine.substring(5, 13);
str1=str1.trim();
Double num1=Double.valueOf(str1);
cust.arrivalTime=num1.doubleValue();
//get the cust length
String str2=strLine.substring(13, strLine.length());
str2=str2.trim();
Double num2=Double.valueOf(str2);
cust.length=num2.doubleValue();
System.out.println(i+".arrival time: "+cust.arrivalTime);
System.out.println(i+".length: "+cust.length);
i++;
// a small example try to tackle the data in other class
ReadFileTest.DoSomething(cust);
}
}
catch (IOException e){
e.printStackTrace();
}
}
}
下面是ReadFileTest类.
public class ReadFileTest {
/** Creates a new instance of ReadFileTest */
public ReadFileTest() {
}
/**
* @param args the command line arguments
*/
public static void DoSomething(Customer cust){
cust.arrivalTime=cust.arrivalTime+100;
System.out.println(".arrival time: "+cust.arrivalTime);
System.out.println(".length: "+cust.length);
}
public static void main(String[] args) {
new ReadFile();
}
}
问题是,ReadFileTest中的DoSomething方法必需是static,否则会报错:
ReadFile.java [44:1] non-static method DoSomething(Customer) cannot be referenced from a static context
ReadFileTest.DoSomething(cust);
^
为什么ReadFileTest.DoSomething(cust)是static context呢?这里的DoSomething只是个小例子,真正以后要作的操作是很复杂的而且不太肯可能一定是static的方法.怎么修正,让它不要求非是static的方法? 谢谢更多精彩文章及讨论,请光临枫下论坛 rolia.net
import java.io.*;
import java.lang.*;
public class ReadFile {
int i=1;
public Customer cust;
String strLine;
/** Creates a new instance of ReadFileTest */
public ReadFile() {
try{
cust=new Customer();
BufferedReader in=new BufferedReader(new FileReader("/home/hua/a-work/project-simu/bc2"));
while ((strLine=in.readLine())!=null){
//get the arrival time
String str1=strLine.substring(5, 13);
str1=str1.trim();
Double num1=Double.valueOf(str1);
cust.arrivalTime=num1.doubleValue();
//get the cust length
String str2=strLine.substring(13, strLine.length());
str2=str2.trim();
Double num2=Double.valueOf(str2);
cust.length=num2.doubleValue();
System.out.println(i+".arrival time: "+cust.arrivalTime);
System.out.println(i+".length: "+cust.length);
i++;
// a small example try to tackle the data in other class
ReadFileTest.DoSomething(cust);
}
}
catch (IOException e){
e.printStackTrace();
}
}
}
下面是ReadFileTest类.
public class ReadFileTest {
/** Creates a new instance of ReadFileTest */
public ReadFileTest() {
}
/**
* @param args the command line arguments
*/
public static void DoSomething(Customer cust){
cust.arrivalTime=cust.arrivalTime+100;
System.out.println(".arrival time: "+cust.arrivalTime);
System.out.println(".length: "+cust.length);
}
public static void main(String[] args) {
new ReadFile();
}
}
问题是,ReadFileTest中的DoSomething方法必需是static,否则会报错:
ReadFile.java [44:1] non-static method DoSomething(Customer) cannot be referenced from a static context
ReadFileTest.DoSomething(cust);
^
为什么ReadFileTest.DoSomething(cust)是static context呢?这里的DoSomething只是个小例子,真正以后要作的操作是很复杂的而且不太肯可能一定是static的方法.怎么修正,让它不要求非是static的方法? 谢谢更多精彩文章及讨论,请光临枫下论坛 rolia.net