java create objects of a variety of methods

1. new to create an object keyword 
     new to create an object will increase the degree of coupling. No matter what framework to use, should reduce the use of new in order to reduce the degree of coupling. 
 2 . Reflection create objects 
( . 1 ) the newInstance method of class Class: 
     Class C = the Class.forName ( "com.briup.text" ); 
     the Hello H = (the Hello) c.newInstance (); 
 ( 2 the newInstance) class of the Constructor The method of 
     Class C = the Class.forName ( "com.briup.text" ); 
     the constructor constructor = c.getConstruction (); 
     the Hello H = (the Hello) Constructor.newInstance ();
  . 3 .clone (); 
   the Hello h1 of = new new the Hello ( ); 
   the Hello h2= (The Hello) h1.clone ();
  . 4 . Serialization mechanism (using serialization, to implement implements Serializable, the object is serialized to a disk, while the use of deserialization may be converted to an object on the disk memory) 
         the Hello H = new new the Hello (); 
         File F = new new File ( "hello.obj" ); 
     a FileOutputStream fos = new new a FileOutputStream (F); 
         the ObjectOutputStream OOS = new new the ObjectOutputStream (fos); 
         the FileInputStream FIS = new new the FileInputStream (F) ; 
         the ObjectInputStream OIS = new new the ObjectInputStream (FIS)
          // serialized object written to disk
         oos.writeObject (H);
          // deserialized 
         the Hello newHello = (the Hello) ois.readObject (); 


Note: In addition to create new objects, other types require casts 
classic face questions: you create an object must be new Please illustration

 

Guess you like

Origin www.cnblogs.com/fyscn/p/11493974.html