name27 / flutter

0 stars 0 forks source link

클래스 #85

Open name27 opened 1 year ago

name27 commented 1 year ago

image

User.dart

import 'dart:core';
import 'package:intl/intl.dart';

class User {
  String name;
  String phoneNum;
  String email;
  String? nickName;
  String? profileImage;
  String? backgroundImage;
  List<User> friends;
  List<User> favorites;

  User(
      {required this.name,
      required this.phoneNum,
      required this.email,
      this.nickName,
      this.profileImage,
      this.backgroundImage,
      required this.friends,
      required this.favorites});
}

class UserData {
  String id;
  DateTime birth;
  String email;
  DateTime lastLoginData;
  String name;
  String phoneNumber;

  UserData(
      {required this.id,
      required this.birth,
      required this.email,
      required this.lastLoginData,
      required this.name,
      required this.phoneNumber});

  UserData.dummy()
      : id = 'DUMMY',
        birth = DateTime.now(),
        email = 'dummyData@dummy.com',
        lastLoginData = DateTime.now(),
        name = '더미데이터',
        phoneNumber = '010' {
    print("id : $id");
    print("birth : $birth");
    print("email : $email");
    print("lastLoginData : $lastLoginData");
    print("name : $name");
    print("phoneNumber : $phoneNumber");
  }

  get getAge {
    return DateTime.now().year - birth.year;
  }

  updateUserName(String name) {
    this.name = name;
    return this.name;
  }
}
import 'package:class_practice_app/model/User.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  UserData userData1 = UserData(
      id: "sniperFactory",
      birth: DateTime.parse('1990-01-01'),
      email: 'sniperfactory@naver.com',
      lastLoginData: DateTime.now(),
      name: '스나이퍼',
      phoneNumber: '01023456789');
  UserData userData2 = UserData(
      id: 'id',
      birth: DateTime.now(),
      email: 'sniperfactory@naver.com',
      lastLoginData: DateTime.now(),
      name: '스나이퍼',
      phoneNumber: '01023456789');

  @override
  Widget build(BuildContext context) {
    print(UserData.dummy().id);
    print("나이는 : ${userData1.getAge}살 입니다");
    print(userData2.name);
    userData2.updateUserName('팩토리');
    print(userData2.name);
    return Container();
  }
}
name27 commented 1 year ago

private 멤버 변수에 직접 접근이 불가능할 때 public인 getter를 통해 접근 가능, 변경할 때 public인 setter를 통해 변경 가능

name27 commented 1 year ago

private로 선언하는 방법: 이름 앞에 _ underscore 붙이기

private를 사용하는 이유

의도하지 않은 곳에서 접근하여 값을 변경하는 일이 발생할 수 있다. 만약 그런 일이 발생하면 수정하는 일이 쉽지 않고 여기저기서 참조하고 있으면 어디서 잘못된 값으로 변경했는지 모두 확인해야하는 일이 생긴다. 이런 일을 방지하고자 클래스의 내부 정보를 공개하지 않도록 하는 정보 은닉 방법 중 한 가지가 있다.

정보 은닉은 캡슐화를 통해 할 수 있다. 멤버 변수를 private로 선언하고 해당 변수에 접근할 수 있는 메서드를 public으로 선언하면 멤버 변수에 직접적으로 접근하는 것을 막을 수 있다. 그런 역할을 하는 메서드 Getter, Setter가 있다. 각각 get과 set이라는 키워드로 사용한다.