09.16 제6장 객체지향프로그래밍2
[클래스변수, 인스턴스 변수]
⦁static : 변화가 필요없을 때
⦁지역변수
[클래스 메소드, 인스턴스 메소드]
⦁인스턴스 공통으로 사용시 static을 붙여 클래스 변수, 클래스 메서드로 정의한다.
⦁클래스 메소드는 인스턴스 없이 호출 가능
⦁클래스명.클래스 메소드
⦁주의
-static 메소드는 인스턴스 메소드를 호출할 수 없다.
-static 메소드는 인스턴스 변수를 사용할 수 없다.
-역의 경우는 성립한다. 인스턴스 메소드/변수는 static 메소드/변수를 사용가능
[초기화]
⦁기본값
- 8가지의 기본형 데이터
: int(0), short(0), long(0L), byte(0), double(0.0), float(0.0f), boolean(false),char(\u0000)
⦁명시적 초기화
⦁초기화블럭(클래스,인스턴스)
⦁생성자
⦁초기화의 순서 : 기본값 → 명시적초기화 → 초기화블럭 → 생성자
⦁맴버변수 초기화
[생성자]
⦁this() 함수 : 다른 생성자를 호출 할 때 사용한다.
⦁this : 인스턴스 자체를 가리키는 참조변수로 인스턴스의 주소가 저장되어 있고 모든 인스턴스 메소드에 지역변수로 숨겨진 채로 존재한다.
⦁super() : 상속에서 사용되는 생성자 함수 호출
class CardTest { public static void main(String[] args) { Card c1 = new Card(); //c1.kind = "Heart" ; //Card.width = 150; //c1.number = 7; c1.printCard();
System.out.println(c1.kind + ":" +c1.number+ "," +Card.width+ "," +Card.height);
Card c2 = new Card("Spade", 5); } }
class Card { String kind = "Heard"; //명시적 초기화 int number=1; {number =2;} // 인스턴스 초기화 블럭 final static int width = 100; static int height = 150; static { height = 200; } //클래스 초기화 블럭 void printCard() { System.out.println(kind + ":" + number); }
//생성자 Card() { //kind = "Spade"; number = 3; this("Spade", 3) }
//특정 종류와 숫자를 갖는 카드를 원하는 생성자 Card(String kind, int number) { this.kind = kind;//=뒤의 변수는 매개변수 this.number = number; }
} |
[상속관계] is-a 상하관계
⦁class Circle VS class Point
⦁class Circle extends Point {
int radius;
}
class Point {
int x;
int y;
}
[포함관계] has-a 수평관계
⦁class Point {
int x;
int y;
}
class Circle {
Point origin;
int radius;
}
[관계 결정]
⦁Circle is a Point
⦁Circle has a Point (√) Better
⦁SportCar is a car (√) Better
⦁SportCar has a car
class Tvtest { public static void main(String[] args) { int x; Tv tv = new Tv();
tv.channel = 7; tv.channelDown(); tv.color = "Yellow"; tv.power = false; tv.power(); System.out.println("현재 채널 : " + tv.channel); tv.printChannel();
CaptionTv ctv = new CaptionTv(); ctv.displayCaption("show caption title"); ctv.caption = true; ctv.displayCaption("Caption is on now. Hello"); } }
class Tv { String color; boolean power; int channel; void power() { power = !power; } void channelUp() { channel++; } void channelDown() { channel--; } void printChannel() { System.out.println("현재 채널 : " + channel); }
Tv() { } //이걸 써주고 재정의 해주면 오류가 안난다. Tv (boolean power, int channel) //재정의 { this.power = power; this.channel = channel; } //이것만 사용할 경우 내부적으로 생성자가 이미 있기에 오버로딩이 되기에 오류
}
class CaptionTv extends Tv { boolean caption; void displayCaption(String text) { if(caption) System.out.println(text); }
} |
[오버라이딩]
⦁오버로딩 : 유사한 기능이지만 새로운 메소드를 정의하는 것
⦁오버라이딩 : 상속받은 메소드의 내용을 변경 수정하는 것
class PointTest { public static void main(String[] args) { Point3D p1 = new Point3D(); System.out.println(p1.getLocation()); } }
class Point { int x; int y; String getLocation() { return " x : " + x + " y : " + y; } }
class Point3D extends Point { int z; String getLocation() //{ return " x : " + x + " y : " + y + " z : " + z; } //오버라이딩 된다 {return super.getLocation() + " z : " + z; } //super를 이용한 오버라이딩 } |
[패키지]
⦁패키지는 디렉토리(폴더)
⦁String 클래스
-import java.lang.String
-실제 사용하려면 import를 붙여주어야 한다.
-그런데 우리는 이를 쓰지 않고도 사용한다. WHY? 자동으로 import 되어 있다.