dart (flutter) 문법 - 클래스. class, abstract, implements, factory
클래스 기본 예제.
dart에서는 단일 상속만 가능하다.
class Point {
double? x;
double? y;
// 이름 없는 생성자. 하나만 가능하다.
Point(double x, double y) {
this.x = x;
this.y = y;
}
// 이름있는 생성자.
Point.init() : x=0, y=0 {
}
// 이름 없는 생성자 호출.
Point.init2() : this(0, 0) {
}
int GetV() { return 1; }
}
class Point3D extends Point {
final bool i3D = true;
double z = 0;
// 생성시 부모 클래스 생성자 호출.
Point3D(double x, double y, double z)
: super(x, y) {
this.z = z;
}
// 부모의 함수를 재정의 할 수 있다.
@override int GetV() { return 2; }
}
void main() {
var p1 = Point(2, 2);
var p2 = new Point3D(2, 2, 2);
print('The type of a is ${p1.runtimeType}');
}
추상 클래스.
인스턴스화 되지 않는 클래스로 주로 인터페이스 정의에 사용된다.
abstract class AbstractContainer {
void updateChildren(); // 추상 함수.
}
class ClassA extends AbstractContainer {
// 추상 함수 정의.
void updateChildren() {}
}
implements (인터페이스)
상속받지 않고 다른 클래스의 인터페이스를 지원할 수 있다.
여러 클래스의 인터페이스를 지원합니다.
class ClassTest {
test() {}
}
class ClassTest2 {
test2() {}
}
class ClassA implements ClassTest, ClassTest2 {
// 인터페이스 정의.
test() {}
test2() {}
}
factory
다음 페이지에 정리되어 있습니다.
참조.
댓글
댓글 쓰기