ISay obj=(ISay)Class.forName(args[0]).newInstance(); obj.say();

来源:互联网 发布:淘宝折扣网站 编辑:程序博客网 时间:2024/06/06 06:06
class mainClass 
{
public static void main(String[] args) 
{
System.out.println(args[0]);
try{
        ISay obj=(ISay)Class.forName(args[0]).newInstance();
obj.say();
}
catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


public interface ISay
{
void say();
}  


class SayHello implements ISay 
{
public void say() 
{
System.out.println("Hello!");
}
}


class SayBye implements ISay 
{
public void say() 
{
System.out.println("Bye!");
}
}


0 0
原创粉丝点击