Java创建对象的5种方式
程序员文章站
2022-05-29 09:17:57
1.使用 new 关键字(最常用): ObjectName obj = new ObjectName(); 2.使用反射的Class类的newInstance()方法: ObjectName obj = ObjectName.class.newInstance(); 3.使用反射的Construct ......
1.使用 new 关键字(最常用):
objectname obj = new objectname();
2.使用反射的class类的newinstance()方法:
objectname obj = objectname.class.newinstance();
3.使用反射的constructor类的newinstance()方法:
objectname obj = objectname.class.getconstructor.newinstance();
4.使用对象克隆clone()方法:
objectname obj = obj.clone();
5.使用反序列化(objectinputstream)的readobject()方法:
objectname obj = new objectinputstream(new fileinputstream(file_name)).readobject();
代码示例:
1.首先创建一个user类:
1 package com.example.demo.model; 2 3 import java.io.serializable; 4 import java.util.objects; 5 6 public class user implements serializable, cloneable { 7 private static final long serialversionuid = 1l; 8 private string id; 9 private string name; 10 private string phone; 11 12 public user(string id, string name, string phone) { 13 this.id = id; 14 this.name = name; 15 this.phone = phone; 16 } 17 18 public user() { 19 } 20 21 public string getid() { 22 return id; 23 } 24 25 public void setid(string id) { 26 this.id = id; 27 } 28 29 public string getname() { 30 return name; 31 } 32 33 public void setname(string name) { 34 this.name = name; 35 } 36 37 public string getphone() { 38 return phone; 39 } 40 41 public void setphone(string phone) { 42 this.phone = phone; 43 } 44 45 @override 46 public user clone() throws clonenotsupportedexception { 47 return (user) super.clone(); 48 } 49 50 @override 51 public boolean equals(object o) { 52 if (this == o) return true; 53 if (o == null || getclass() != o.getclass()) return false; 54 user user = (user) o; 55 return objects.equals(id, user.id) && 56 objects.equals(name, user.name) && 57 objects.equals(phone, user.phone); 58 } 59 60 @override 61 public int hashcode() { 62 return objects.hash(id, name, phone); 63 } 64 65 @override 66 public string tostring() { 67 return "user{" + 68 "id='" + id + '\'' + 69 ", name='" + name + '\'' + 70 ", phone='" + phone + '\'' + 71 '}'; 72 } 73 }
2.然后开始创建user对象:
1 package com.example.demo.practice; 2 3 import com.example.demo.model.user; 4 5 import java.io.fileinputstream; 6 import java.io.fileoutputstream; 7 import java.io.objectinputstream; 8 import java.io.objectoutputstream; 9 10 public class objectcreation { 11 private static final string file_name = "user.obj"; 12 13 public static void main(string[] args) throws exception { 14 //方式一 使用new关键字 15 user user = new user("1", "张三", "135****8457"); 16 system.out.println(user.tostring()); 17 18 //方式二 使用class类的newinstance()方法 19 user user2 = user.class.newinstance(); 20 user2.setname("李四"); 21 system.out.println(user2.tostring()); 22 23 //方式三 使用constructor类的newinstance()方法 24 user user3 = user.class.getconstructor().newinstance(); 25 user3.setname("王五"); 26 system.out.println(user3.tostring()); 27 28 //方式四 使用clone()方法,前提是被克隆类必须实现cloneable接口并且重写其clone()方法 29 user user4 = user.clone(); 30 system.out.println(user4.tostring()); 31 system.out.println(user == user4); 32 system.out.println(user.equals(user4)); 33 34 //方式五 使用反序列化方式,调用objectinputstream对象的readobject()方法,前提是类需要实现serializable接口 35 //序列化 36 objectoutputstream oos = new objectoutputstream(new fileoutputstream(file_name)); 37 oos.writeobject(user); 38 //反序列化 39 objectinputstream ois = new objectinputstream(new fileinputstream(file_name)); 40 user user5 = (user) ois.readobject(); 41 system.out.println(user5.tostring()); 42 } 43 }
输出结果如下:
user{id='1', name='张三', phone='135****8457'} user{id='null', name='李四', phone='null'} user{id='null', name='王五', phone='null'} user{id='1', name='张三', phone='135****8457'} false true user{id='1', name='张三', phone='135****8457'}
上一篇: C#中窗口关闭时没有取消事件订阅导致事件重复执行的解决方法
下一篇: 第一个SpringBoot项目