// 标准对象实例化
Person person = new Person();
特点:
// 使用Class.newInstance()
Class<?> clazz = Class.forName("com.example.Person");
Person person = (Person) clazz.newInstance();
// 使用Constructor.newInstance()
Constructor<Person> constructor = Person.class.getDeclaredConstructor();
Person person = constructor.newInstance();
对比:
方式 | 是否需要无参构造 | 能否调用私有构造 | 异常处理 |
---|---|---|---|
Class.newInstance() | ✅ 必须 | ❌ | 包裹在反射异常中 |
Constructor.newInstance() | 支持任意参数构造 | ✅ 通过setAccessible(true) | 直接抛出InvocationTargetException |
class Person implements Cloneable {
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone(); // 浅拷贝
}
}
// 深拷贝示例
class Address implements Cloneable {
String city;
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class DeepPerson implements Cloneable {
Address address;
@Override
public Object clone() throws CloneNotSupportedException {
DeepPerson cloned = (DeepPerson) super.clone();
cloned.address = (Address) address.clone();
return cloned;
}
}
// 序列化对象
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.dat"))) {
oos.writeObject(new Person("Alice", 30));
}
// 反序列化创建新对象
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.dat"))) {
Person restoredPerson = (Person) ois.readObject();
}
注意事项:
Serializable
接口serialVersionUID
防止版本不一致问题// 汽车工厂示例
interface Car {
void drive();
}
class Sedan implements Car {
public void drive() { System.out.println("驾驶轿车"); }
}
class SUV implements Car {
public void drive() { System.out.println("驾驶SUV"); }
}
class CarFactory {
public static Car createCar(String type) {
switch(type) {
case "sedan": return new Sedan();
case "suv" : return new SUV();
default : throw new IllegalArgumentException();
}
}
}
// 使用示例
Car myCar = CarFactory.createCar("suv");
// 复杂对象创建
class Computer {
private String cpu;
private String ram;
private Computer(Builder builder) {
this.cpu = builder.cpu;
this.ram = builder.ram;
}
public static class Builder {
private String cpu;
private String ram;
public Builder cpu(String cpu) {
this.cpu = cpu;
return this;
}
public Builder ram(String ram) {
this.ram = ram;
return this;
}
public Computer build() {
return new Computer(this);
}
}
}
// 使用示例
Computer pc = new Computer.Builder()
.cpu("i7")
.ram("32GB")
.build();
// JDK中的经典实现
public final class LocalDateTime {
public static LocalDateTime now() {
// 实际实现逻辑
}
public static LocalDateTime of(int year, int month, int day, int hour, int minute) {
// 参数校验和构造逻辑
}
}
// 使用示例
LocalDateTime meetingTime = LocalDateTime.of(2023, 12, 25, 14, 30);
// 函数式接口实例化
Runnable task = () -> System.out.println("执行任务");
Comparator<String> lengthComparator = (s1, s2) -> s1.length() - s2.length();
// 等效匿名内部类
Runnable oldTask = new Runnable() {
@Override
public void run() {
System.out.println("传统实现方式");
}
};
<!-- XML配置方式 -->
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mydb"/>
<property name="username" value="root"/>
</bean>
// 注解配置方式
@Configuration
public class AppConfig {
@Bean
public DataSource dataSource() {
return new HikariDataSource(config);
}
}
创建方式 | 适用场景 | 优点 | 缺点 |
---|---|---|---|
new关键字 | 简单对象直接创建 | 直观高效 | 耦合度高 |
反射机制 | 动态加载类/框架开发 | 灵活性强 | 性能开销大 |
工厂方法 | 多态对象创建 | 解耦创建逻辑 | 增加类复杂度 |
建造者模式 | 复杂参数对象 | 参数灵活可读性好 | 代码量较大 |
克隆 | 对象复制场景 | 快速复制 | 深拷贝实现复杂 |
依赖注入 | 企业级应用 | 集中管理依赖关系 | 需要框架支持 |
InstantiationException
setAccessible(true)
)Object.clone()
执行浅拷贝readObject()
方法进行输入验证ObjectInputFilter
interface Subject {
void request();
}
class RealSubject implements Subject {
public void request() {
System.out.println("真实请求");
}
}
class DynamicProxyHandler implements InvocationHandler {
private Object target;
public DynamicProxyHandler(Object target) {
this.target = target;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("前置处理");
Object result = method.invoke(target, args);
System.out.println("后置处理");
return result;
}
}
// 创建代理对象
Subject proxyInstance = (Subject) Proxy.newProxyInstance(
RealSubject.class.getClassLoader(),
new Class[]{Subject.class},
new DynamicProxyHandler(new RealSubject())
);
// 危险操作示例(仅作了解,实际不推荐使用)
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
Unsafe unsafe = (Unsafe) theUnsafe.get(null);
Person person = (Person) unsafe.allocateInstance(Person.class);
注意:
根据具体业务需求选择合适的对象创建方式,在灵活性与性能之间找到最佳平衡点。对于核心高频创建的对象,建议进行性能压测。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。