티스토리 뷰
클래스
객체지향 프로그래밍에서 핵심적인 요소로 현실 세계의 모델을 프로그래밍 언어로 표현할 때 사용한다.
자바와 선언 방식이 비슷하다.
main() {
var p1 = new Position();
p1.show();
}
class Position {
int x = 0;
int y = 0;
show() {
print("x: $x, y:$y");
}
}
기본 생성자
생성자를 통해서 객체를 생성할 때 필요한 초기값을 설정할 수 있다. 생성자도 함수처럼 네임드 함수, 선택 매개변수 함수 등의 방법으로 정의할 수 있다.
main() {
var p1 = new Position(5, 4);
p1.show();
}
class Position {
int x = 0;
int y = 0;
Position([int x = 0, int y = 0]) {
this.x = x;
this.y = y;
}
show() {
print("x: $x, y:$y");
}
}
이름이 있는 생성자
Dart는 여러 종류의 생성자를 만드려면 새로운 이름으로 생성자를 계속 만들어야 한다.
main() {
var p1 = Position(5, 4);
var p2 = Position.fromPosition(p1);
var p3 = Position.fromMap({"x":5});
p2.show();
p3.show();
}
class Position {
int x = 0;
int y = 0;
Position([int x = 0, int y = 0]) {
this.x = x;
this.y = y;
}
Position.fromPosition(Position position) {
this.x = position.x;
this.y = position.y;
}
Position.fromMap(Map<String, int> map) {
this.x = map["x"] != null ? map["x"]! : 0;
this.y = map["y"] != null ? map["y"]! : 0;
}
show() {
print("x: $x, y:$y");
}
}
초기화 리스트
C++ 처럼 생성자가 실행되기 전에 값을 할당할 수 있다.
main() {
var p1 = Position(5, 4);
p2.show();
p3.show();
}
class Position {
int x = 0;
int y = 0;
Position([int x = 0, int y = 0]): this.x = x, this.y = y {
}
show() {
print("x: $x, y:$y");
}
}
리다이렉팅
기존 생성자를 재활용할 경우이다. Body를 가질 수 없으며 다른 생성자와 달리 ;을 붙여야한다.
main() {
var p1 = Position(5, 4);
var p2 = Position.fromPosition(p1);
var p3 = Position.fromMap({"x":5});
p2.show();
p3.show();
}
class Position {
int x = 0;
int y = 0;
Position([int x = 0, int y = 0]): this.x = x, this.y = y {
}
Position.fromPosition(Position position) : this(position.x, position.y);
Position.fromMap(Map<String, int> map) : this(map["x"] == null ? 0 : map["x"]!, map["y"] == null ? 0 : map["y"]!);
show() {
print("x: $x, y:$y");
}
}
this 생성자
생성자 인자에 this를 사용하여 코드를 더 간결하게 작성할 수 있다.
main() {
var p1 = Position(5, 4);
p1.show();
}
class Position {
int x = 0;
int y = 0;
// Position(this.x, this.y); body가 필요없는 경우 ;으로 대체할 수 있음
Position(this.x, this.y) {
}
show() {
print("x: $x, y:$y");
}
}
상수 생성자
클래스의 맴버 변수가 모두 final일 경우 사용할 수 있다. 성능 측면에서 이점이 있음
main() {
/*
new를 사용해서 클래스를 만들면 새로운 객체가 생성되고
const를 사용해서 클래스를 만들면 내부 final값이 모두 같은 경우 하나의 객체를 공유한다.
*/
var p0 = Position(5, 4);
var p1 = new Position(5, 4);
var p2 = const Position(5, 4);
var p3 = const Position(5, 4);
p0.show();
p1.show();
p2.show();
p3.show();
}
class Position {
final int x;
final int y;
const Position([this.x = 0, this.y = 0]);
show() {
print("$hashCode => (x: $x, y:$y)");
}
}
554857637 => (x: 5, y:4)
80050908 => (x: 5, y:4)
531188670 => (x: 5, y:4)
531188670 => (x: 5, y:4)
Process finished with exit code 0
팩토리 생성자
디자인 패턴 중 팩토리 패턴을 사용하여 클래스르 만들 수 있다. 팩토리 생성자르 사용하면 기본 생성자를 사용할 수 없다.
class Job {
Job.init();
factory Job(String type) {
switch(type) {
case "Student":
return Student();
case "Teacher":
return Teacher();
default:
return Job.init();
}
}
}
class Student extends Job {
Student() : super.init();
}
class Teacher extends Job {
Teacher() : super.init();
}
class Employee extends Job {
Employee() : super.init();
}
'Flutter > Dart' 카테고리의 다른 글
Flutter Dart - 함수 (0) | 2022.01.14 |
---|---|
Flutter Dart - 분기처리(조건문, 반복문) (0) | 2022.01.08 |
Flutter Dart - 연산자 (0) | 2022.01.08 |
Flutter Dart - 변수(2) (0) | 2022.01.08 |
Flutter Dart - 변수(1) (0) | 2022.01.08 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Widget
- TDD
- 코루틴
- ConcatAdapter
- Flowable
- ViewModelProvider
- 연산자
- ConcatAdapter.Config
- 함수
- Flutter
- viewmodel
- Kotlin
- Exception
- isActive
- DSL
- rxjava
- Android
- gradle
- commit
- Coroutine
- observable
- DART
- CancellationException
- 클린코드
- git
- 보이스카우트 규칙
- 클린 코드
- ViewModelStoreOwner
- clean code
- null
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함