วันเสาร์ที่ 20 มีนาคม พ.ศ. 2564

สรุปภาษา Dart เบื้องต้น สำหรับคนมีพื้นฐานการเขียนโปรแกรมแล้ว

method main() สำหรับการเริ่มต้นโปรแกรม และ method print() ในการแสดงผล

เริ่มต้นโครงสร้างต้องมี method main() ระบบจะเริ่มทำงานภายใน method เป็นอันดับแรก
method print('ข้อความ') เป็นการสั่งให้แสดงผลออกมาทาง console
void main() {
	print('Hello World!');
}

การ Comment

การ Comment ให้โปรแกรมไม่ต้องสนใจข้อความนั้น ๆ ส่วนมากใช้สำหรับการอธิบายคำสั่ง
// ด้านหน้า สำหรับบรรทัดเดียว

/*
	ปิดหัวท้าย สำหรับหลายบรรทัด
*/

ชนิดของตัวแปร และ การประกาศตัวแปร

การประกาศตัวแปรจะใช้ ชื่อชนิดของตัวแปร ตามด้วยชื่อตัวแปร และกำหนดหรือไม่กำหนดค่าของตัวแปร
type variableName = initialValue;
// Example
country;
country = 'Thailand';
ภาษา Dart จะมีตัวชนิดตัวแปรแบบ Static และ Dynamic
Static เมื่อกำหนดแล้วไม่สามารถเปลี่ยนประเภทข้อมูลได้ขณะกำลังทำงาน โดยจะมีชนิดดังนี้
- bool เก็บค่า true หรือ false
- num เก็บค่าตัวเลขได้ทั้ง จำนวนเต็ม และ ทศนิยม
  - int เก็บเลชจำนวนเต็ม
  - double เก็บเลขทศนิยม
- String เก็บข้อความ
- List<dataType> เก็บชุดข้อมูล ได้ประเภทข้อมูลเดียว
- Map<index, value> เก็บชุดข้อมูล ได้หลายประเภท

Dynamic สามารถเปลี่ยนแปลงประเภทข้อมูลได้ตลอดเวลา
- var เลือกประเภทข้อมูลเองให้ตรงกับค่าที่เก็บเอง เมื่อกำหนดค่าเริ่มต้นแล้วจะเปลี่ยนแปลงประเภทข้อมูลไม่ได้
- dynamic เลือกประเภทข้อมูลเองให้ตรงกับค่าที่เก็บเอง เมื่อกำหนดค่าเริ่มต้นแล้วจะเปลี่ยนแปลงประเภทข้อมูลได้
// Static
bool isDart = true;
num age = 20.0;
int year = 2000;
double weight = 72.5;

List<num> = [];
Map = {};

// Dynamic
var isDart;
dynamic name;
Constant เก็บค่าคงที่ โดยจะไม่สามารถเปลี่ยนแปลงประเภทและค่าภายหลังจากประกาศใช้งานแล้ว
- const ต้องกำหนดค่าตั้งแต่เริ่มต้น และไม่สามารถใช้ค่าต่อจากตัวแปรอื่น ๆ มาร่วมกำหนดค่าเริ่มต้นได้
- final ต้องกำหนดค่าตั้งแต่เริ่มต้น และสามารถใช้ค่าต่อจากตัวแปรอื่น ๆ มาร่วมกำหนดค่าเริ่มต้นได้
//Example
const int COUNT = 0;  // กำหนดค่าโดนไม่มีการนำค่าจากตัวแปรอื่นมาใช้งาน
final int SUM = COUNT + 20; // กำหนดค่าโดยนำค่าจากตัวแปรอื่นมาร่วมด้วย

การจัดการกับข้อความ ตัวแปรประเภท String

String "" '' + '' $x x.toString();

การจัดการกับตัวเลข ตัวแปรประเภท Number (num, int, double)

Number

เครื่องหมายทางคณิตศาสตร์ (Alimathic)

คณิตศาสตร + - * / % ++pre post++ --pre post-- compound += -= *= /= %=

เครื่องในการเปรียบเทียบ (Compairesion)

Compare == != > < >= <=

Decision

คือการควบคุมคำสั่งต่าง ๆ ตามเงื่อนไข
- if, if ... else, else ... if ladder
// Syntax
// if statement
if(booleanExpression) {
	... statement ...
}

// if ... else statement
if(booleanExpression) {
	... statement ...
} else {
	... statement ...
}

// else ... if ladder
if(booleanExpression) {
	... statement ...
} else if(booleanExpression) {
	... statement ...
} else {
	... statement ...
}

// Example

- short if
// Syntax
[variableType] variableName = booleanExpression ? trueValue : falseValue ;

// Example

- switch ... case
// Syntax
// switch ... case
switch(variableExpression) {
	case constantExpression1: {
    	// ... statements ...
    } 
    break;
    
    case constantExpression2: {
    	// ... statements ...
    } 
    break;
    
    ...
    
    default: {
    	// ... statements ...
    }
    break;
}

// Example


Loops

การทำซ้ำจะมีการเช็คเงื่อนไขก่อนหรือหลังจากทำงานแต่ละรอบเสร็จ ถ้าเงื่อนไขเป็นจริงก็จะทำวนซ้ำ แต่ถ้าเงื่อนไขเป็นเท็จก็จะทำคำสั่งต่อไป
- for loop ทำตามคำสั่งตามจำนวนและเงื่อนไขที่กำหนด
// Syntax
for(initialCountValue; terminationCondition; step) {
	// ... statements ...
}

// Example
void main() {
	for(int count = 0; count > 5; count++) {
		print(count);
	}
}
// Result

0
1
2
3
4
- for ... in loop ทำตามคำสั่งตามจำนวนข้อมูลที่มีอยู่ใน Object
// Syntax
for(variableName in object) {
	// ... statements ...
}

// Example
void main() {
	List<int> numberList = [1,2,3,4,5];
    
    for (var number in numberList) {
    	print(number);
    }
}
// Result

1
2
3
4
5
- while loop ตรวจสอบเงื่อนไขเมื่อเงื่อนไขเป็นจริงจะทำคำสั่งใน { ... } เมื่อเสร็จแล้วจะตรวจสอบเงื่อนไขอีกครั้งไปเรื่อย ๆ
// Syntax
while(expression) {
	// ... statement ...
}

// Example
void main() {
	var number = 1;
    
    while(number <= 5){
    	print(number);
        number++;
    }
}
// Result

1
2
3
4
5
- do ... while loop ทำตามคำสั่งใน { ... } เสร็จแล้วตรวจสอบเงื่อนไขถ้าเป็นจริงก็กลับไปทำตามคำสั่งอีกครั้งไปเรื่อย ๆ
// Syntax
do {
	// ... statement ...
} while(expression);

// Example
void main() {
	var number = 1;
    
    do {
        print(number);
        number++;
    } while(number < 5);
}
// Result

1
2
3
4
- continue กลับไปเริ่มซ้ำใหม่
- break ออกจากการทำซ้ำ
// Example
void main() {
  
  for(int count = 1; count <= 10; count++){
    if(count%2 == 1) continue;
    print(count);
  }
  
  for(int count = 1; count <= 10; count++){
    if(count == 5) break;
    print(count);
  }
  
}
// Result
// continue;
2
4
6
8
10

// break;
1
2
3
4

Function

เป็นการเขียนแยกเฉพาะหน้าที่การทำงานออกจากกันและเก็บแยกไว้เป็นส่วน ๆ
[returnType|void] functionName([dataType parameterName,...]) { ... }

[returnType|void]
โดยโครงสร้างประกอบไปด้วยการกำหนดประเภทข้อมูลที่ Function จะส่งกลับมา หากไม่มีการส่งข้อมูลกลับมาใช้ void แทนประเภทข้อมูล

functionName([dataType parameterName, ...])
ตามด้วยชื่อ Function และกำหนดชื่อตัวแปรที่รับข้อมูลเข้ามาทำงานด้วย หากไม่มีก็ไม่ต้องใส่

{ ... }
และชุดคำสั่ง
//Example
void sayHello() {
	print('Hello');
}

void sayHelloWithName(String name){
	print('Hello ' + name);
}

int plus(int num1, int num2) {
	return num1 + num2;
}

Default Value ใน Function

การกำหนดค่า Default ให้กับ Parameter หรือเรียก Parameter นั้นว่าเป็น Optional เพราะไม่ต้องส่งข้อมูลเข้า Function ก็สามารถทำงานได้
โดยทำการเพิ่ม [ ... ] ครอบ Parameter เอาไว้ โดยจะเอาไว้ที่ Parameter ท้าย ๆ
ตัวอย่าง functionName(parameter1, ..., [optionParameter1 = defalutValue1, optionParameter2 = defaultValue2])
// Example
void sayHelloWithName(String name, [int age = 0]){
	print('Hello ' + name + ', Age ' + age.toString());
}

void main() {
	sayHelloWithName('Jumbo', 25);
    sayHelloWithName('Jumbo');
}

// สามารถเรียก sayHelloWithName('Jumbo') โดยจะแสดงข้อมูลออกมาเป็น
// Hello Jumbo, Age 0

Named Parameter

กำหนดชื่อให้กับ Parameter เพื่อให้ค่าที่ส่งเข้ามามีการระบุชื่อ Parameter และค่าที่จะส่งเข้ามาด้วยโดยให้ใส่เครื่องหมาย { ... } ครอบไว้
ตัวอย่าง functionName({parameterName1, parameterName2, ...})
// Example
void sayHello({String name, int age}){
	print('Hello ' + name + ', Age ' + age.toString());
}

void main() {
	String name = 'Jumbo';
    int age = 25;
    
	sayHello(name:name, age:age);
}

// เราสามารถเรียกใช้งาน sayHello(age:'25', name:'Jumbo') โดน Function จะนำข้อมูลมาแสดงได้ถูกต้องตามชื่อ Parameter
// Hello Jumbo, Age 25

First Class Function

คือการใช้งาน ตัวแปร ในลักษณะเป็น Function โดยจะทำการสืบทอด Function ที่กำหนดให้
// Example
int getNumber() {
	return 20;
}

void main() {
	var number = getNumber;
    print(number());
}

// กำหนดให้ number เป็นการสืบทอดมาจาก Function getNumber และทำการเรียกใช้ number()
// * ตอนสืบทอดจะไม่มี () ต่อท้าย

การเขียน Function แบบย่อ (Arrow Funcion)

คือการเขียนย่อ Function ให้มีขนาดสั้นลง โครงสร้าง [returnType] [functionName]() => [returnValue|{statement}]
// Example
void sayHello(name) => print('Hello ' + name);

void main() {
	sayHello('Jumbo');
}

// * ใส่ในการเป็น Function สำหรับ Callback

// Template

ไม่มีความคิดเห็น:

แสดงความคิดเห็น