JAVA
사용자 정의 객체와 this
김윤지.
2023. 8. 8. 07:21
public class Main {
public static void main(String[] args) {
//정수, 논리, String으로 구성된 객체 생성 후 출력
홍길동 person = new 홍길동();
person.age = 22;
person.isMarried = false;
person.name = "길동";
person.introduce();
person.age++;
person.name = "길동2";
person.introduce();
}
}
class 홍길동 {
int age;
boolean isMarried;
String name;
void introduce() { //동사적인 형태(어떤 기능을 수행), 메서드(함수)
int age = this.age; //this : 리모컨 이름
String name = this.name;
boolean isMarried = this.isMarried;
System.out.println("---자기소개---");
System.out.printf("이름 : %s\n", name);
System.out.printf("혼인여부 : %b\n", isMarried);
System.out.printf("나이 : %d\n", age);
}
void run(){
System.out.println("달리다");
}
}
this : 해당 객체의 리모컨
introduce 내의 int age(1) = this.age(2); 서로 다른 것임!