Java中对象创建的几种方式
- 本文记录Java创建对象的六种方式,详细内容请查看代码
测试对象
| import java.io.Serializable; |
| |
| |
| |
| |
| |
| public class People implements Cloneable , Serializable { |
| |
| private int age; |
| |
| private String name; |
| |
| |
| public void setName(String name) { |
| this.name = name; |
| } |
| |
| public void setAge(int age) { |
| this.age = age; |
| } |
| |
| public String getName() { |
| return name; |
| } |
| |
| public int getAge() { |
| return age; |
| } |
| |
| |
| |
| |
| |
| |
| @Override |
| protected Object clone() throws CloneNotSupportedException { |
| return super.clone(); |
| } |
| |
| public People() { |
| System.out.println("调用了无参构造方法"); |
| } |
| |
| protected People(int age, String name) { |
| this.name = name; |
| this.age = age; |
| System.out.println("调用了有参构造方法"); |
| } |
| |
| @Override |
| public String toString() { |
| return "People{" + |
| "name='" + name + '\'' + |
| ", age='" + age + '\'' + |
| '}'; |
| } |
| |
| |
| } |
| |
测试示例
| import org.junit.jupiter.api.Test; |
| import sun.misc.Unsafe; |
| |
| import java.io.*; |
| import java.lang.invoke.MethodHandle; |
| import java.lang.invoke.MethodHandles; |
| import java.lang.invoke.MethodType; |
| import java.lang.reflect.Constructor; |
| import java.lang.reflect.Field; |
| import java.lang.reflect.InvocationTargetException; |
| |
| |
| |
| |
| |
| |
| |
| public class TestPeople { |
| |
| |
| |
| |
| |
| @Test |
| public void TestNew (){ |
| |
| People people = new People(); |
| System.out.println(people); |
| } |
| |
| |
| |
| |
| |
| |
| |
| @Test |
| public void TestClone () throws CloneNotSupportedException { |
| |
| |
| People clone = (People) new People(1,"张三").clone(); |
| |
| System.out.println(clone); |
| } |
| |
| |
| |
| |
| |
| |
| @Test |
| public void TestSerializable () throws IOException, ClassNotFoundException { |
| |
| People people = new People(1, "张三"); |
| System.out.println(people); |
| |
| |
| ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); |
| ObjectOutputStream objectInputStream = new ObjectOutputStream(byteArrayOutputStream); |
| objectInputStream.writeObject(people); |
| byte[] byteArray = byteArrayOutputStream.toByteArray(); |
| |
| |
| ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(byteArray)); |
| People deserializable = (People)inputStream.readObject(); |
| System.out.println("反序列化"+deserializable); |
| } |
| |
| |
| |
| |
| |
| @Test |
| public void testReflex () throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { |
| |
| |
| Constructor<People> declaredConstructor = People.class.getDeclaredConstructor(int.class, String.class); |
| |
| |
| |
| People people = declaredConstructor.newInstance(1, "张三"); |
| System.out.println(people); |
| |
| } |
| |
| |
| |
| |
| |
| @Test |
| public void TestMethodHandles () throws Throwable { |
| |
| |
| MethodHandle constructor = MethodHandles.lookup().findConstructor(People.class, MethodType.methodType(void.class, int.class, String.class)); |
| |
| People people = (People)constructor.invoke(1,"1" ); |
| System.out.println(people); |
| } |
| |
| |
| |
| |
| |
| |
| @Test |
| public void TestUnsafe () throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException { |
| |
| |
| Class<Unsafe> unsafeClass = Unsafe.class; |
| |
| |
| Constructor<Unsafe> declaredConstructor = unsafeClass.getDeclaredConstructor(); |
| |
| declaredConstructor.setAccessible(true); |
| Unsafe unsafe1 = declaredConstructor.newInstance(); |
| People people = (People)unsafe1.allocateInstance(People.class); |
| people.setName("张三"); |
| System.out.println(people); |
| |
| |
| Field theUnsafe = unsafeClass.getDeclaredField("theUnsafe"); |
| |
| theUnsafe.setAccessible(true); |
| Unsafe unsafe2 = (Unsafe)theUnsafe.get(null); |
| People people1 = (People)unsafe2.allocateInstance(People.class); |
| people.setName("李四"); |
| System.out.println(people1); |
| |
| } |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端