在本篇文章中,将介绍5种创建JAVA对象的方式 。类是创建对象的基本模板,接下来将介绍5种不同的方式,利用Java类来实例化Java对象 。
文章插图
【5种创建Java对象的方式】
1. 使用new关键字? 采用new关键字实例化对象是Java中最为常见的方法,下面是采用new关键字实例化对象的示例 。在开始之前,我们先准备好一个Java类:Student.java 。
package com.ramostear.oops;public class Student{private String name;private String nickname;public Student(String name,String nickname){this.name = name;this.nickname = nickname;}public String getName(){return name;}public void setName(String name){this.name = name;}public String getNickname(){return nickname;}public void setNickname(String nickname){this.nickname = nickname;}}
接下来,我们通过new关键字来创建几个学生:package com.ramostear.oops;public class TestNewStudent{public static void main(String[] args){Student zhangsan = new Student("Zhangsan","张三");Student lisi = new Student();lisi.setName("Lisi");lisi.setNickname("李四");}}
2. 使用Class类的newInstance()方法? 首先,我们可以通过Class.forName()方法动态加载目标类,然后再调用newInstance()方法,动态实例化对象 。同理,在开始之前,我们需要准备一个目标类:Student.java 。package com.ramostear.oops;public class Student{private String name = "Zhangsan";private String nickname = "张三";public Student(){super();}public Student(String name,String nickname){super();this.name = name;this.nickname = nickname;}public String getName(){return name;}public void setName(String name){this.name = name;}public String getNickname(){return nickname;}public void setNickname(String nickname){this.nickname = nickname;}}
接下来,将演示如何通过newInstance()方法来创建新的对象:package com.ramostear.oops;public class TestUseNewInstanceMethod{public static void main(String[] args){try{String className = "com.ramostear.oops.Student";Class clz = Class.forName(className);Student student = (Student) clz.newInstance();System.out.println("student name :"+ student.getName()+", nickname: "+student.getNickname());}catch(InstantiationException | IllegalAccessException | ClassNotFoundException e){e.printStackTrace();}}}
运行main()方法,将输出如下信息:student name :Zhangsan, nickname:张三
3. 使用Constructor.newInstance()方法? 与Class类的newInstance()方法相似,我们还可以使用java.lang.reflect.Constructor类的newInstance()方法来创建新的对象 。同样,我们先准备一个目标类:package com.ramostear.oops;public class Student{private String name = "Constructor";private String nickname = "构造器";public Student(){super();}public Student(String name,String nickname){super();this.name = name;this.nickname = nickname;}public String getName(){return name;}public void setName(String name){this.name = name;}public String getNickname(){return nickname;}public void setNickname(String nickname){this.nickname = nickname;}}
接下来,我们看看如何使用Constructor的newInstance()方法创建新的对象:package com.ramostear.oops;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;public class UseConstructorNewInstanceMethod{public static void main(String[] args){Constructor<Student> studentConstructor;try{studentConstructor = Student.class.getConstructor();Student student = studentConstructor.newInstance();System.out.println("student name :"+ student.getName()+", nickname: "+student.getNickname());}catch(InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException |NoSuchMethodException | SecurityException e){e.printStackTrace();}} }
执行main()方法,将在控制台输出如下信息:student name:Constructor, nickname:构造器
4. 使用反序列化? 如果要使用反序列化创建对象,则目标类首先要实现Serializable接口 。Serializable是一个标记接口 。在本次示例中,我们先创建一个Student对象,并将其保存到data.txt文件中,然后在通过反序列化操作,读取data.txt中的数据,并创建新的对象 。package com.ramostear.oops;import java.io.Serializable;public class Student implements Serializable{ private String name; private String nickname; public Student() {super(); } public Student(String name, String nickname) {super();this.name = name;this.nickname = nickname; } public String getName() {return name; } public void setName(String name) {this.name = name; } public String getNickname() {return nickname; } public void setNickname(String nickname) {this.nickname = nickname; }}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 冬至如何养生 5种蔬菜最适合冬天吃
- Java中数据传输加密与签名那些事
- Java随机产生中文昵称
- JDBC基础整理
- JavaScript 数据类型与类型判断详解
- 微信广告怎么创建计划?创建流程有哪些?
- 图解Java内存区域
- 夏季5种阔腿裤穿法,舒适、时髦又凉爽
- Java虚拟机工作原理详解
- 12个提高 JavaScript 技能的概念