java5种常用的创建对象的方式
除了new可以创建对象,你还知道哪一些创建对象的方式(文中介绍了5种常用创建对象的方式,有补充请在评论区留言)
Java中5种创建对象的方法
今天来聊一聊在Java创建对象的几种方法.
-
使用new关键字
-
Class对象(有3种方式获取,任意选择一种即可)的newInstance()方法
-
构造函数对象的newInstance()方法
-
对象反序列化
-
Object对象的clone()方法
1.使用new关键字
这是最常用也最简单的方式,也是我们最常用的创建方式,看看下面这个例子就知道了。
1 public class Object1 { 2 public static void main(String[] args) { 3 Student songyoulian = new Student("songyoulian"); 4 } 5 } 6 class Student{ 7 private String name; 8 public Student(String name) { 9 this.name = name; 10 }
2.Class对象(有3种获取Class对象的方式,任意选择一种即可)的newInstance()方法
首先我们通过Class.forName()动态的加载类的Class对象,然后通过newInstance()方法获得Student类的对象
1 public class Object2 { 2 public static void main(String[] args) { 3 String className = "com.zhuhuicong.objectspeak.Student"; 4 try { 5 Class<?> clazz = Class.forName(className); 6 Student student = (Student) clazz.newInstance(); 7 student.setName("songyoulian"); 8 System.out.println(student); 9 } catch (ClassNotFoundException e) { 10 e.printStackTrace(); 11 } catch (IllegalAccessException e) { 12 e.printStackTrace(); 13 } catch (InstantiationException e) { 14 e.printStackTrace(); 15 } 16 } 17 } 18 19 class Student{ 20 private String name; 21 public void setName(String name) { 22 this.name = name; 23 } 24 }
3.构造函数对象的newInstance()方法
类Constructor也有newInstance方法,用到了反射的知识.
1 import java.lang.reflect.Constructor; 2 import java.lang.reflect.InvocationTargetException; 3 4 public class Object4 { 5 public static void main(String[] args) { 6 try { 7 Constructor<Student> constructor = Student.class.getConstructor(String.class); 8 Student student = constructor.newInstance("songyoulian"); 9 System.out.println(student); 10 } catch (NoSuchMethodException e) { 11 e.printStackTrace(); 12 } catch (IllegalAccessException e) { 13 e.printStackTrace(); 14 } catch (InstantiationException e) { 15 e.printStackTrace(); 16 } catch (InvocationTargetException e) { 17 e.printStackTrace(); 18 } 19 } 20 } 21 22 class Student{ 23 private String name; 24 25 public Student(String name) { 26 this.name = name; 27 } 28 29 @Override 30 public String toString() { 31 return "Student{" + 32 "name='" + name + '\'' + 33 '}'; 34 } 35 }
4.对象反序列化
使用反序列化来获得类的对象,要求Student类要用到序列化Serializable接口.
1 public class Object5 { 2 public static void main(String[] args) { 3 //先将一个对象写出到指定文件中 4 try { 5 Student student = new Student("songyoylian"); 6 FileOutputStream out = null; 7 out = new FileOutputStream("E:/object.txt"); 8 ObjectOutputStream objectOutputStream = new ObjectOutputStream(out); 9 objectOutputStream.writeObject(student); 10 objectOutputStream.flush(); //清缓存 11 objectOutputStream.close(); //关闭流 12 } catch (FileNotFoundException e) { 13 e.printStackTrace(); 14 } catch (IOException e) { 15 e.printStackTrace(); 16 } 17 18 //将对象从文件中读出来 19 FileInputStream in = null; 20 try { 21 in = new FileInputStream("E:/object.txt"); 22 ObjectInputStream objectInputStream = new ObjectInputStream(in); 23 Student student2 = (Student) objectInputStream.readObject(); 24 System.out.println(student2); 25 } catch (FileNotFoundException e) { 26 e.printStackTrace(); 27 } catch (IOException e) { 28 e.printStackTrace(); 29 } catch (ClassNotFoundException e) { 30 e.printStackTrace(); 31 } 32 } 33 } 34 35 class Student implements Serializable{ 36 private String name; 37 38 public Student(String name) { 39 this.name = name; 40 } 41 42 @Override 43 public String toString() { 44 return "Student{" + 45 "name='" + name + '\'' + 46 '}'; 47 } 48 }
5.Object对象的clone()方法
Object对象中存在clone方法,它的作用是创建一个对象的副本。要求的是Student类必须继承Cloneable方法并且重写clone()方法.
1 public class Object6{ 2 public static void main(String[] args) { 3 Student student = new Student("songyoulian"); 4 try { 5 Student student2 = (Student) student.clone(); 6 System.out.println(student2); 7 } catch (CloneNotSupportedException e) { 8 e.printStackTrace(); 9 } 10 } 11 } 12 13 class Student implements Cloneable{ 14 private String name; 15 16 public Student(String name) { 17 this.name = name; 18 } 19 20 @Override 21 public String toString() { 22 return "Student{" + 23 "name='" + name + '\'' + 24 '}'; 25 } 26 27 @Override 28 protected Object clone() throws CloneNotSupportedException { 29 return super.clone(); 30 } 31 }