在Java中,创建对象可以用多种形式,本文详解下列六种创建对象的形式:
1. 应用new关键词
new关键词是Java中常用创建对象的形式。根据启用类的构造函数,new关键词创建对象一个对象。
实例如下所示:
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
Person person = new Person("小亮", 18);
2. 应用Class的newInstance()方式
Class的newInstance()方法来在运行中建立一个类的全新案例。它等效于应用new运算符,可是词法更为动态性。
实例如下所示:
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
try {
Person person = Person.class.newInstance();
person.name = "小亮";
person.age = 18;
} catch (Exception e) {
e.printStackTrace();
}
3. 应用Constructor的newInstance()方式
Constructor的newInstance()方法来在运行中建立一个类的全新案例,并可以传到结构函数的参数。这种方法比Class的newInstance()方式更加高效,因为能选择不同对象。
实例如下所示:
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
try {
Constructor<Person> constructor = Person.class.getConstructor(String.class, int.class);
Person person = constructor.newInstance("小亮", 18);
} catch (Exception e) {
e.printStackTrace();
}
4. 应用clone()方式
clone()方法来创建对象的一个团本,并可以重新写过clone()方式来完成深复制。
实例如下所示:
public class Person implements Cloneable {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
Person person = new Person("小亮", 18);
Person clone = null;
try {
clone = (Person) person.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
5. 应用反序列化
反序列化是把目标从字节流中恢复得全过程。根据实例化后,能把阿里云oss到文件和网络里,然后根据反序列化的形式修复成目标。
实例如下所示:
public class Person implements Serializable {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
Person person = new Person("小亮", 18);
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.dat"));
oos.writeObject(person);
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.dat"));
Person clone = (Person) ois.readObject();
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
6. 应用工厂模式
工厂模式能将对象的创建与使用耦合。根据界定一个对象加工厂,能够更加高效地造成目标。
实例如下所示:
public interface Animal {
String getName();
}
public class Cat implements Animal {
@Override
public String getName() {
return "Cat";
}
}
public class Dog implements Animal {
@Override
public String getName() {
return "Dog";
}
}
public class AnimalFactory {
public Animal createAnimal(String type) {
switch (type) {
case "Cat":
return new Cat();
case "Dog":
return new Dog();
default:
throw new IllegalArgumentException("Unsupported animal type: " type);
}
}
}
AnimalFactory factory = new AnimalFactory();
Animal cat = factory.createAnimal("Cat");
Animal dog = factory.createAnimal("Dog");
汇总
文中阐述了Java中六种比较常见的创建对象的形式,分别为应用new关键词、Class的newInstance()方式、Constructor的newInstance()方式、clone()方式、反序列化、工厂模式等。在具体设计中,能够根据实际的需求场景选择不同建立方法。
本站是一个以CSS、JavaScript、Vue、HTML为中心的前端开发技术网址。我们的使命是为众多前端工程师者提供全方位、全方位、好用的前端工程师专业知识和技术服务。 在网站上,大家可以学到最新前端开发技术,掌握前端工程师最新发布的趋势和良好实践。大家提供大量实例教程和实例,让大家可以快速上手前端工程师的关键技术和程序。 本站还提供了一系列好用的工具软件,帮助你更高效地开展前端工程师工作中。公司提供的一种手段和软件都要经过精心策划和改进,能够帮助你节约时间精力,提高研发效率。 此外,本站还拥有一个有活力的小区,你可以在社区里与其它前端工程师者沟通交流技术性、交流经验、处理问题。我们坚信,街道的能量能够帮助你能够更好地进步与成长。 在网站上,大家可以寻找你需要的一切前端工程师网络资源,使您成为一名更加出色的网页开发者。欢迎你添加我们的大家庭,一起探索前端工程师的无限潜能!