在线揭帖:使用类封装操作后就执行出问题了
h3w4 2007-01-30 10:10:25 ///////////////////////////////////sort.java
import java.io.*;
public class sort
{
public static void main(String args[]) throws IOException
{
int myArray[]={99,88,77,66,55,44,33,22,11};
sortTest st = new sortTest ( myArray );
st.init(); //初始化写文件的那些对象
// logic operation
st.out_file ( "Before Sorting:" ); // 没有写入文件
st.out_crt ("before"); // 执行正确
st.sortnub (); // 进行排序
st.out_crt ("after"); // 执行正确
st.out_file ( "After Sorting:" ); // 没有写入文件
}
}
///////////////////////////////sortTest.java
import java.io.*;
class sortTest
{
// data member
private int myArray[];
private FileWriter fw;
private BufferedWriter bf;
private PrintWriter out;
// member function
sortTest ( int arr[] ) throws IOException
{
myArray = arr;
}
protected void finalize() throws IOException
{
out.close();
}
void init() throws IOException
{
fw = new FileWriter("hh");
bf = new BufferedWriter(fw);
out = new PrintWriter(bf);
}
void sortnub()
{
for( int i=0;i<myArray.length;i++){
for( int j=i+1;j<myArray.length;j++){
if ( myArray[i]>myArray[j]){
int temp=myArray[i];
myArray[i]=myArray[j];
myArray[j]=temp;
}
}
}
};
void out_crt ( String s )
{
System.out.println(s);
for (int i=0;i<myArray.length;i++)
System.out.print(myArray[i]+" ");
System.out.println();
};
void out_file ( String text ) throws IOException
{
out.print(text+"\n");
for (int i=0;i<myArray.length;i++)
out.print(myArray[i]);
out.print("\n");
};
};
%java sort
before
99 88 77 66 55 44 33 22 11
after
11 22 33 44 55 66 77 88 99
% ls -l
-rwxr-xr-x 1 root wheel 0 Jan 30 09:53 hh
但是一开始sortTest.init的内容是些在sort.main中的,然后传参进去,执行都好着呢,不知改后为什么会这样
由于第一次写java,请大家不吝赐教,此外如果觉得程序的写法,分布...等任何地方有问题的请提出来,谢谢哦