記事ディレクトリ
序文
Javaではオブジェクトを作成する方法がたくさんありますが、この記事では次の6つのオブジェクトの作成方法を詳しく紹介します。
1. 新しいキーワードを使用します
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. クラスの 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. コンストラクターの newInstance() メソッドを使用します。
コンストラクターの 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. デシリアライズを使用する
逆シリアル化は、バイト ストリームからオブジェクトを回復するプロセスです。シリアル化後、オブジェクトをファイルまたはネットワークに保存し、逆シリアル化によってオブジェクトに復元できます。
例は次のとおりです。
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 でオブジェクトを作成する 6 つの一般的な方法、つまりnew关键字、Class的newInstance()方法、Constructor的newInstance()方法、clone()方法、反序列化、工厂模式
などを紹介します。実際の開発では、ビジネスシナリオに応じてさまざまな作成方法を選択できます。