神马 - 云代码空间
—— 任何傻瓜都能写出计算机可以理解的代码,好的程序员能写出人能读懂的代码。
Class aClass = Class.forName(xxx.xx.xx); Object anInstance = aClass.newInstance();
Class Driver{ protected static Driver current; public static Driver getDriver(){ return current; } } Class MyDriver extends Driver{ static{ Driver.current=new MyDriver(); } MyDriver(){} }
Class.forName("MyDriver"); Driver d=Driver.getDriver();
public class MyJDBCDriver implements Driver { static { DriverManager.registerDriver(new MyJDBCDriver()); } }
class A { void aa() { System.out.println ("A里的"); } } class B extends A { void aa() { System.out.println ("B里的"); } } class C extends A { void aa() { System.out.println ("C里的"); } } public class ClassDemo { public static void main (String[] args) { ClassDemo t = new ClassDemo(); t.show ("C"); } void show (String name) { try { A show = (A) Class.forName (name).newInstance(); show.aa(); } catch (Exception e) { System.out.println (e); } } }