1. new + 构造器
例如:有一个Dog类,则创建对象方式如下
Dog d1 = new Dog(); |
2. Object的clone
注意:可能必须要实现Cloneable接口
public class Dog implements Serializable,Cloneable{ public static void main(String[] args) { Dog d1 = new Dog(); Dog d2 = null; try { d2 = (Dog) d1.clone(); System.out.println(d1); System.out.println(d2); System.out.println(d1==d2); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } } |
运行结果如下:
3. 反序列化
直译:就是将磁盘或者网络中的对象读取出来(反序列化前先得序列化)
public class 反序列化 { public static void main(String[] args) { try { File file=new File("D:/hello.txt"); //因为是读数据 FileInputStream fis=new FileInputStream(file); //因为它不能读对象,所以使用ObjectInputStream ObjectInputStream ois=new ObjectInputStream(fis); Object obj=ois.readObject(); //向下转型 if(obj instanceof Dog){ Dog dog=(Dog)obj; System.out.println(dog.getDogName()); } ois.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
4. 反射
java代码
方法一:
/** * 通过反射创建对象 * @author Administrator */ public class ObjectPoolFactory { //定义一个对象池,前面是对象名,后面是实际对象 private Map<String,Object> objectPool = new HashMap<>(); //定义一个创建对象的方法,该方法传入一个对象的字符串类名,可以根据类名生成java对象 private Object createObject(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException{ Class<?> clazz = Class.forName(className); return clazz.newInstance(); } //创建一个初始化对象池的方法,它会根据配置文件创建对象 private void initPool(String fileName) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException{ FileInputStream fis = null; try { fis = new FileInputStream(fileName); Properties pros =new Properties(); pros.load(fis); for (String names : pros.stringPropertyNames()) { //根据得到的属性名来创建对象 objectPool.put(names, createObject(pros.getProperty(names))); } } catch (FileNotFoundException e) { e.printStackTrace(); }finally{ if(fis != null){ fis.close(); } } } //根据属性名在连接池中获取对象 public Object getObject(String name){ return objectPool.get(name); } public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException { ObjectPoolFactory opf = new ObjectPoolFactory(); opf.initPool("src/obj.txt"); System.out.println(opf.getObject("c")); System.out.println(opf.getObject("a")); System.out.println(opf.getObject("b")); } } |
配置文件如下:(配置文件中的Dog对象可以自己创建)
obj.txt
a=com.reflect.domain.Dog b=javax.swing.JFrame c=java.util.Date |
注意:每一个类后面都不能加空格,不能会报如下异常
还有一个问题就是,这个配置文件的后缀名也可以是.properties也不会报错
方法二:
public class CreateFrame { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { Class<?> clazz = Class.forName("javax.swing.JFrame"); Constructor constructor = clazz.getConstructor(String.class); Object obj = constructor.newInstance("测试窗口"); System.out.println(obj); } } |
5. 工厂方法
interface IProduct { public void productMethod(); } class Product implements IProduct { public void productMethod() { System.out.println("产品"); } } interface IFactory { public IProduct createProduct(); } class Factory implements IFactory { public IProduct createProduct() { return new Product(); } } /** * 通过工厂方法来创建对象 * @author Administrator * */ public class Client { public static void main(String[] args) { IFactory factory = new Factory(); IProduct prodect = factory.createProduct(); System.out.println(prodect); prodect.productMethod(); } } |