[Flutter] 4. Dart (4) - 클래스

김주희's avatar
Jun 08, 2025
[Flutter] 4. Dart (4) - 클래스
클래스가 들고 있는 상태 = 필드
접근제어자 없음
 
Dart는 컴파일 시점에 null safety 검사를 한다 실행시점이 아니라
java는 클래스를 new하면 생성자가 실행이 되고 중괄호가 끝날때 username이 초기화가 된다.
java는 초기화 시점을 생성자 내부 stack이 끝날때까지라고 본다
컴파일 시점에서는 메서드 내부 체킹하지 않는다. 외부만 봄. 호출시에만 스택이 메모리에 올라오면서
 
<null 체킹 이야기>
null일 수 없지만 Dart는 내부를 안봄
username이 null여부 판단하기 위해 {} 실행 전에 판단
매개변수로 들어오는 username은 ?가 안붙어서 무조건 값이 들어오지만 생성자 내부는 안보기 때문에 필드에 바인딩된다고 확신 할 수 없어서 오류가 뜬다.
notion image
 
방법1
notion image
 
방법2. late - 지연전략 (사용X)
개발자에게 전적으로 맡기는 것
notion image
 
💡
{} 실행전에 초기화됐다고 확신을 줘야함!
 
문법1.
매개변수에 this.username이라고 ㅅ해서
notion image
notion image
notion image
notion image
 
사용X지만 알고 있어야 함
_변수 = private
 
 
 
 
 
 
 
 
 
 
 
 
 
문법2. initialized keyword 초기화 키워드
중괄호 내부 실행 전에 이걸 먼저 하고 실행해라
notion image
값이 여러개라 구분하고 싶어서 key값을 넣고 싶다면 ? → 선택적 매개변수 사용
 
 
 
 
 
 
 
 
 
 
 
💡
생성자의 중괄호는 없다고 생각하자
 
 
notion image
무조건 한줄만 됨
여러줄할거면 cascade를 해야됨
 
 

void init(){ // 생성자에 여러줄 쓰고 싶으면 이런식으로 함수 만들어서 cascade로 하면 깔끔하게 처리 가능
if(){
if(){
}
}
}
 
..init() → cascade 연산자
원래는 u1.init();이라고 해야하는데
여러줄을 못적는 경우가 많을 때가 있다. 한줄로 넣고 싶은
User(”ssar”,”1234”)..init() 이런식으로 쓰면 된다.
 
여태 객체 초기화하는 방법 4가지 배움?
// 클래스 class User { String username; String password; User(this.username, this.password); } void main() { User u1 = User("ssar", "1234"); print(u1.username); print(u1.password); }
// 클래스 class User { String username; String password; User({required this.username, this.password = "1234"}); } void main() { User u1 = User(username: "ssar"); print(u1.username); print(u1.password); }
컴파일러가 중괄호 내부가 실행되기 전에 확인할 수 있음 // 클래스 class User { String username; String password; User(String username, String password) : this.username = username, this.password = password == "1234" ? "5678" : password; } void main() { User u1 = User("ssar", "1234"); print(u1.username); print(u1.password); }
// 클래스 class User { String username; String password; User(this.username, this.password); void init() { if (username == "ssar") { if (password == "1234") { this.password = "9999"; } } } } void hello(User u) {} void main() { hello(User("ssar", "1234")..init()); }
 
 

추상클래스, 상속
 
상속의 목적 = 추상화 (+ 데이터를 물려받는건 덤)
new → 생성자 → initialized keyword → 부모 생성자
// 상속 class Burger { String name; Burger(this.name); } class ChickenBurger extends Burger{ int price; ChickenBurger(this.price, super.name); } // CheeseBurger는 Burger 타입이다. class CheeseBurger extends Burger{ CheeseBurger(String name) : super(name); // initialized keyword } // 비추천 void main(){ Burger b1 = CheeseBurger("치즈버거"); Burger b2 = ChickenBurger(1000,"치킨버거"); }
 

생성자 오버로딩이 아니라 이름이 있는 생성자
이름이 있는 생성자일수도 있고 static 함수?뭐?일수도 잇다
// 상속 class Button { String text; String color; int x; int y; int width; int height; Button( this.text, { this.color = "회색", this.x = 0, this.y = 0, this.width = 200, this.height = 150, }); Button.block( this.text, { this.color = "회색", this.x = 0, this.y = 0, this.width = 100000000, this.height = 150, }); static Button inlineButton(String text) { return Button(text); } } void main() { Button basicButton = Button("로그인"); Button redButton = Button("로그인", color: "red"); Button blockButton = Button.block("회원가입"); Button inlineButton = Button.inlineButton("회원가입"); }
 
 

강제성 부여하고 싶으면 - 추상클래스 (인터페이스 없음)
함수 전달하고 싶으면 그냥 함수 전달하면 된다

합성 = 생성자 주입
Engine과 Car 는 상속 관계X
with = 합성
mixin class Engine{}
 
java에서는 상속을 하나만 할 수 있는데
class Car with Engine, with Wheel {}가능하다
 
 

상속
// 상속 class Burger { String name; Burger(this.name); } // is class ChickenBurger extends Burger { int price; ChickenBurger(this.price, super.name); } class CheeseBurger extends Burger { CheeseBurger(String name) : super(name); } void main() { Burger b1 = CheeseBurger("치즈버거"); Burger b2 = ChickenBurger(1000, "치킨버거거"); }
이름이 있는 생성자
// 상속 class Button { String text; String color; int x; int y; int width; int height; Button( this.text, { this.color = "회색", this.x = 0, this.y = 0, this.width = 200, this.height = 150, }); Button.block( this.text, { this.color = "회색", this.x = 0, this.y = 0, this.width = 100000000, this.height = 150, }); static Button inlineButton(String text) { return Button(text); } } void main() { Button basicButton = Button("로그인"); Button redButton = Button("로그인", color: "red"); Button blockButton = Button.block("회원가입"); Button inlineButton = Button.inlineButton("회원가입"); }
 
composition
mixin class Engine { int power = 5000; } mixin class Wheel { int size = 21; } // has class Car with Engine, Wheel {} void main() { Engine e = Engine(); Car c1 = Car(); print(c1.power); print(c1.size); }
Share article

jay0628