| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- Fultter
- 클론코딩
- Flutter
- coding
- 엔지니어
- YOLO
- 영화
- Ai
- 야호
- 딥러닝
- Type
- lightly
- ultralytics
- DART
- clone
- 논문리뷰
- 파이썬
- chatGPT
- GenAI
- 구글
- container
- image
- llm
- Python
- 리뷰
- 인공지능
- FunctionGemma
- Gemma
- docker
- Kubernetes
Archives
- Today
- Total
딥러닝 공부방입니다. 근데 이제 야매를 곁들인.
[Flutter] Constructor(생성자) 본문
기본적인 생성자 사용법은 다른 언어들과 크게 다르지 않다.
하지만 독특한 문법이 있어서 정리해본다.
단순한 사용법은 다음과 같다.
class Person {
String name;
int age;
// 기본
Person(String name, int age) {
this.name = name;
this.age = age;
}
// 조금 다르게
Person(String name, int age)
: this.name = name,
this.age = age;
// 조금 짧게
Person(this.name, this.age);
}
너무 방법이 다양해서 헷갈리는 것 같다..ㅠ
다른 언어에서는 인자의 개수나 타입을 다르게하여 여러개의 생성자를 만들 수 있다.
하지만 Dart 에서는 생성자 이름이 같으면 error 가 뜬다.
named constructor 를 통해 여러개를 만들 수 있다.
class Person {
String name;
int age;
Person(String name, int age)
: this.name = name,
this.age = age;
Person.young(String name)
: this.name = name,
this.age = 10;
Person.old(String name)
: this.name = name,
this.age = 50;
}
Person.young("홍길동")
하면 10살 짜리 홍길동 객체가 생성된다.
디폴트 값을 자유도 높게 여러개를 설정할 수 있는 기능이라고 보면 될 것 같다.
class Person {
String name;
int age;
Person(String name, int age)
: this.name = name,
this.age = age;
Person.young(String name)
: this.name = name,
this.age = 10;
Person.old(String name)
: this.name = name,
this.age = 50;
Person.hong() : this.old("홍길동");
}
이렇게 다른 생성자에서 다른 생성자를 사용할 수도 있다.
'지식 > Flutter' 카테고리의 다른 글
| [Ultralytics Hub Flutter Clone Coding] #1. 사이드 메뉴 만들기 (0) | 2023.02.11 |
|---|---|
| [Flutter] Stateless vs Stateful (0) | 2023.02.03 |
| [Flutter] 개요 (0) | 2023.01.29 |
| Dart 반복문 (0) | 2023.01.23 |
| Dart 조건 분기 (0) | 2023.01.23 |