static
static 사용하는 도중에 궁금한 점이 있습니다.
일단 제가 수강하면서 작성한 코드입니다.
void main() {
Employee seulgi = Employee('seulgi');
Employee chocho = Employee('chocho');
seulgi.printNameAndBuilding();
chocho.printNameAndBuilding();
Employee.building = 'OTO';
seulgi.printNameAndBuilding();
chocho.printNameAndBuilding();
Employee.printBuilding();
}
class Employee {
//static 은 instance에 귀속되지 않고 class에 귀속된다.
static String? building;
final String name;
Employee(
this.name,
);
void printNameAndBuilding() {
print('My name is $name. $building is my workplace');
}
static void printBuilding() {
print('im working on $building');
}
}
여기서 static을 사용할떄 Employee.building을 사용했는데
building은 constructor안에서 사용되지 않았으니 class같은데 맞나요?
그렇다면 class안에서 선언된 name도 static처럼 값을 바꿔도 되나요?
답변 1
1
안녕하세요.
값을 바꿔도 되고 안되고의 차이는 static과는 관련 없습니다. 위 예제에서 name을 한번 선언 후 바꿀 수 없는 이유는 name이 final로 선언되어있기 때문입니다. 반대로 building은 final이나 const로 선언돼있지 않기때문에 값 변경이 가능합니다.
그러니 질문에 답변드리면 building은 static으로 선언되었기 때문에 클래스 자체에 귀속되는 속성입니다.
인스턴스의 변수로 선언된 name은 값을 바꿀 수 없습니다. (final로 선언하셔서. static과 관련 x)
안드로이드 스튜디오 코드 하이라이팅하고 자동완성이 안됩니다.
0
240
2
프로젝트에서 dart map method가 보이지 않습니다.
0
112
1
LocationPermission.denied관련 질문입니다
0
587
1
videocontrollor dispose
0
430
1
onlogotap함수의 video nullable관련질문
0
389
1
setState 사용 차이
1
439
1
블로그 웹앱 배포 방법
0
505
1
sizedbox 위젯에 primary = RED_COLOR
1
432
1
개념 강의 순서 질문
0
407
1
materialstateProperty 다른 메소드관련질문
0
413
1
내 노트 모아보기
0
500
1
SafeArea (iPhone, Android)
0
407
1
web view 실행 안됩니다.
0
406
1
랜덤 숫자 생성기 프로젝트 관련
0
533
2
안도르이드 애뮬레이터 질문
0
382
1
super를 사용하는 건, 부모와 자식 클래스의 파라미터 이름이 꼭 같아야하나요?
0
471
1
선생님 flutter device를 크롬으로 했을때 http 통신이 안됩니다..
0
781
1
no element
0
438
1
암호화
0
233
1
책과 강의
0
384
1
voidcallback함수와 클래스내에 변수선언에 관한질문
0
474
1
android studio 세팅
0
478
1
오늘도 출근 세팅하기
2
562
1
재사용 클래스 임폴트 방법
0
478
1





