
// 객체지향언어 OOP: object oriented programming
// OOP 순서: 클래스 작성(클래스) -> 객체 생성(메인 메서드) -> 객체의 사용(메인 메서드)
class EX_1 {
// 메인 메서드
public static void main(String() args) {
// OOP 순서
// 2. 객체 생성
Tv t; // Tv t: 참조형 변수의 형태 <->
// int myNum: 기본형 변수의 형태
// Tv 인스턴스를 참조하기 위한 참조변수 t를 선언.
// Tv 클래스 타입의 참조변수 t를 선언
t = new Tv(); // Tv라는 객체 생성
// Tv t = new Tv(); 변수 선언과 생성 동시에
// Tv 인스턴스를 생성한다. 만들어진 Tv 클래스 내의 변수와 메서드를 사용할 수 있도록 한다
// OOP 순서
// 3. 객체(의 멤버)의 사용
// 사용할 때에는 .멤버변수 / .멤버메서드
t.channel = 7; // Tv 인스턴스의 멤버변수 channel의 값을 7로 정함
t.channelDown(); // Tv 인스턴스이 메서드 channelDown()을 호출한다
System.out.println("현재 채널은" + t.channel + "입니다.");
}
}
// 클래스: 객체의 속성과 기능을 정의. 객체를 생성하기 위한 설계도
// 클래스의 구성: 데이터(변수) + 함수(메소드)
// 인스턴스 : 클래스로부터 생성된 객체 EX) TV 인스턴스 Instance
// OOP의 순서
// 1. (Tv) 클래스 작성
class Tv{ // Tv 인스턴스
// Tv의 속성(멤버변수)
String color; // 색상 = null;
boolean power; // 전원 상태
int channel; // 채널
// Tv의 기능(멤버 메서드)
void power() {power =! power;} // Tv를 켜거나 끄는 기능을 하는 메서드
void channelUp() {++channel;} // Tv의 채널을 높이는 기능을 하는 메서드
void channelDown() {--channel;} // Tv의 채널을 낮추는 기능을 하는 메서드
}

public class Ex_3 {
public static void main(String() args) {
// Card c1 = new Card() 이렇게 객체 생성 안해도 static은 바로 사용할 수 있음
// cv(class variable, static, 공유변수)는 객체 생성 없이 사용 가능
System.out.println("Card.width = " + Card.width);
System.out.println("Card.height = " + Card.height);
System.out.println();
// iv(instance variable)은 객체 생성 후에 사용 가능
Card c1 = new Card();
c1.type = "Heart";
c1.number = 7;
// iv(instance variable)은 객체 생성 후에 사용 가능
Card c2 = new Card();
c2.type = "Spade";
c2.number = 4;
System.out.println("c1은 " + c1.type + "," + c1.number + "이며," + "크기는 (" + Card.width +"," + Card.height +")");
System.out.println("c2은 " + c2.type + "," + c2.number + "이며," + "크기는 (" + Card.width +"," + Card.height +")");
System.out.println();
// Card.width = 50;
// Card.height = 80;
// 공유변수는 생선된 객체 중 하나만 바꿔도 모든 객체에 적용
Card.width = 50;
Card.height = 80;
System.out.println("c1은 " + c1.type + "," + c1.number + "이며," + "크기는 (" + Card.width +"," + Card.height +")");
System.out.println("c2은 " + c2.type + "," + c2.number + "이며," + "크기는 (" + Card.width +"," + Card.height +")");
}
}
class Card { // Card 클래스
// 인스턴스 변수 (Instance variable) : 개별속성
String type;
int number;
// 공유 변수 (static variable) : 인스턴스 변수 앞에 static을 붙인 것, 항상 유지되는 변수
// 전체 카드에 공통적으로 적용되는 변수
static int width = 100;
static int height = 250;
}
