Created with <3 with dartpad.dev.
Created
June 9, 2023 02:24
-
-
Save junddao/0e5649a1053208a4721128b54bdde9a1 to your computer and use it in GitHub Desktop.
dart 3.0 _ class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
} | |
// 주의 : (같은 파일에서는 가능하다) | |
// final 로 클래스를 선언하면 | |
// extends, implement 또는 mixin 으로 사용 불가능하다 | |
final class Person{ | |
final String name; | |
final int age; | |
Person({ | |
required this.name, | |
required this.age, | |
}); | |
} | |
// base 로 클래스를 선언하면 | |
// extend 가능, implement 불가능 | |
// base, sealed, final로 선언된 클래스만 extend 가능하다. | |
base class Person2{ | |
final String name; | |
final int age; | |
Person2({ | |
required this.name, | |
required this.age, | |
}); | |
} | |
// 요렇게 쓰라는뜻 | |
// base class Devs extends Person2{} | |
// interface로 선언하면 | |
// implement만 가능 | |
interface class Person3{ | |
final String name; | |
final int age; | |
Person3({ | |
required this.name, | |
required this.age, | |
}); | |
} | |
// 요렇게 쓰라는뜻 | |
// class Devs implements Person3{} | |
// sealed 클래스는 abstract 이면서 final 이다 | |
// 그리고 패턴 매칭용으로 사용할 수 있도록 해준다. | |
sealed class Person4{ | |
} | |
class Devs extends Person4{} | |
class Marketers extends Person4{} | |
class Planner extends Person4{} | |
String whoIsHe(Person4 person) => switch(){ | |
Devs d => '개발지', | |
Marketers m => '마케터', | |
// 아래 기획자 안넣으면 에러남 | |
// Planner p => '기획자', | |
}; | |
// Mixin Class | |
// 1) mixin 은 extends 나 with를 사용할수 없다. 그렇기 때문에 mixin class 도 마찬가지로 불가 | |
// 2) class는 on 키워드를 사용할 수 없다. 그렇기 때문에 mixin class도 on 키워드를 사용할 수 없다. | |
mixin class AnimalMixin{ | |
String bark(){{ | |
return '멍멍'; | |
}} | |
} | |
class Dog with AnimalMixin{} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment