소개
Java 에서 객체를 생성하는 것은 클래스의 청사진을 사용할 수 있게 해주는 객체 지향 프로그래밍 (OOP, Object-Oriented Programming) 의 기본 개념입니다. 객체는 실제 엔티티를 나타내며 실제 엔티티와 같은 속성을 가집니다. 이 Lab 에서는 클래스의 객체를 생성하는 방법을 배우겠습니다.
Java 에서 객체를 생성하는 것은 클래스의 청사진을 사용할 수 있게 해주는 객체 지향 프로그래밍 (OOP, Object-Oriented Programming) 의 기본 개념입니다. 객체는 실제 엔티티를 나타내며 실제 엔티티와 같은 속성을 가집니다. 이 Lab 에서는 클래스의 객체를 생성하는 방법을 배우겠습니다.
첫 번째 단계는 속성을 가진 엔티티를 나타내는 클래스를 생성하는 것입니다. 예를 들어, 색상, 브랜드 이름, 연료 유형과 같은 속성을 가진 자동차의 실제 엔티티를 나타내는 Car라는 클래스를 생성해 보겠습니다.
public class Car {
private String carColor;
private String brand;
private String fuelType;
public void start() {
//code to start the car
}
public void stop() {
//code to stop the car
}
}
Java 에서 객체를 생성하려면 생성자와 함께 new 키워드를 사용합니다. 다음 예제에서는 new 키워드를 사용하여 myCar라는 객체를 생성합니다. myCar 객체를 사용하여 start() 및 stop() 메서드를 호출합니다.
public class Main {
public static void main(String[] args) {
//Creating an object using the new keyword
Car myCar = new Car();
//Calling methods using the object
myCar.start();
myCar.stop();
}
}
코드를 실행하려면 터미널에서 다음 명령을 실행하십시오.
javac Main.java && java Main
Class 클래스의 newInstance() 메서드를 사용하여 객체를 생성할 수도 있습니다. 다음 예제에서는 newInstance() 메서드를 사용하여 Student 클래스의 newStudent 객체를 생성합니다.
class Student {
private String name;
private int id;
}
public class Main {
public static void main(String[] args) throws Exception {
//Creating an object using the newInstance() method
Student newStudent = Student.class.newInstance();
//Assigning values to the object
newStudent.name = "John";
newStudent.id = 123;
}
}
코드를 실행하려면 터미널에서 다음 명령을 실행하십시오.
javac Main.java && java Main
clone() 메서드는 해당 클래스의 객체가 이미 하나 이상 생성된 경우 객체를 생성하는 데 사용할 수 있습니다. 다음 예제에서는 clone() 메서드를 사용하여 Student 클래스의 newStudent 객체를 생성합니다.
class Student implements Cloneable {
private String name;
private int id;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Main {
public static void main(String[] args) throws Exception {
//Creating an object
Student student = new Student();
student.name = "John";
student.id = 123;
//Cloning the object
Student newStudent = (Student) student.clone();
}
}
코드를 실행하려면 터미널에서 다음 명령을 실행하십시오.
javac Main.java && java Main
직렬화 및 역직렬화를 사용하여 객체를 생성하는 것은 클래스가 직렬화 가능해야 하는 객체를 생성하는 방법입니다. 다음 예제에서는 직렬화 및 역직렬화를 사용하여 Student 클래스의 newStudent 객체를 생성합니다.
import java.io.Serializable;
class Student implements Serializable {
private String name;
private int id;
}
public class Main {
public static void main(String[] args) throws Exception {
//Creating an object
Student student = new Student();
student.name = "John";
student.id = 123;
//Serialization of the object
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("file.obj"));
out.writeObject(student);
out.close();
//Deserialization of the object
ObjectInputStream in = new ObjectInputStream(new FileInputStream("file.obj"));
Student newStudent = (Student) in.readObject();
in.close();
}
}
코드를 실행하려면 터미널에서 다음 명령을 실행하십시오.
javac Main.java && java Main
Java 에서 클래스의 객체를 생성하는 방법:
new 키워드를 사용하여 객체를 생성합니다.Class 클래스의 newInstance() 메서드를 사용하여 객체를 생성합니다.clone() 메서드를 사용하여 객체를 생성합니다.