1/28 oop_this

이 글은 2019년 홍익대학교 자바 겨울 특강 수업을 메모한 것이다.

당시에는 에버노트에 메모해놨었기 때문에,

티스토리 블로그를 시작하면서 백업해둔다.


 

 
this : 자기 참조 객체
 
멤버 변수에 멤버 메소드의 값을 넣어주는 역할을 한다???
 
자기자신안에 있는 참조객체를 하기 위해 this를 쓴다
 
package chapter6;
//this
//현재 자신의 객체를 참조하는 참조변수
//현 객체의 멤버 변수나 멤버 메소드를 참조하기 위한 참조변수
//this()
//1. 생성자 메소드 안에서 다른 생성자 메소드 호출만 가능
//2. 생성자 메소드 안에서 첫줄만 사용가능
//3. 생성자 메소드 이외의 멤버 메소드나 main() 메소드 사용 불가능
class car {
    int speed;
    int wheel;
    String carname;
    
    car() {
        
    }
    
    car(String carname) {
        this.carname = carname;
    }
    
    car(int wheel) {
        //this(wheel);
        this.wheel = wheel;
    }
    
    car(int wheel, int speed) {
        //this(wheel);
        //this.wheel = wheel;
        this.speed = speed;
    }
    
    void speedup() {
        speed++;
    }
    
    void speedup(int speed) {
        this.speed += speed; // ts = ts + s
    }
    
    void speeddown() {
        speed--;
    }
    
    void stop() {
        speed = 0;
    }
}
public class oop_this {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        car mycar = new car(4);
        System.out.println(mycar.wheel + " : " + mycar.speed);
        
        car yourcar = new car(4, 10);
        yourcar.speedup(8);
        System.out.println(yourcar.wheel + " : " + yourcar.speed);
        
        //garbage collecting
        mycar = null;
        yourcar = null;
        System.gc();
    }
}

'┝ 개발 언어 > ┎ JAVA' 카테고리의 다른 글

1/30 접근제어자 private  (0) 2022.03.01
1/28 클래스 변수와 인스턴스 변수  (0) 2022.03.01
1/28 오버로딩  (0) 2022.03.01
1/28 생성자 파라미터 아규먼트  (0) 2022.03.01
1/25 객체지향 클래스 메소드  (0) 2022.03.01