【Java】创建对象的几种方式
1.new创建新的对象
String str = new String("str");
2.通过Java的反射机制
public static void main(String[] args) throws Exception { // 获取类的Class对象 String str = (String)Class.forName("java.lang.String").newInstance();
System.out.println(str);
}
3.通过clone机制(克隆机制)
=========================Myclass.java===========================================
public class MyClass implements Cloneable {
private int value;
public MyClass(int value) {
this.value = value;
}
public int getValue() {
return value;
}
@Override
public MyClass clone() throws CloneNotSupportedException {
return (MyClass) super.clone();
}
}
=Main.java===================
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
MyClass obj1 = new MyClass(10);
MyClass obj2 = obj1.clone();
System.out.println(obj1.getValue()); // 输出:10
System.out.println(obj2.getValue()); // 输出:10
}
}
4.通过序列化机制
通过序列化机制来创建新的对象。序列化是将对象转换为字节流的过程,以便可以将其存储在磁盘上或通过网络传输。反序列化则是将字节流转换回对象的过程。
===================================================================================================
import java.io.*;
public class MyClass implements Serializable {
private int value;
public MyClass(int value) {
this.value = value;
}
public int getValue() {
return value;
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.writeInt(value * 2);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
value = in.readInt();
}
}
public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException {
MyClass obj1 = new MyClass(10);
FileOutputStream fileOut = new FileOutputStream("obj1.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(obj1);
out.close();
fileOut.close();
FileInputStream fileIn = new FileInputStream("obj1.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
MyClass obj2 = (MyClass) in.readObject();
in.close();
fileIn.close();
System.out.println(obj1.getValue()); // 输出:10
System.out.println(obj2.getValue()); // 输出:20
}
}
5、构造函数对象的newInstance()方法
类Constructor也有newInstance方法,这一点和Class有点像。从它的名字可以看出它与Class的不同,Class是通过类来创建对象,而Constructor则是通过构造器。依然使用第一个例子中的Test类。
public static void main(String[] args) throws Exception { Constructor<Test> constructor; try { constructor = Test.class.getConstructor(); Test t = constructor.newInstance(); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { e.printStackTrace(); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2023-10-12 vue中下载excel文件4种方法,2、通过 a 标签 download 属性结合 blob 构造函数下载发送post请求和后台poi返回文件流实现下载
2023-10-12 getMonth():获取当前月(注意:返回数值为0~11,需要自己+1来显示),0代表一月份,如果要显示2位月份,需要再前面补0,比如 20230512
2023-10-12 excel 导出 The maximum length of cell contents (text) is 32767 characters Excel单元格最大存储长度32767个字符,超长会报错,数据库中也有这个最大长度
2022-10-12 stringboot 报错 org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration 可能是jar版本不对 或者是maven 写错了
2022-10-12 2020版本idea version control 不见了 解决办法
2022-10-12 idea项目文件夹浅黄色编写代码无提示或看不到项目中的文件夹 找到项目下的.idea文件夹并删除 -> 重启ide打开前面的项目即可解决。
2022-10-12 IDEA 中的.iml文件和.idea文件夹 ( 隐藏方式 )