■ 지정 생성자(named constructor)를 사용하는 방법을 보여준다.
▶ 예제 코드 (DART)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Player { String name = ''; int color = 0; Player(this.name, this.color); Player.fromPlayer(Player another) { this.name = another.name; this.color = another.color; } } void main() { Player player1 = Player('홍길동', 20); Player player2 = Player.fromPlayer(player1); print(player1.name); print(player2.name); } |