[Java反射]动态加载类

目录结构

lee-Macbook-Air:java-learn lee$ tree reflect/
reflect/
├── Excel.class
├── Excel.java
├── Office.class
├── Office.java
├── OfficeAble.class
├── OfficeAble.java
├── Word.class
└── Word.java

0 directories, 10 files
class Office {
    public static void main(String[] args) {
        try {
            Class c = Class.forName(args[0]);
            OfficeAble oa = (OfficeAble)c.newInstance();
            oa.start();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
interface OfficeAble {
    public void start();
}
class Word implements OfficeAble {
    public void start() {
        System.out.println("Word start..");
    }
}
class Excel implements OfficeAble {
    public void start() {
        System.out.println("Excel start");
    }
}

java-reflect

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部