main() {
Parent parent = new Parent(3);
int result = parent.calculate();
print(result);
Child child = new Child(3);
int result2 = child.calculate();
print(result2);
}
class Parent{
final int number1;
Parent(int number) :
this.number1 = number;
int calculate(){
return this.number1 * this.number1;
}
}
class Child extends Parent{
Child(int number):
super(number); // -> Parent(int number) : ~~~ 이것을 의미하므로 number1 변수를 사용
// ↱부모 메서드 오버라이드 함
int calculate(){
return this.number1 + this.number1;
}
}
클래스
main() {
Idol redVelvet = new Idol();
redVelvet.sayName();
}
class Idol{
String name = '레드벨벳';
void sayName(){
print("저는 ${this.name} 입니다.");
}
}
생성자 constructor
main() {
Idol redVelvet = new Idol("사나", "트와이스");
redVelvet.sayName();
Idol bts = new Idol("제이콥", "BTS");
bts.sayName();
}
class Idol{
// 멤버변수 선언
final name2;
final group2;
// 객체 생성 매개변수를 멤버변수에 저장하기
Idol(String name, String group) :
this.name2 = name,
this.group2 = group;
void sayName(){
print("저는 ${this.name2}이고 ${this.group2} 그룹입니다.");
}
}
오버로딩 및 private 변수 ("_")
main() {
Idol seulgi = new Idol("슬기", "레드벨벳", 1);
Idol rm = new Idol("rm", "bts", 2);
seulgi.sayName();
rm.sayName();
print("\n--- 오버로딩 ---");
Idol seulgi2 = new Idol.fromMap1({"name":"슬기", "group":"레드벨벳", "id":3});
seulgi2.sayName();
Idol rm2 = new Idol.fromMap1({"name":"rm", "group":"bts", "id":4});
rm2.sayName();
}
class Idol{
// 멤버변수 선언
final name2;
final group2;
// private 변수 선언
final _id;
// 객체 생성 매개변수를 멤버변수에 저장하기
Idol(String name, String group, int id) :
this.name2 = name,
this.group2 = group,
this._id = id;
// 오버로딩
Idol.fromMap1(Map values) :
this.name2 = values["name"],
this.group2 = values["group"],
this._id = values["id"];
void sayName(){
print("저는요 ${this.name2}이고 ${this.group2} 그룹이예요.");
print(_id); // Dart 같은 파일에 있으므로 접근 가능함, 다른 파일에 있으면 접근 불가
}
}
getter, setter
main() {
Idol seulgi = new Idol("슬기", "레드벨벳", 1);
// getter 이용하기
print(seulgi.getId);
// setter 이용하기
seulgi.setId = 20;
print(seulgi.getId);
}
class Idol{
// 멤버변수 선언
final name2;
final group2;
int id;
// 객체 생성 매개변수를 멤버변수에 저장하기
Idol(String name, String group, int id) :
this.name2 = name,
this.group2 = group,
this.id = id;
// 오버로딩
Idol.fromMap1(Map values) :
this.name2 = values["name"],
this.group2 = values["group"],
this.id = values["id"];
void sayName(){
print("저는요 ${this.name2}이고 ${this.group2} 그룹이예요.");
print(id); // Dart 같은 파일에 있으므로 접근 가능함, 다른 파일에 있으면 접근 불가
}
// getter, setter 이용하기
get getId{
return this.id;
}
set setId(int id){
this.id = id;
}
}
상속 extends
main() {
BoyGroup rm = new BoyGroup("RM", "BTS", 11);
GirlGroup seulgi = new GirlGroup("슬기", "레드벨벳", 22);
// 부모클래스 메서드 사용하기
rm.sayName();
// 자식클래스 메서드 사용하기
rm.sayMale();
seulgi.sayFemale();
}
class Idol{
// 멤버변수 선언
final name2;
final group2;
int id;
// 객체 생성 매개변수를 멤버변수에 저장하기
Idol(String name, String group, int id) :
this.name2 = name,
this.group2 = group,
this.id = id;
// 오버로딩
Idol.fromMap1(Map values) :
this.name2 = values["name"],
this.group2 = values["group"],
this.id = values["id"];
void sayName(){
print("저는요 ${this.name2}이고 ${this.group2} 그룹이예요.");
print(id); // Dart 같은 파일에 있으므로 접근 가능함, 다른 파일에 있으면 접근 불가
}
get getId{
return this.id;
}
set setId(int id){
this.id = id;
}
}
// 상속받기
class BoyGroup extends Idol{
BoyGroup(String name3, String group3, int id3):
super(name3, group3, id3);
void sayMale(){
print("\n저는 남자 아이돌 $name2 입니다.");
}
}
class GirlGroup extends Idol{
GirlGroup(String name4, String group4, int id4):
super(name4, group4, id4);
void sayFemale(){
print("저는 여자 아이돌 $name2 입니다.");
}
}
메서드 오버라이딩
main() {
BoyGroup rm = new BoyGroup("RM", "BTS", 11);
GirlGroup seulgi = new GirlGroup("슬기", "레드벨벳", 22);
// 부모클래스 메서드 사용하기
rm.sayName();
// 자식클래스 메서드 사용하기
rm.sayMale();
seulgi.sayFemale();
}
class Idol{
// 멤버변수 선언
final name2;
final group2;
int id;
// 객체 생성 매개변수를 멤버변수에 저장하기
Idol(String name, String group, int id) :
this.name2 = name,
this.group2 = group,
this.id = id;
// 오버로딩
Idol.fromMap1(Map values) :
this.name2 = values["name"],
this.group2 = values["group"],
this.id = values["id"];
void sayName(){
print("저는요 ${this.name2}이고 ${this.group2} 그룹이예요.");
print(id); // Dart 같은 파일에 있으므로 접근 가능함, 다른 파일에 있으면 접근 불가
}
get getId{
return this.id;
}
set setId(int id){
this.id = id;
}
}
// 상속받기
class BoyGroup extends Idol{
BoyGroup(String name3, String group3, int id3):
super(name3, group3, id3);
void sayMale(){
print("\n저는 남자 아이돌 $name2 입니다.");
}
}
class GirlGroup extends Idol{
GirlGroup(String name4, String group4, int id4):
super(name4, group4, id4);
void sayFemale(){
print("저는 여자 아이돌 $name2 입니다.");
}
}
메서드 오버라이딩2 (부모 메서드 그대로 사용하기)
main() {
Parent parent = new Parent(3);
int result = parent.calculate();
print(result);
Child child = new Child(3);
int result2 = child.calculate();
print(result2);
}
class Parent{
final int number1;
Parent(int number) :
this.number1 = number;
int calculate(){
return this.number1 * this.number1;
}
}
class Child extends Parent{
Child(int number):
super(number); // -> Parent(int number) : ~~~ 이것을 의미하므로 number1 변수를 사용
// ↱부모 메서드 오버라이드 함
int calculate(){
// ↱ Parnet의 메서드 사용하기 (오버라이드 해도 super.~~~ 하면 가능함)
int result = super.calculate();
return result * result;
}
}
'Dart' 카테고리의 다른 글
Dart 인터페이스 (0) | 2021.06.13 |
---|---|
Dart static 사용 (0) | 2021.06.13 |
Dart typedef (0) | 2021.06.13 |
Dart enum (열거형 타입) (0) | 2021.06.13 |
Dart 함수 (0) | 2021.06.13 |