这是主要的类:public class TestAbstractFactory{ public static void main(String []args){ String [] creatros = {"ConcreteCreatorA", "ConcreteCreatorB"}; try{ int choice = Integer.parseInt("0"); //---------- //Creator c = Creator)Class.forName(creatros[choice]).newInstance(); //Creator c = new ConcreteCreatorA(); //---------- c.anOperation(); } catch(Exception e){ System.out.println("Problem Encoutered"); System.out.println(e); } }}结果 1 (Creator c = (Creator)Class.forName(creators[choice]).newInstance();):遇到的问题java.lang.ClassNotFoundException:ConcreteCreatorA结果 2(Creator c = new ConcreteCreatorA();):创建的产品:ConcreteProductA*这个结果是预期的。所以问题是,为什么我在第一个程序中运行失败,但在第二个程序中运行以获得我的预期结果?
添加回答
举报
0/150
提交
取消