본문 바로가기

그 땐 Programming Languages했지/그 땐 JAVA했지

[self-study/생활코딩] 객체지향프로그래밍(OOP) | ①클래스&인스턴스 ②변수와 메소드 ③클래스 ④인스턴스 ⑤static ⑥생성자와 this

728x90

참고자료: Youtube - 생활코딩 JAVA1

 

남의 클래스 & 남의 인스턴스
import java.io.FileWriter;
import java.io.IOException;

public class OthersOOP {

	public static void main(String[] args) throws IOException {
    	//class: System, Math, FileWriter
        //instance: f1, f2
		System.out.println(Math.PI);
		System.out.println(Math.floor(1.8));
		System.out.println(Math.ceil(1.8));
		
		FileWriter f1 = new FileWriter("data.txt");
		f1.write("Hello");
		f1.write(" Java");
		f1.close();

		FileWriter f2 = new FileWriter("data2.txt");
		f2.write("Hello");
		f2.write(" Python");
		f2.close();
	}

}

📌 인스턴스는 클래스의 복제본 이라고 생각하면 된다.
👉🏻Math와 같은 클래스는 값을 구하는 일회성의 작업을 할 때 용이하기 때문에 인스턴스를 만들지 않아도 된다.
👉🏻FileWriter와 같은 클래스는 추가적으로 글을 쓰는 작업을 해야하므로 인스턴스를 만들어 작업하는 것이 더 좋은 방법이다.

 

변수와 메소드
public class MyOOP {
	public static String delimiter = "";
	public static void main(String[] args) {
		delimiter = "----";
		printA();
		printA();
		printB();
		printB();

		delimiter = "****";
		printA();
		printA();
		printB();
		printB();
	}

	public static void printA() {
		System.out.println(delimiter);
		System.out.println("A");
		System.out.println("A");
	}

	public static void printB() {
		System.out.println(delimiter);
		System.out.println("B");
		System.out.println("B");
	}

}

📌전역변수(public)를 만들어 main 메소드에서 접근 가능하게 만들어준다.

👉🏻변수를 선언해 코드를 간결하게 만들고 유지보수가 더욱 쉬워지게 할 수 있다.

👉🏻option+command+M: 해당 코드를 메소드로 만들어준다.

 

클래스

📌연관된 메소드들을 모아준다.

👉🏻접근제어자 public: 클래스에 붙는 public은 파일 이름과 똑같은 클래스를 구현할 때 한 번만 등장한다.

 

인스턴스

📌클래스의 복제본

public class MyOOP {
	public static void main(String[] args) {
		Print p1 = new Print();
		p1.delimiter = "----";
		p1.A();
		p1.A();
		p1.B();
		p1.B();
		
		Print p2  = new Print();
		p2.delimiter = "****";
		p2.A();
		p2.A();
		p2.B();
		p2.B();
	}
}
class Print {
	public String delimiter = "";
	
	public void A() {
		System.out.println(delimiter);
		System.out.println("A");
		System.out.println("A");
	}

	public void B() {
		System.out.println(delimiter);
		System.out.println("B");
		System.out.println("B");
	}

}

👉🏻new라는 키워드를 이용해 복제한다.

👉🏻인스턴스를 사용하면 클래스를 입맛에 맞게 변형시켜 사용할 수 있다.

 

static
class Foo{
	//static이 붙어있으면 class 소속 
	public static String classVar = "I class var";
	//static이 붙어있지 않으므로 instance 소속 
	public String instanceVar = "I instance var";
	
	public static void classMethod() {
		System.out.println(classVar); // ok
	//	System.out.println(instanceVar); // error
	}
	public void instanceMethod() {
		//모두 접근 가능 
		System.out.println(classVar); // ok
		System.out.println(instanceVar); // ok
	}
}

public class StaticApp {
	
	public static void main(String[] args) {
		System.out.println(Foo.classVar); //class 변수 접근 가능 
	//	System.out.println(Foo.instanceVar); //instance 변수 접근 불가능
		
		Foo f1 = new Foo();
		Foo f2 = new Foo();
		
		//인스턴스로는 클래스 변수와 인스턴스 변수 모두 접근가능하다. 
		System.out.println(f1.classVar); //I class var
		System.out.println(f1.instanceVar); //I instance var
		
		//static이 붙어있는 classVar는 클래스, 인스턴수 둘 다 접근해도 값이 변해있다.
		f1.classVar = "changed by f1";
		System.out.println(Foo.classVar); //changed by f1
		System.out.println(f2.classVar); //changed by f1
		
		//static이 붙어있지 않은 instanceVar는 클래스로 접근할 때만 값이 변해있다.
		f1.instanceVar = "changed by f1";
		System.out.println(f1.instanceVar); //changed by f1
		System.out.println(f2.instanceVar); //I instance var
	}
}

📌static클래스와 인스턴스를 잇는 링크와 비슷한 개념. 인스턴스 속 변수를 바꾸면 클래스 속 변수도 변하고, 클래스 속 변수가 변하면 인스턴스의 변수도 변한다.

👉🏻인스턴스는 인스턴스를 통해서 고안된 변수이므로 클래스를 통해 접근이 불가능하다.

 

생성자와 this
class Print {
	public String delimiter = "";
	//생성자 
	public Print(String delimiter) {
		//이 인스턴스의 delimiter에 받은 문자열을 넣는다.
		this.delimiter = delimiter;
	}
	
	public void A() {
		System.out.println(this.delimiter);
		System.out.println("A");
		System.out.println("A");
	}

	public void B() {
		System.out.println(this.delimiter);
		System.out.println("B");
		System.out.println("B");
	}

}

📌생성자: 클래스의 이름과 똑같은 메소드이다.

👉🏻인스턴스가 만들어졌을 때, 즉각적으로 어떠한 일을 처리하거나 특정 값을 주입하여 사용할 경우에 활용한다. 

👉🏻static이나 void 관련 수식어를 붙이지 않는다.

👉🏻this: 우리가 생성한 인스턴스를 가리킨다.

728x90