조건문
if() {} else if() {} else {}
swich(e) {
case 1:
break;
case 2:
break;
default:
break;
}
void main() {
int total = 0;
List<int> numberList = [2,3,5,2];
for (int e = 0; e < numberList.length; e++){
if(total == 10) {
break;
}
total += numberList[e];
}
for(int number in numberList){
if(total == 10) {
continue;
}
total += number;
}
while(total < 10) {
total++;
}
do {
total++;
}while(total < 10);
print(total);
}
함수
파라미터
void main() {
calcul1(1);
calcul2(1, b: 1);
}
double calcul1 (double a, [double b = 2]){
return a*b;
}
double calcul2 (double a, {double b = 2}){
return a*b;
}
클래스
상속
void main() {
final Castie = new Korean('castie', 'male', 1);
Castie.sayCountry();
Castie.sayName();
}
class Human {
final String name;
final String gender;
int id;
Human(String name, String gender, int id)
: this.name = name,
this.gender = gender,
this.id = id;
void sayName() {
print('저는 ${this.name} 입니다');
}
}
class Korean extends Human {
Korean(String name, String gender, int id) : super(name, gender, id);
void sayCountry() {
print('저는 한국인입니다');
}
}
메서드 오버라이드
void main() {
final parent = new Parent(3,4);
parent.calcul();
final child = new Child(3,4);
child.calcul();
}
class Parent {
int num1;
int num2;
Parent(this.num1, this.num2);
void calcul () {
print(num1 + num2);
}
}
class Child extends Parent {
int num1;
int num2;
Child(this.num1, this.num2) : super(num1, num2);
@override
void calcul () {
print(num1 * num2);
}
}
static & cascade
void main() {
final castie = Privacy('castie', 29);
Privacy.country = 'korea';
castie.introduce();
Privacy.myCountry();
Privacy.country = 'america';
new Privacy('harring', 30)
..introduce()
..myAge();
}
class Privacy {
static String country = '';
String name;
int age;
Privacy(this.name, this.age);
void introduce () {
print('저는 $country 에서 사는 $name 입니다');
}
void myAge () {
print('$name 의 나이는 $age 입니다');
}
static void myCountry () {
print('저는 $country 출신입니다');
}
}
List 주요 메서드
void main() {
List test1 = [
'첫번째',
'두번째',
'세번째',
'네번째',
];
print(test1.isEmpty);
print(test1.isNotEmpty);
print(test1.length);
print(test1.first);
print(test1.last);
print(test1.reversed);
test1.add('다섯번째');
print(test1);
test1.addAll(['여섯번째', '일곱번째']);
print(test1);
}
test1.remove('첫번째');
print(test1);
test1.removeAt(0);
print(test1);
Map 주요메서드
void main() {
Map test2 = {
'first' : 1,
'second' : 2,
'third' : 3,
};
print(test2.isEmpty);
print(test2.isNotEmpty);
print(test2.length);
print(test2.keys);
print(test2.values);
test2.addAll({'fourth' : 4});
print(test2);
test2['fifth'] = 5 ;
print(test2);
test2.update('first', (prev)=>prev*10);
print(test2['first']);
test2.update('sixth', (prev)=>prev*10, ifAbsent: ()=>10);
print(test2);
test2.remove('first');
print(test2);
test2.removeWhere((key, value){
return value == 10;
});
print(test2);
}