inblog logo
|
Gyeongwon's blog
    FlutterDart

    [Dart 문법 #6] 클래스: 객체 초기화

    도경원's avatar
    도경원
    Sep 23, 2025
    [Dart 문법 #6] 클래스: 객체 초기화
    Contents
    1. 클래스: 객체 초기화 방법

    1. 클래스: 객체 초기화 방법

    방법 1.

    // 클래스 class User { String username; String password; User(this.username, this.password); } void main() { User u1 = User("ssar", "1234"); print(u1.username); print(u1.password); }

    방법 2.

    // 클래스 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); }

    방법 3.

    // 클래스 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); // 5678 }

    방법 4. CASCADE

    // 클래스 class User { // 상태 String username; String password; // 생성자 (클래스 초기화) User(this.username, this.password); // 행위 (메서드)) void passwordChange(String password) { this.password = password; } void init() { print("객체 초기화 코드가 필요할때 캐스캐이드 사용"); } } void main() { User u1 = User("ssar", "1234")..init(); // User 메모리에 로드 print(u1.username); print(u1.password); }
     
    Share article
    Contents
    1. 클래스: 객체 초기화 방법

    Gyeongwon's blog

    RSS·Powered by Inblog