Class


class Player {
	String name = 'pgrrr';
	int xp = 1500;
}

var player = Player();
  • class 생성시 타입 명시
  • class 객체 생성시 new 불필요 (옵션)
  • class 내에서 this 불필요 (옵션)

Constructors

class Player {
	late final String name;
	late int xp;

	Player(String name, int xp){
		this.name = name;
		this.xp = xp;
	}
}

var player = Player("pgrrr", 100);
class Player {
	final String name;
	int xp;

	Player(this.name, this.xp);
}

Named Constructor Parameters

class Player {
	final String name;
	int xp;

	Player({
		required this.name,
		required this.xp
	});
}

var player = Player(
	name: "pgrrr",
	xp: 100,
);

Named Constructor

class Player {
	final String name;
	int xp;

	Player.createPlayer({required String name})
		: this.name = name,
			this.xp = 0;
}
  • : 콜론을 사용해 argument 와 property 를 일대일 초기화 생성자 생성

Cascade Notation

var player = Player(name: 'pgrrr', xp: 100, team: 'red');
player.name = 'pg';
player.xp = 0;
player.team = 'blue';

var player = Player(name: 'pgrrr', xp: 100, team: 'red')
..name = 'pg'
..xp = 0
..team = 'blue';

Enums

enum Team { red, blue }

class Player {
		Team team;
}

var player = Player(team: Team.red)
..team = Team.red;
  • 오타 실수를 줄여준다

Abstract class

abstract class Human {
		void walk();
}

class Player extends Human{
		Team team;

		void walk(){
			print('walk');
		};
}

Inheritance

class Human {
	final String name;
	Human(this.name);
	void sayHello() {
			print("My name is $name");
	}
}

class Player {
	final Team team;
	Player({required this.team, required String name}) : super(name);

	
	void sayHello() {
		super.sayHello();
		print('something');
}

Mixins

class Strong {
		final double level = 10;
}
class QuickRunner {
		void runQuick() {
				print("run");
		}
}

class Player with Strong, QuickRunner {
		final Team team;
}
  • 생성자가 없는 클래스
  • with 로 사용
  • 여러 클래스에 재사용 가능
profile
🧑🏻‍💻 Hello World!

0개의 댓글