다트(Dart)는 구글이 자바스크립트를 대체하기 위해 2011년도 개발한 멀티 플랫폼 프로그래밍 언어입니다.
다트의 특징으로는 모바일 앱, 웹 앱 등 여러 플랫폼을 다트를 이용해 만들 수 있습니다.
다트는 Java
혹은 Javascript
와 유사한 문법을 가지고 있습니다.
먼저 자료형부터 살펴보겠습니다.
변수와 함수 선언 예시는 아래와 같습니다.
String name;
num age;
List<String> students = ['Nayeon', 'Yeony', 'Kim'];
Map<String, int> scores = {
'math': 97,
'eng': 77
}
voin main() {
print('Hello World!');
}
String sayHi() {
return 'Hi!';
}
다트의 특징으로 dynamic type이 있다고 했는데요.
변수 선언 시 dynamic
을 붙이면 해당 변수는 모든 형식을 허용하는 형태가 됩니다. 처음 타입이 String이고 후에 int 타입을 할당해도 에러가 나지 않습니다.
void main() {
dynamic changing = 'Hi!!';
changing = 123;
print(changing); // 123
}
var
는 타입 추론을 가능케 하는 키워드입니다.
타입을 꼭 선언하지 않아도, 변수에 처음으로 할당되는 값에 따라 자동으로 타입을 추론합니다.
단, 이후 다른 타입의 값이 할당된다 해도 그 변수의 타입은 변경되지 않습니다.
void main() {
var age = 10; // age는 int 타입으로 고정
age = 20;
print(age); // 20
age = 'I am so young'; // Error! (int 변수에는 String을 할당할 수 없음)
}
자바나 자바스크립트에서 볼 수 있던 키워드를 다트에서도 볼 수 있습니다.
모두 상수를 의미하지만 차이가 있습니다.
자바스크립트를 주로 사용하는 개발자로서 자바스크립트 연산자와 아주 유사하다고 생각되었습니다.
자바스크립트와 다르거나 다트에만 있는 독특한 연산자만 정리합니다.
void main() {
print(101~/2); // 50
}
자바스크립트의 typeof
연산자와 유사합니다.
void main() {
var name = 'Yeony';
print(name is int); // false;
print(name is String); // true;
}
?.
를 적어줌으로써 해당 객체의 속성을 사용할 수 있는지 여부에 따라 반환되는 값이 달라집니다.
객체의 속성에 접근할 수 있다면 그대로 반환하고, 사용할 수 없다면 null
을 반환합니다.
void main() {
String? name;
print(name.length); // Error!
print(name?.length); // null
}
자바스크립트의 ||
연산자와 똑같이 동작합니다.
void main() {
String name;
String myName = name ?? 'Yeony';
print(myName); // name이 null이므로 'Yeony'
}
??
연산자와 반대로 동작합니다.
void main() {
int age;
int myAge = 24;
myAge ??= age;
print(myAge); //age가 null이므로 24
}
class는 객체를 정의하는 일종의 틀입니다. 특정한 모양의 객체를 생성하기 위해 변수, 메소드를 지정하는 것이죠.
class Fruit {
String name = 'Banana';
void printFruitName() {
print('This is a ${name}!');
}
}
void main() {
Fruit fruit = new Fruit();
fruit.printFruitName(); // This is a Banana!
fruit.name = 'Grapes';
fruit.printFruitName(); // This is a Grapes!
}
다트의 class또한 다른 객체지향언어와 같이 생성자(constructor)를 가질 수 있습니다
class Fruit {
String? name;
int? price;
Fruit(String name, int price) {
this.name = name;
this.price = price;
}
void printFruitName() {
print('This is a ${name}! This is ${price} won!');
}
}
void main() {
Fruit fruit = new Fruit('Strawberry', 10000);
fruit.printFruitName(); // This is a Strawberry! This is 10000 won!
}
Named Parameter 를 사용하면 이렇게 작성할 수도 있습니다.
class Fruit {
String? name;
int? price;
Fruit({String? name, int? price}) // named parameter
: this.name = name,
this.price = price;
void printFruitName() {
print('This is a ${name}! This is ${price} won!');
}
}
void main() {
Fruit fruit = new Fruit(name: 'Strawberry', price: 10000);
fruit.printFruitName(); // This is a Strawberry! This is 10000 won!
}
클래스에서는 상속이 가능합니다.
상속의 개념을 간단히 설명해보죠.
부모와 자식이 있습니다. 부모는 부모가 가진 것들만 자식에게 줄 수 있습니다. 반면 자식은 부모의 것도 가지고, 본인만의 것도 가질 수 있습니다. 자식이 부모의 것을 가지는 것이 상속, 자식이 부모의 것에 더해 자신만의 것을 가지는 것을 확장이라고 합니다.
상속을 받을 때는 extends
키워드를 사용합니다.
class Fruit {
String? name;
int? price;
Fruit({String? name, int? price}) // named parameter
: this.name = name,
this.price = price;
void printFruitName() {
print('This is a ${name}! This is ${price} won!');
}
}
class Juice extends Fruit {
Juice({super.name, super.price});
void printJuiceName() {
print('This is juice made of ${this.name}!!');
}
}
void main() {
Juice juice = new Juice(name: 'Lemon', price : 5000);
// 부모 class인 Fruit에도 접근 가능
juice.printFruitName(); // This is a Lemon! This is 5000 won!
juice.printJuiceName(); // This is juice made of Lemon!!
}
다트는 클래스를 활용해 인터페이스를 정의할 수 있습니다.
인터페이스란, 클래스 정의 시 반드시 정의해야하는 변수 및 함수를 지정할 때 사용합니다.
앞서 클래스가 일종의 틀이라고 언급했는데, 인터페이스도 유사하지만 규격에 가깝습니다.
다른 클래스를 작성할 때 기본 틀을 제공하는 것이죠.
자바 등에서는 interface 키워드를 이용해 정의하지만, 다트는 class
키워드를 사용해 인터페이스를 정의하고, implements
키워드로 인터페이스를 사용합니다.
class Food {
String? name;
void printFoodName() {} // 함수 정의만 함
}
class Fruit implements Food {
String? name;
Fruit(String name) : this.name = name;
// 함수 작성
void printFoodName() {
print('Hi! There is ${name} here!');
}
}
void main() {
Fruit fruit = new Fruit('Melon');
fruit.printFoodName();
}
◾ [Flutter] Widget 다루기 👈 다음 글 보기