phpclass/객체정보
{추상클래스}4.PHP
방글24
2000. 12. 28. 15:03
추상함수(???)
PHP에는 추상함수나 추상클래스라는 개념이 없습니다. 있다고 가정하고 추상함수와 추상클래스를 흉내내어 보겠습니다. 아래와 같이 추상함수를 선언합니다.
[code php;gutter:false]
function breathe() {} // 추상함수(메소드의 몸체는 없음)
[/code]
추상클래스(???) 정의
클래스에 하나 이상의 추상함수가 있다면 그것을 추상클래스라고 가정하겠습니다.
[code php;gutter:false]
//
// 추상클래스 animal
//
class animal {
function breathe() {} // 추상함수
} [/code]
// 추상클래스 animal
//
class animal {
function breathe() {} // 추상함수
} [/code]
추상함수의 오버라이드
하위클래스를 정의할 때는 추상함수 breathe()를 오버라이드해야 합니다.
[code php;gutter:false]
//
// 추상클래스 animal로부터 파생된 일반클래스 fish
//
class fish extends animal {
//
// 추상함수 breathe()의 오버라이드
//
function breathe() {
print "Bubbling...\n";
}
} [/code]
// 추상클래스 animal로부터 파생된 일반클래스 fish
//
class fish extends animal {
//
// 추상함수 breathe()의 오버라이드
//
function breathe() {
print "Bubbling...\n";
}
} [/code]
추상클래스 예제
[code php;gutter:false]
//
// 추상클래스 animal
//
class animal {
function eat() { // 일반메소드
print "Eating...\n";
}
function breathe() {} // 추상함수
}
//
// 추상클래스 animal로부터 파생된 일반클래스 fish
//
class fish extends animal {
//
// 추상함수 breathe()의 오버라이드
//
function breathe() {
print "Bubbling...\n";
}
}
//
// 프로그램의 최초 진입점
//
$my_fish = new fish;
$my_fish->breathe(); [/code]
// 추상클래스 animal
//
class animal {
function eat() { // 일반메소드
print "Eating...\n";
}
function breathe() {} // 추상함수
}
//
// 추상클래스 animal로부터 파생된 일반클래스 fish
//
class fish extends animal {
//
// 추상함수 breathe()의 오버라이드
//
function breathe() {
print "Bubbling...\n";
}
}
//
// 프로그램의 최초 진입점
//
$my_fish = new fish;
$my_fish->breathe(); [/code]
이 경우에 프로그램은 클래스 fish에 정의된 breathe()함수를 수행하게 됩니다. 따라서 "Bubbling..."이 화면에 표시됩니다.