我写的一个程序里面main是静态的调用了非静态的方法报错了,结果所有的方法都要改成static才成,有什么好的办法不用把这些方法改成静态的吗?下面是我的程序,谢啦
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
public class BookInfoControl {
private static  List allBookList = new ArrayList();

private static  Book creatBook(File dir) throws UnsupportedEncodingException {
Book book = new Book();
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(new FileInputStream(
dir.getAbsolutePath() + "\\inf.txt"),"UTF-8"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str = null;
try {
while ((str = in.readLine()) != null) {
if (str.startsWith("书名")) {
book.setName(str.substring(3).trim());
} else if (str.startsWith("作者")) {
book.setAuthor(str.substring(3).trim());
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
book.setAbsolutePath(dir.getAbsolutePath());
book.setBelongTo(dir.getParentFile().getName());
File[] chapterFile = dir.listFiles();
for (int i = 0; i < chapterFile.length; i++) {
try {
String tmp = chapterFile[i].getName();
int offset = tmp.indexOf('_');
if (offset == -1)
continue;
int index = Integer.parseInt(tmp.substring(0, offset));
} catch (NumberFormatException e) {
continue;
} Chapter ch = createChapter(book,chapterFile[i]);
book.addChapter(ch);
} return book;
}

private static  Chapter createChapter(Book b,File file) {
Chapter chapter = new Chapter(b);
String tmp = file.getName();
// String name = tmp.substring(tmp.indexOf('_')+1);
int index = Integer.parseInt(tmp.substring(0, tmp.indexOf('_')));
chapter.setName(file.getName());
chapter.setAbsolutePath(file.getAbsolutePath());
chapter.setIndex(index);
chapter.setSize((int) file.length());
return chapter;
}
private static  void buildBooks(File directory) throws UnsupportedEncodingException {


if (isContainsInfFile(directory)) { 
Book b = creatBook(directory);
allBookList.add(b);
} else {
File[] fs = directory.listFiles();
for (int i = 0; i < fs.length; ++i) {
if (fs[i].isDirectory()) {
buildBooks(fs[i]);
}
}
}
}
private static  boolean isContainsInfFile(File d) {
File[] files = d.listFiles();
for (int i = 0; i < files.length; ++i) {
if (files[i].getName().equalsIgnoreCase("inf.txt"))
return true;
}
return false;
}
public static void main(String[] args) throws UnsupportedEncodingException {
BookInfoControl controller = new BookInfoControl();
BookInfoControl.buildBooks(new File(args[0]));

for (int i = 0; i < controller.allBookList.size(); ++i) {
Book book = (Book) BookInfoControl.allBookList.get(i);
System.out.println("书的路径: "+book.getAbsolutePath());
System.out.println("书名:"+book.getName() + '\n'+" 作者: " + book.getAuthor());
System.out.println("属于:"+book.getBelongTo());
System.out.println();
}
System.out.println();
}
}

解决方案 »

  1.   

    BookInfoControl.buildBooks(new File(args[0]));
    改成controller.buildBooks(new File(args[0]));
    Book book = (Book) BookInfoControl.allBookList.get(i);
    改成Book book = (Book) controller.allBookList.get(i);
    再把那些static去掉.
      

  2.   

    同意楼上的!
    对于类的方法,非static类型的须通过对象调用,而类方法(static修饰的)可以直接用 
    类名.method 方式调用.楼主刚学Java?
      

  3.   

    要new的public class Test{
    public static void main(String[] args){
    new Test().tomuno();
    //tomuno();
    }
    public void tomuno(){}
    }
      

  4.   

    static是同一个类的不同实例(甚至不需要实例)所共有的而non-static是某一个实例都有的(必须要有实例,也就是为何要new)
      

  5.   

    把要用到的方法改成public的,然后在主方法内生成一个本类的对象调用方法就好了,
    不知道楼主为什么要自称MM,是不是感觉这样会给你解答问题的人比较多呢?
    还是自认为女人的优势比较大呢?
      

  6.   

    怎么我跟楼上两位同感呢,
    问就问好了,MM又怎么样,尤其厌恶自称MM的
      

  7.   

    在static的方法中是没有办法使用this的,
    要使用类中的非static方法就需要先实例化这个类
    (不论是这个方法自身的类,还是调用其它类),
    都必须先形成对象,然后再调用对象中的方法就可以传递消息了。
      

  8.   

    liu_you(滴水藏海) 正解MM还是去看看面向对象方面的书吧!
      

  9.   

    static看成直接和class交流
    非static看成和class的object说话