Dart๋ Typed Language์ด๋ค. ์ด๋ค ํ๋์ ์ธ์คํด์ค์ type์ ๊ทธ๊ฒ์ ํด๋์ค์ด๋ค.
Constructor ์ดํดํ๊ธฐ
๋คํธ์ ์์ฑ์๋ ์๋ฐ์ธ์ด์ ๋๊ฐ์ ๊ฐ๋ ์ด๋ค.
์ธ์คํด์ค๋ฅผ ์ด๊ธฐํํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๋ฉ์๋์ด๊ณ , ํด๋์ค๋ ๋ฐ๋์ ์์ฑ์๊ฐ ํ์ํ๋ค ๋ฑ,,, ๋น์ทํจ
๊ฐ์ ํ(ํด๋์ค)๋ก ์ฐ์ด๋์ผ๋๊ฐ ๊ตฌ์กฐ๋ ๊ฐ๋๋ผ๋ ๊ฐ ํน์ง๊ฐ์ ๋ค๋ฅธ ์ธ์คํด์ค๋ฅผ ๋ง๋ค์ด๋ด๊ธฐ ์ํ ํจ์๋ค ๋ผ๊ณ ์๊ฐํ์
์ด์ ๊ฐ๋จํ ์ฝ๋๋ฅผ ๋ด ์๋ค.
< ๊ธฐ๋ณธ์ ์ธ ํด๋์ค ๋ง๋ค๊ธฐ >
class Student{
String name = 'jiny';
int age = 21;
void sayhi(){
print("hi my name is $name");
}
}
void main(){
var student = Student();
print(student.age);
}
์ค์ํ Tip
- Class์์ property๋ฅผ ์ ์ธํ ๋๋ ํ์
์ ์ฌ์ฉํด์ ์ ์ํ๋ค.
- ๋ฌผ๋ก ์ฌ๊ธฐ์์๋ var something ์ด๋ ๊ฒ ์ ์ธํ ์๋ ์์ง๋ง, ์ด๋ ๊ฒ ํด๋์ค๋ฅผ ์์ฑํ ๋๋ ๊ผญ ํ์ ์ ๋ช ์ํด์ฃผ๋๋ฐฉ์์ ์จ์ผํ๋ค๊ณ ํจ!
- ๋น๊ต) function๋ด์์ variable์ ์ฌ์ฉํ ๋์๋ ๋ฐ๋ก ํ์ ๋ช ์ ์์ด var์ ์ฌ์ฉํ๋ค.(์ด ๋ํ ๊ถ์ฅ์ฌํญ)
- Student class์ sayhi๋ฉ์๋ ๋ด์์ name์ด๋ผ๋ ํ๋๋ณ์๋ฅผ ์ธ ๋ $this.name์ด๋ผ๊ณ ์ฐ์ง ์์๋ ๋จ
- this.name์ด๋ผ๊ณ ์จ๋ ๋์์ ํ์ง๋ง dart์์๋ Class method๋ด์์ this.๋ฅผ ์์ฐ๋๊ฑธ ๊ถ๊ณ ํจ.
- ๋จ, sayhi๋ฉ์๋์ name์ด๋ผ๋ ๋๊ฐ์ ์ด๋ฆ์ ๊ฐ์ง ๋ณ์๊ฐ ์กด์ฌํ๋ค๋ฉด, class์ ๋ณ์ name์์ ์๋ ค์ฃผ๊ธฐ ์ํด this.name์ด๋ผ๊ณ ์จ์ผํ๊ธด ํจ
- Student์ instance๋ฅผ ๋ง๋ค ๋ Student(); ์์ new ํค์๋๋ฅผ ๋ถ์ด์ง ์์๋ ๋๋ค. not required
(์ฌ๊ธฐ์ student์ธ์คํด์ค์ type์ Student)
์ด ์ฝ๋๋ ๋คํธ๊ฐ ์์์ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ๋ง๋ค์ด์คฌ๊ธฐ ๋๋ฌธ์ ๋ฌธ๋ฒ์ ์ค๋ฅ๋ ์๋ ์ฝ๋.
์ฐ๋ฆฌ๊ฐ ์์ฑ์ ์๋ง๋ค๋ฉด ๋คํธ๊ฐ ์์์ ๋ง๋ค์ด์ค. ์ด๊ฒ๋ ์๋ฐ๋ ๋๊ฐ๋ค.
๊ทผ๋ฐ ์ด๊ฑด ์ข ์ด์ํ๋ค.
๋งค๋ฒ ์ด ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ์์ฑํ ๋๋ง๋ค ๋๊ฐ์ ์์ฑ์ ๊ฐ์ง ๋๋ง ์ฐ์ด๋ด๋๊น...
๊ทธ๋์ ์ผ๋จ ์ด๋ ๊ฒ ์ฐ๋ฉด ์๋ฌ๊ฐ ๋ฌ๋ค.
class Student {
String name;
int age;
Student(String name, int age); //์ด๊ฒ Constructor method
}
void main() {
var student = Student('jiny', 21);
print(student.age);
}
์๋ฌ์ด๋ฆ: Non-nullable instance field 'age' must be initialized.
Flutter์ null safety๊ธฐ๋ฅ(*) ๋๋ฌธ์ ํด๋์ค ๋ด์์ ์์ฑ๋ ๋ณ์์๋ ๋ฐ๋์ ๊ฐ์ ํ ๋นํด์ผ ํ๋ค.
๊ณต์ ๋ฌธ์๋ ํ ๋ฒ ์ฝ์ด๋ณด์:
Diagnostic messages
Details for diagnostics produced by the Dart analyzer.
dart.dev
ํด๊ฒฐ๋ฐฉ๋ฒ1) null ๊ฐ๋ ์ ์ฅ์ด ๊ฐ๋ฅํ ์๋ฃํ์ธ '[์๋ฃํ]?' ๊ธฐ๋ฅ์ ์ฌ์ฉํ๊ณ , ์์ฑ์๋ฅผ ์ฌ์ฉํด์ ์ฝ๋๋ฅผ ๊ฐ์ ํด๋ณด์
class Student {
String? name;
int? age;
Student(String name, int age){
// ์ธ์๋ก ๋ฐ์ ๋ณ์๋ค์ ๊ฐ๋ค์
// ํด๋์ค์์ ์์ฑํ ์์ฑ๋ณ์(๋ฉค๋ฒ๋ณ์)์ ๊ฐ์ผ๋ก ํ ๋นํด์ค์ผ ํ๋ค.
// ๋ฉค๋ฒ๋ณ์๋ช
๊ณผ ์ธ์๊ฐ์ผ๋ก ๋ฐ์์จ ๋ณ์๋ช
์ด ๋์ผํ๋ฏ๋ก ๊ตฌ๋ถ์ด ํ๋ฆ
// ๊ตฌ๋ถ์ ์ํด, ๋ฉค๋ฒ๋ณ์ ์์ thisํค์๋+.๋ฅผ ๋ถ์ฌ ์ฌ์ฉํ๋ค.
this.name = name;
this.age = age;
}
}
void main() {
var student = Student('jiny', 21);
print(student.age);
}
ํด๊ฒฐ๋ฐฉ๋ฒ2) late ํค์๋ ์ฌ์ฉ
class Student {
late final String name;
late int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
}
void main() {
var student = Student('jiny', 21);
print(student.age);
}
๊ทผ๋ฐ ๊ฒฝ๊ณ ๊ฐ ๋ ์ ๋ณด๋ฉด ์ด๊ฒ ํจ์จ์ ์ธ ์คํ์ผ์ด ์๋๋ผ๊ณ ํ๋ค.
์ฐธ๊ณ :
prefer_initializing_formals
prefer_initializing_formals Group: style Maturity: stable View all Lint Rules Using the Linter DO use initializing formals when possible. Using initializing formals when possible makes your code more terse. BAD: class Point { num x, y; Point(num x, num y)
dart-lang.github.io
๊ฐ์ :
์์ผ๋ก๋ ์๋ ์ฝ๋์ ๊ฐ์ด ์์ฑ์๋ฅผ ์์ฑํ๋ค!
๋ฐ์์จ ๊ฐ์ ๋ฐ๋ก ๊ฐ ํ๋กํผํฐ์ ํ ๋นํด์ฃผ๋๊น ์๋ฌด ๋ฌธ์ ๊ฐ ์๋ค.
class Student {
String name;
int age;
Student(this.name, this.age);
}
void main() {
var student = Student('jiny', 21);
print(student.age);
}
Named Constructor Parameters
- ์ ๋ฐฉ์์ positional ๋ฐฉ์์ด๋ค. ์ด๊ฑด '์์น'๊ฐ ์ค์ํ๋ค. ํจ์์ ํธ์ถ๋ถ๋ฅผ ๋ณด๊ณ ๋ญ๋ฅผ ๋ปํ๋์ง ๋ชจ๋ฅธ๋ค.
- ์ด๊ฒ์ ๊ฐ์ ํ๊ธฐ ์ํ ๊ฒ์ด ํจ์์์์ ๋๊ฐ์ด Named Parameters๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค.
- ํจ์์์์ ๋๊ฐ์ด {} ์ฌ์ฉํ๊ณ , parameter์ด๋ฆ: ๋ฐ์ดํฐ ํ์์ผ๋ก ์ธ์คํด์ค๋ฅผ ๋ง๋ ๋ค.
- ๊ทธ๋ฆฌ๊ณ null์ฐธ์กฐ๋ฅผ ๋ง๊ธฐ ์ํด ์์ฑ์์ ์ธ์๋ค ์์ required๋ฅผ ๋ถ์ธ๋ค. ์ด๊ฒ ๊ธฐ๋ณธ๊ฐ์ ์ฃผ๋ ๋ฐฉ์๋ณด๋ค ๋ ๋์
class Student {
String name;
int age;
String team;
Student({
required this.name,
required this.age,
required this.team,
});
}
void main() {
var student = Student(
name: 'jiny',
age: 21,
team: 'sm',
);
print(student.age);
}
Named Constructor
* Null Safety?
- values CAN'T BE NULL unless you can say BE
- null safety๋, ์ด๋ค ๋ณ์๊ฐ null์ด ๋ ์ ์์์ ๋ช
์ํจ์ผ๋ก์จ ์๋ชป๋ ์ํ์ ๋ณ์๋ฅผ ์ฐธ์กฐํ๋ ๊ฑธ ๋ง๋ ๊ธฐ๋ฅ์ด๋ค.
- ์ด๋ ๊ฐ๋ฐ์๊ฐ null๊ฐ์ ์ฐธ์กฐํ ์ ์๋๋ก ํ๊ฒ ํ๊ธฐ ์ํ ๊ฒ์ด๋ค.
- ์ด๊ฑธ ๋ฐฉ์งํ์ง ์์์ null๊ฐ์ ์ฐธ์กฐํ๋ฉด => ๋ฐํ์์๋ฌ ๋ฐ์, ์ด๊ฑด ์ปดํ์ผ๋ฌ๊ฐ ์ก์ง ๋ชปํ๋ ์๋ฌ์
- null์ '๊ฐ์ด ์์'์ ๋ํ๋ด๋ ๊ฐ์ด๊ณ , ๋งค์ฐ ์ ์ฉํ๊ฒ ์ฌ์ฉ๋จ. ์ด๊ฑธ ์์จ ์๋ ์์.
- ๋ฐ๋ผ์ ์ด๊ฑธ ๋ฐฉ์งํ๊ธฐ ์ํด ์ด๋ค ๋ณ์๊ฐ null์ด ๋ ์ ์์์ ์ ํํ ํ์ํ ์ ์๋๋ก ์๋ฃํ + '?' ๋ผ๋ ๊ฒ์ ๋ง๋ฆ
- "์๋ฃํ?" <= ์ด๋ ๊ฒ ์ ์ธ๋ ๋ณ์๋ ์ปดํ์ผ๋ฌ๊ฐ null์ด ๋ ์ ์๋ค๋ ๊ฒ์ ์๊ธฐ ๋๋ฌธ์ ์ฐ๋ฆฌ๋ฅผ ๋์์ค ์ ์์. ํ๋จ ์์์ฝ๋ ์ฐธ๊ณ
- ์์ ํ ํ๋ก๊ทธ๋จ์ ๋น๋ํ ๋ ๋งค์ฐ ์ค์ํ ๊ฐ๋
- Java, C++๋ฑ ๋ค๋ฅธ ์ธ์ด์์ ๋ฌธ์ ๊ฐ ์๋ ๊ฐ๋ ์.
- API์์ ๋ฐ์ดํฐ ๋ฐ์์ฌ ๋ null safety๋ง์ด ๋ณผ ๊ฒ.
- ์ ๋ฆฌ:
- ๊ธฐ๋ณธ์ ์ผ๋ก dart์ ๋ณ์๋ค์ ๊ธฐ๋ณธ์ ์ผ๋ก nullable์ด ์๋๊ณ (null์ด ๋ ์ ์๋ค.),
- nullableํ๊ฒ ๋ง๋ค๊ณ ์ถ๋ค๋ฉด ?๋ฅผ ๋ถ์ฌ์ผ ํ๋ค๋ ๊ฒ.
void main(){
String? jiny = 'jiny';
jiny = null;
// jiny.length; // jiny๊ฐ null์ผ์๋ ์๊ธฐ ๋๋ฌธ์ ํ์ธ์ด ํ์ํ๋ค๊ณ ์๋ฌ๋ฅผ ๋์
if (jiny != null){
jiny.length;
}
// ์์ฝ ๋ฌธ๋ฒ
jiny?.length; // jiny๊ฐ null์ด ์๋๋ฉด length์์ฑ์ ์ฌ์ฉํ๋ค.
}
์๋ชป๋ ๋ถ๋ถ์ด๋ ์ดํด๊ฐ ์๋๋ ๋ถ๋ถ์ ๋๊ธ ๋จ๊ฒจ์ฃผ์ธ์ :)
'๐ฌ Programming Language' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Dart] #3 FUNCTIONS: Definition, Parameters, QQ, Typedef (0) | 2023.01.22 |
---|---|
[Dart] #2 DATA TYPES: List, String Interpolation, Collection For, Maps, Sets (0) | 2023.01.21 |
[Dart] #1 VARIABLES(2): Final, Late, Constant (0) | 2023.01.21 |
[Dart] #1 VARIABLES(1): Var, Dynamic (0) | 2023.01.21 |