Day 18 (23.01.18)

Jane·2023년 1월 18일
0

IT 수업 정리

목록 보기
18/124

1. import 사용하기 (패키지 불러오기)

import com.fxmx.simple.*;
// com.fxmx.simple 패키지에 있는 모든 것을 불러오기

public class CircleObjectTest {

	public static void main(String[] args) {
//		com.fxmx.simple.Circle c1 = new com.fxmx.simple.Circle();
		Circle c1 = new Circle();
		com.wxfx.smart.Circle c2 = new com.wxfx.smart.Circle();
	}

}
  • 각각의 Java 파일은 package 이름이 존재한다.
    package 이름이 없는 것은 컴파일러가 default라는 이름으로 정해준다. (package default;)
  • 패키지를 import해주면 직접 패키지 이름까지 지정해주지 않아도 Circle 클래스를 생성할 수 있다.
import com.fxmx.simple.*;
import com.wxfx.smart.*;

public class CircleObjectTest {

	public static void main(String[] args) {
		com.fxmx.simple.Circle c1 = new com.fxmx.simple.Circle();
		com.wxfx.smart.Circle c2 = new com.wxfx.smart.Circle();
//		Circle c1 = new Circle();
//		Circle c2 = new Circle();
	}

}
  • 다수의 패키지 안에 같은 이름의 패키지가 2개 이상 존재하면 Error, 같은 이름의 클래스가 존재하는 경우에는 꼭 package 이름을 적어두도록 한다.

2. JRE System Library

  • String 데이터타입을 긁어서 F3을 누르면 String.class 를 볼 수 있다.
    (경로 :: JRE System Library > java.base > java.lang > String.class)
  • 우리는 Java에 내장되어 있는 Library 파일을 이용하여 프로그램을 만들고 있다.
    자바 프로젝트 안의 'JRE System Library'에 들어 있는 각종 패키지와 파일들을 확인해볼 수 있다. (Math.class, Character.class, Boolean.class 등)

3. this

3-1. this로의 접근

  • 객체가 생성되었을 때의 자기 자신을 표현하고 싶을 때 사용한다
  • 이름이 다르면 굳이 this를 쓰지 않아도 되지만, 같은 이름을 쓰고 싶어하는 경우가 있기 때문에 this를 사용한다.

3-2. this()

  • this()로 생성자 호출 가능
  • 같은 클래스 안에 다른 생성자를 호출할 경우에 사용한다.
class Song2 {
	String title;
	String artist;
	
	Song2(){
		System.out.println("Default Constructor");
	}
	
	Song2(String title, String artist){
		this(); // 생성자 함수 실행, 아랫줄에서 실행할 수 없다.
		this.title = title;
		this.artist = artist;
        show(); // 코딩 상에서는 this를 생략할 수도 있다.
	}
	
	void show() {
		System.out.println("Title : " + title + ", Artist : " + artist);
	}
}

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

[Console]
Default Constructor // this() 실행
Title : Dancing Queen, Artist : ABBA // this.show() 실행

4. Java 프로그램 예제 (생성자 함수, this 사용)

class TV {
	int size;
	String color;

	public TV(int size, String color) {
		this.size = size;
		this.color = color;
	}

	public int getSize() {
		return size;
	}

	public String getColor() {
		return color;
	}

	public void show() {
		System.out.println(getSize() + "인치, " + getColor() + "색입니다");
//		System.out.println(this.size + "인치, " + this.color + "색입니다");
	}
	
	public void compareSize(TV tv) {
		if(this.size > tv.getSize()) {
			System.out.println("내가 더 크다.");
		} else if(this.size == tv.getSize()) {
			System.out.println("크기가 같다.");
		} else {
			System.out.println("내가 더 작다.");
		}
	}
}

public class Hello {

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

		tv.show();
		
		TV tv2 = new TV(20, "blue");
		
		tv.compareSize(tv2); // 10인치는 20인치보다 작다
		tv2.compareSize(tv); // 20인치는 10인치보다 크다
		
	}

}

tv.compareSize(tv); 도 될까?

  • 된다. ("크기가 같다"로 콘솔에 출력)
  • 매개변수는 'tv가 참조하고 있는 주소'를 찾아가면서 비교한다.
  • main() 함수에 있는 객체는 함수 안에 그대로 있지만(main() 함수가 끝날 때까지 살아있음), 매개변수로 들어온 것은 compareSize() 함수가 끝나면 사라진다.

5. 객체 지향 언어 (Object Oriented Language / Program)

  • 반대 개념 : 절차 지향 언어 (Procedure Oriented Language / Program)
  • 객체 지향 언어의 주요 키워드
    • 정보 은닉 (Hidden Information)
    • 상속 (Inheritance)
    • 다형성 (Polymorphism)
    • 캡슐화 (Encapsulation)
  • 이외의 특징 : 클래스, 추상화 등
profile
velog, GitHub, Notion 등에 작업물을 정리하고 있습니다.

0개의 댓글