
new라는 키워드를 사용인스턴드화라고 표현public class Person() {
...
}
public class Main {
public static void main(String[] args) {
Person personA = new Person(); // ✅ 첫번째 객체 생성
Person personB = new Person(); // ✅ 두번째 객체 생성
}
}
1. 속성, 2. 생성자, 3. 기능 부분으로 나누어 진다.public class Main {
public static void main(String[] args) {
// 1. 객체 생성
Person personA = new Person();
Person personB = new Person();
// 2. ✅ 객체를 통해 접근 personA 의 name
System.out.println(personA.name);
// 3. ✅ 객체를 통해 접근 personB 의 name
System.out.println(personB.name);
}
}
public class Person {
String name;
int age;
String address;
Person() {} // ❌ 기본생성자 제거됨
Person(String name, int age) { // ✅ 새로운 생성자(조립설명서)
this.name = name;
this.age = age;
}
}
메서드라고 표현class Person {
...
// ✅ 사람의 소개 기능
void introduce() {
System.out.println("안녕하세요.");
System.out.println("나의 이름은 " + this.name + "입니다.");
System.out.println("나이는 " + this.age + "입니다.");
}
// ✅ 사람의 더하기 기능
int sum(int a, int b) {
int result = a + b;
return result;
}
}
public class Main {
public static void main(String[] args) {
Person personA = new Person("gygim", 10);
personA.introduce(); // ✅ personA 객체 introduce() 호출
Person personB = new Person("Steve", 5);
}
}
게터란 클래스의 속성을 가져올 때 사용되는 기능세터란 객체의 속성을 외부에서 설정할 수 있게 해주는 기능