目录
使用 Constructor 类的 newInstance 方法
面试回答
使用 new 关键字
这是我们最常用的、也是最简单的创建对象的方式,通过这种方式我们还可以调用任意的构造函数(无参的和有参的)。
User user=new User();
使用反射机制
运用反射手段,调用 java.lang.Class
或者 java.lang.reflect.Constructor
类的 newInstance()
实例方法。
使用 Class 类的 newInstance()
方法
可以使用 Class
类的 newInstance
方法创建对象。这个 newInstance
方法调用无参的构造函数创建对象。
User user = User.class.newInstance();
User user = (User) Class.forName("com.chiyi.model.User").newInstance();
使用 Constructor 类的 newInstance 方法
和 Class
类的 newInstance
方法很像,java.lang.reflect.Constructor
类里也有一个 newInstance
方法 可以创建对象。我们可以通过这个 newInstance
方法调用有参数的和私有的构造函数。
Constructor constructor=User.class.getConstructor();
User user= (User) constructor.newInstance();
这两种newInstance
方法就是大家所说的反射。事实上 Class 的 newInstance
方法内部调用 Constructor
的 newInstance
方法。
使用 clone 方法
无论何时我们调用一个对象的 clone
方法,jvm
就会创建一个新的对象,将前面对象的内容全部拷贝进去。用 clone
方法创建对象并不会调用任何构造函数。
要使用 clone
方法,我们需要先实现 Cloneable
接口并实现其定义的 clone
方法。如果只实现了 Cloneable
接口,并没有重写 clone
方法的话,会默认使用 Object
类中的 clone
方法,这是一个 native
的方法。
public class CloneTest implements Cloneable{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public CloneTest(String name,int age){
super();
this.name=name;
this.age=age;
}
public static void main(String[] args) {
try {
CloneTest cloneTest=new CloneTest("Tango",17);
CloneTest copyClone=(CloneTest) cloneTest.clone();
System.out.println("newClone:"+cloneTest.getName());
System.out.println("copyClone:"+copyClone.getName());
cloneTest.setName("Change Name");
System.out.println("newClone:"+cloneTest.getName());
System.out.println("copyClone:"+copyClone.getName());
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
}
使用反序列化
当我们序列化和反序列化一个对象,jvm
会给我们创建一个单独的对象。其实反序列化也是基于反射实现的。
public static void main(String[] args) {
User user=new User();
user.setName("Tango");
user.setAge(17);
System.out.println(user);
ObjectOutputStream oos=null;
try {
oos=new ObjectOutputStream(new FileOutputStream("tempFile"));
oos.writeObject(user);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
IOUtils.closeQuietly(oos);
}
File file=new File("tempFile");
ObjectInputStream ois=null;
try {
ois=new ObjectInputStream(new FileInputStream(file));
User newUser=(User) ois.readObject();
System.out.println(newUser);
} catch (IOException e) {
throw new RuntimeException(e);
}catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}finally {
IOUtils.closeQuietly(ois);
try {
FileUtils.forceDelete(file);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
使用方法句柄
通过使用方法句柄,可以间接地调用构造函数来创建对象
public static void main(String[] args) throws Throwable {
// 定义构造函数的方法句柄类型为 void 类型,无参数
MethodType methodType=MethodType.methodType(void.class);
// 获取构造函数的方法句柄
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle constructor = lookup.findConstructor(User.class, methodType);
// 使用方法句柄调用构造函数创建对象
User user=(User) constructor.invoke();
}
使用了 MethodHandles.Lookup.findConstructor()
方法获取构造函数的方法句柄,然后通过 invoke
方法调用构造函数来创建对象。
使用 Unsafe 分配内存
在 Java 中,可以使用 sun.misc.Unsafe
类来进行直接的内存操作,包括内存分配和对象实例化。然而,需要注意的是,sun.misc.Unsafe
类是 Java 的内部 API,它并不属于 Java 标准库的一部分,也不建议直接在生产环境使用。
public static void main(String[] args) throws Exception {
Field field=Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
Unsafe unsafe= (Unsafe) field.get(null);
// 获取 User 类的字段偏移量
long nameOffset = unsafe.objectFieldOffset(User.class.getDeclaredField("name"));
long ageOffset = unsafe.objectFieldOffset(User.class.getDeclaredField("age"));
// 使用 allocateInstance 方法创建对象,不会调用构造函数
User user = (User) unsafe.allocateInstance(User.class);
// 使用 putObject 方法设置字段的值
unsafe.putObject(user,nameOffset,"Tango");
unsafe.putObject(user,ageOffset,17);
System.out.println(user);
}
这种方式有以下几个缺点:
- 不可移植性:Unsafe 类的行为在不同的 Java 版本和不同的 JVM 实现中可能会有差异,因此代码在不同的环境下可能会出现不可移植的问题。
- 安全性问题:Unsafe 类的功能是非常强大和危险的,可以绕过 Java 的安全机制,可能会导致内存泄漏、非法访问、数据损坏等安全为。
- 不符合面向对象的原则:Java 是一门面向对象的语言,鼓励使用构造函数和工厂方法来创建对象,以确保对象的正确初始化和维护对象的不变性。