안녕하세요. 우당탕탕 개발 일지 입니다. ios 프로젝트를 준비중이라 혼자 dart를 독학해야 합니다. 유튜브 <코드 팩토리_ 왕초보 dart 강의>를 보고 공부 합니다. 자료가 많아서 마음만 먹으면 공부할 수 있는 환경에 감사하네요...아자아자 화이팅!
#15강 Function
- return하지 않으면) 반환 타입 적지 않음.
void main()
{
print_list();
}
print_list() {
List testList = [1, 2, 3, 4, 5];
for (var i in testList)
{
print(i);
}
}
- 리턴 해줄 경우 ) 함수 앞에 리턴 타입 적어주기
void main() {
print(print_list());
}
List testList = [1, 2, 3, 4, 5];
int print_list() {
int tot = 0;
for (int i in testList) {
tot = tot + i;
}
return (tot);
}
- 매개 변수로 함수에 값 넘겨주기.
→ 매개변수가 여러개일 때 함수 호출할때 매개변수의 자릿수를 지켜야 한다.
void main() {
List testList = [1, 2, 3, 4, 5];
print(print_list(testList));
}
int print_list(List t) {
int tot = 0;
for (int i in t) {
tot = tot + i;
}
return (tot);
}
- 매개 변수를 지정 하지 않은 경우 정해진 값으로 출력 하려면 → [int b=3]
void main() {
List testList = [1, 2, 3, 4, 5];
int result1 =print_list(testList,1,2);
print("tot= $result1");//b=2
int result2 =print_list(testList,1);
print("tot= $result2"); //b=3
}
int print_list(List t ,int a, [int b=3]) {
int tot = 0;
for(int i=0; i<10; i++)
{
tot+=i;
}
print("a= $a");
print("b= $b");
return (tot);
}
#16 Typrdef
:함수를 변수처럼 사용할 수 있게 해주는 키워드.
void main()
{
add(1,3); //4
Operation oper =add;
oper(1,2); //3
calculate(1,2,add);//3
}
typedef Operation (int x, int y);
void add (int x, int y)
{
print("x+y= ${x+y}");
}
void calculate(int x, int y, Operation oper)
{
oper(x,y);
}
#17 Class 선언과 Constructor
- OOP :객체 지향 프로그래밍.
- Class: 비슷한 기능, 비슷한 성질을 모아둔것._ 무조건 클래스 이름의 첫글자는 대문자로 쓰기.
- Instantiation: 인스턴스화_클래스를 인스턴스로 바꾸는 것.
- Constructor : 처음 클래스가 선언이 될때 원하는 변수를 클래스 안에 집어 넣을 때 사용.
※final 사용시, 클래스 안에서 final을 사용한 변수는 무조건적으로 지정해야함. 그리고 변경 안됨.
void main()
{
//Idol red_velvet =new Idol();
//redVelvet.sayname();
Idol bts =new Idol('뷔','rm',);
bts.sayname();
//#1
Idol red_velvet =new Idol(
group:'레드벨벳',
name:'슬기',
);
//#2
Idol rm =new Idol.from_map({
'name':'RM',
'group':'BTS',
});
}
class Idol
{
//String name = '레드벨벳';
// Constructor
String name;
String group;
//#1
Idol(String name, String group)
: this.name=name , this.group=group;
//#2
Idol.from_map(
Map input,
): this.name =input['name'],
this.group =input[''group'];
void sayname()
{
print("이름은 ${this.name}입니다");
}
}
728x90
'[프로그래밍 언어 & Tool] > Dart' 카테고리의 다른 글
[Dart] 독학 필기 #5 _super, this, interface,cascade (1) | 2025.04.09 |
---|---|
[Dart] 독학 필기 #4_Class get ,set , 상속, Method Overriding,static (6) | 2025.04.08 |
[Dart] 독학 필기 #2_ final,const, 반복문,조건문 ,enum (0) | 2025.03.31 |
[Dart] 독학 필기 #1_ String,List,Map,var,dynamic,캐멀케이징 (0) | 2025.03.30 |