18일차 - 2023.1.18

문우림·2023년 1월 18일
0

Java

목록 보기
6/23

1. this

자바에서 this는 객체, 자기자신을 가리킴.

기능

this. : 자기자신의 객체 멤버를 가리킴. 현재 객체를 참조 하기 위한 용도.
this() : this생성자. 같은 클래스에 있는 다른 생서자를 호출하는 용도. 생성자는 첫줄에 선언애야 호출 가능(규칙).

//this의 이해

class Song {
	String title;
	String artist;

	Song() {
		System.out.println("디폴트 생성자 입니다.");
	}

	Song(String title, String artist) {
		this(); // this함수, 첫줄에서만 사용 가능
		this.title = title;
		this.artist = artist;
		show();
	}

	void show() {
		System.out.println(title + "::" + artist);
	}
}

public class TVTest {

	public static void main(String[] args) {
		Song song = new Song("Dancing Queen", "ABBA");
		song.show();
		System.out.println(song);
	}

}

this . title = title; 은 Song객체의 title 속성 = 매개변수 title 형태가 되어, Song객체의 속성이 값을 입력하게 된다.
여기서 this키워드는 객체 자신의 속성을 나타내게 되는 것이다.

this ( ); 클래스내의 다른 생성자 호출.
같은 이름의 메서드에 매개변수 타입, 개수를 다르게 한 생성자(오버로딩)

연습 예제

"10인치 입니다." 호출하는 코드

class TV {
	int inch;

	TV(int inch) {// 생성자 함수
		this.inch = inch;
	}

	int getSize() {
		return inch;
	}
}

public class TVTest {

	public static void main(String[] args) {
		TV tv = new TV(10);
		System.out.println(tv.getSize() + "인치 입니다.");

아래의 코드를 작성하시오.
TV tv = new TV(10,"blue");
TV tv2 = new TV(20,"blue");
System.out.println(tv.getSize() + "인치 입니다");
System.out.println(tv.getColor() + "색 입니다");
tv.show(); // 10 인치 bule 색 입니다.
tv.compareSize(tv); // 내가 작습니다.
tv2.compareSize(tv); // 내가 큽니다.

class TV {
	int size;
	String color;

	TV(int size, String color) {// 생성자 함수
		this.size = size;
		this.color = color;
	}

	int getSize() {// get메소드
		return size;
	}

	String getColor() {
		return color;
	}

	void show() {
		System.out.println(size + " 인치 " + color + " 색 입니다.");
	}

	void compareSize(TV tv) {
		if (this.size < tv.size) {
			System.out.println("내가 작습니다.");
		} else if (this.size == tv.size) {
			System.out.println("같습니다.");
		} else {
			System.out.println("내가 큽니다.");
		}
	}
}

public class TVTest {

	public static void main(String[] args) {
		TV tv = new TV(10, "blue");
		TV tv2 = new TV(20, "blue");

		System.out.println(tv.getSize() + "인치 입니다.");
		System.out.println(tv.getColor() + "색 입니다.");

		tv.show();

		tv.compareSize(tv2);
		tv2.compareSize(tv);

	}

}

[결과]
10인치 입니다.
blue색 입니다.
10 인치 blue 색 입니다.
내가 작습니다.
내가 큽니다.

2.import

import는 다른 패키지의 자바, 클래스 파일을 끌어올 때 쓴다.

  • 같은 패키지라면 객체화를 해서 꺼내다 쓰면 되지만, 다른 패키지라면 import를 사용함.

클래스 하나의 대한 import 선언

import com.wxfx.smart.Circle;

패키지 전체에 대한 import 선언

import com.wxfx.smart.Circle;
import com.fxmx.simple.Circle;

위의 경우 동일한 클래스명을 가진 패키지 2개를 import 선언하면 구분이 안되어 컴파일 오류 발생.

ex) com.wxfx.smart.Circle패키지안에 있는 Baby.class
com.fxmx.simple.Circle패키지안에 있는 Baby.class
어느 Baby.class인지 구분 안됨.

import com.wxfx.smart.*; // * = 패키지 전체를 불러오는 기호

com.wxfx.smart 패키지로 묶인 전체 클래스에 대한 페키지 선언.

3. OOP(Object Oriented Programming)객체 지향 프로그래밍 특징 (자바 언어의 70%)

① 정복 은닉 (hidden infomation)
隠蔽=外部のクラスから自クラスのフィールドへ直接アクセスできない**
② 상속 (inheritance)
継承する=あるクラスの機能を引き継ぎながら、新しいクラスを定義すること
③ 다형성 (polymorphism)
④ 캡슐화 (encapsulation)
이외) 클래스, 추상화

0개의 댓글