java5种常用的创建对象的方式
2018-11-27 08:30:50来源:博客园 阅读 ()
除了new可以创建对象,你还知道哪一些创建对象的方式(文中介绍了5种常用创建对象的方式,有补充请在评论区留言)
Java中5种创建对象的方法
-
使用new关键字
-
Class对象(有3种方式获取,任意选择一种即可)的newInstance()方法
-
构造函数对象的newInstance()方法
-
对象反序列化
-
Object对象的clone()方法
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 }
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 }
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 }
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 }
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 }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- JAVA中常用的类 2020-06-05
- 这 30 个常用的 Maven 命令你必须熟悉! 2020-05-28
- BAT程序员常用的开发工具,建议收藏! 2020-05-18
- Java中常用的包、类、接口以及util包下的常用类和方法(附: 2020-05-13
- 非常有用的并发控制-循环栅栏CyclicBarrier 2020-05-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash