java 五种创建对象的方法
2016-11-29 09:41
323 查看
一: 背景
作为java开发,创建对象是最基本最常用的方法。我整理一下五中常用的创建对象的常用方法,如有不全,欢迎大家补全。
二: 五种常见的创建对象的方法
2.1)使用new关键字
2.2)使用clone方法
注意使用这个方法的话要给实体类加上Cloneable接口
2.3)使用序列化和反序列化
注意使用这个方法的话要给实体类加上Serializable接口
2.4)Class对象的newInstance
2.5)Class对象的Constructor方法
三:代码举例
3.1描述说明:
一共两个文件Student和Test;Student是实体类,实现了Serializable和Cloneable接口,主要有两个成员变量名字和年龄,Test主要是个测试类,实现了五中对象创建的方法。
3.2 具体代码
a)Student.java代码
package com.lyg.test;
import java.io.Serializable;
/**
*@author luyg
*@function: 测试实体类 . Serializable:序列化时候用到 ; Cloneable复制对象的时候用到
*/
public class Student implementsSerializable , Cloneable{
privateString name ;
privateint age ;
publicString school="张三中学";
publicStudent(String name, int age) {
super();
this.name= name;
this.age= age;
}
publicStudent() {
this("",0);
}
publicString getName() {
returnname;
}
publicvoid setName(String name) {
this.name= name;
}
publicint getAge() {
returnage;
}
publicvoid setAge(int age) {
this.age= age;
}
@Override
protectedObject clone() {
try{
returnsuper.clone();
}catch (Exception e) {
//TODO: handle exception
}
returnnull;
}
@Override
publicString toString() {
return"Student [name=" + name + ", age=" + age + "]";
}
}
b)Test.java测试类
package com.lyg.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;
/**
*@author luyg
*@function: 测试创建对象类,五种常见的方法
* 1)new
* 2)clone
* 3)序列化
* 4)Class类newInstance
* 5)Class类里Constructor构造方法
*/
public class Test {
publicstatic void main(String[] args) {
try{
System.out.println("************方式一:使用new关键字创建对象**************");
Studentstudent1 = new Student("张三", 12);
System.out.println(student1);
System.out.println("************方式二:使用clone复制对象***************");
/***
* 克隆实际上是系统通过当前对象的属性,完成复制出来一个内容相同的对象,
* 但是新对象和克隆出来的对象是两个不同的对象.
*/
Studentstudent2= (Student) student1.clone();
System.out.println(student2);
System.out.println("*********方式三:使用序列化和范序列化创建对象对象*********");
StringfileName="D:\\student.obj";
ObjectOutputStreamoos = new ObjectOutputStream(new FileOutputStream(fileName));
oos.writeObject(student1);
ObjectInputStreamois = new ObjectInputStream(new FileInputStream(fileName));
Studentstudent3 = (Student) ois.readObject();
System.out.println(student3);
System.out.println("********方式四:通过Class对象的newInstance创建对象*******");
Classclass1 = Class.forName("com.lyg.test.Student");
//Classclass =Student.class;
Studentstudent4= (Student) class1.newInstance();
student4.setName("李四");
student4.setAge(13);
System.out.println(student4);
System.out.println("********方式四:通过Class对象反射构造方法创建对象*******");
Constructorconstructor = class1.getConstructor(String.class , int.class);
Studentstudent5=(Student) constructor.newInstance("王五",15);
System.out.println(student5);
}catch (Exception e) {
e.printStackTrace();
}
}
}
c)执行效果
************方式一:使用new关键字创建对象**************
Student [name=张三, age=12]
************方式二:使用clone复制对象***************
Student [name=张三, age=12]
*********方式三:使用序列化和范序列化创建对象对象*********
Student [name=张三, age=12]
********方式四:通过Class对象的newInstance创建对象*******
Student [name=李四, age=13]
********方式四:通过Class对象反射构造方法创建对象*******
Student [name=王五, age=15]
作为java开发,创建对象是最基本最常用的方法。我整理一下五中常用的创建对象的常用方法,如有不全,欢迎大家补全。
二: 五种常见的创建对象的方法
2.1)使用new关键字
2.2)使用clone方法
注意使用这个方法的话要给实体类加上Cloneable接口
2.3)使用序列化和反序列化
注意使用这个方法的话要给实体类加上Serializable接口
2.4)Class对象的newInstance
2.5)Class对象的Constructor方法
三:代码举例
3.1描述说明:
一共两个文件Student和Test;Student是实体类,实现了Serializable和Cloneable接口,主要有两个成员变量名字和年龄,Test主要是个测试类,实现了五中对象创建的方法。
3.2 具体代码
a)Student.java代码
package com.lyg.test;
import java.io.Serializable;
/**
*@author luyg
*@function: 测试实体类 . Serializable:序列化时候用到 ; Cloneable复制对象的时候用到
*/
public class Student implementsSerializable , Cloneable{
privateString name ;
privateint age ;
publicString school="张三中学";
publicStudent(String name, int age) {
super();
this.name= name;
this.age= age;
}
publicStudent() {
this("",0);
}
publicString getName() {
returnname;
}
publicvoid setName(String name) {
this.name= name;
}
publicint getAge() {
returnage;
}
publicvoid setAge(int age) {
this.age= age;
}
@Override
protectedObject clone() {
try{
returnsuper.clone();
}catch (Exception e) {
//TODO: handle exception
}
returnnull;
}
@Override
publicString toString() {
return"Student [name=" + name + ", age=" + age + "]";
}
}
b)Test.java测试类
package com.lyg.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;
/**
*@author luyg
*@function: 测试创建对象类,五种常见的方法
* 1)new
* 2)clone
* 3)序列化
* 4)Class类newInstance
* 5)Class类里Constructor构造方法
*/
public class Test {
publicstatic void main(String[] args) {
try{
System.out.println("************方式一:使用new关键字创建对象**************");
Studentstudent1 = new Student("张三", 12);
System.out.println(student1);
System.out.println("************方式二:使用clone复制对象***************");
/***
* 克隆实际上是系统通过当前对象的属性,完成复制出来一个内容相同的对象,
* 但是新对象和克隆出来的对象是两个不同的对象.
*/
Studentstudent2= (Student) student1.clone();
System.out.println(student2);
System.out.println("*********方式三:使用序列化和范序列化创建对象对象*********");
StringfileName="D:\\student.obj";
ObjectOutputStreamoos = new ObjectOutputStream(new FileOutputStream(fileName));
oos.writeObject(student1);
ObjectInputStreamois = new ObjectInputStream(new FileInputStream(fileName));
Studentstudent3 = (Student) ois.readObject();
System.out.println(student3);
System.out.println("********方式四:通过Class对象的newInstance创建对象*******");
Classclass1 = Class.forName("com.lyg.test.Student");
//Classclass =Student.class;
Studentstudent4= (Student) class1.newInstance();
student4.setName("李四");
student4.setAge(13);
System.out.println(student4);
System.out.println("********方式四:通过Class对象反射构造方法创建对象*******");
Constructorconstructor = class1.getConstructor(String.class , int.class);
Studentstudent5=(Student) constructor.newInstance("王五",15);
System.out.println(student5);
}catch (Exception e) {
e.printStackTrace();
}
}
}
c)执行效果
************方式一:使用new关键字创建对象**************
Student [name=张三, age=12]
************方式二:使用clone复制对象***************
Student [name=张三, age=12]
*********方式三:使用序列化和范序列化创建对象对象*********
Student [name=张三, age=12]
********方式四:通过Class对象的newInstance创建对象*******
Student [name=李四, age=13]
********方式四:通过Class对象反射构造方法创建对象*******
Student [name=王五, age=15]
相关文章推荐
- java 五种创建对象的方法
- java创建对象五种方法
- Java中五种不同方法的创建对象
- java 五种创建对象的方法
- Java中创建(实例化)对象的五种方式
- Java创建对象的4种方法
- 学习effective java-4创建和销毁对象之利用私用构造方法强化类的非实例化
- java创建对象实例的三种方法
- Java创建对象的方法
- java创建对象的四种方法
- 笔记2-----创建一个java实例对象的方法
- JNI学习笔记(四)JNI中本地语言创建Java对象并且访问具体方法(附例子)
- Java创建对象实例的三种方法
- java创建对象的四种方法
- JAVA创建对象方法
- java中创建对象的方法
- Java创建对象的四种方法
- java创建一个子类对象是会调用父类的构造方法会不会创建父类
- Java中创建对象的四种方法
- java创建对象的四种方法