강의

멘토링

커뮤니티

Inflearn Community Q&A

tech's profile image
tech

asked

[Code Factory] [Beginner] Flutter 3.0 App Development - Escape Beginner Status Today with 10 Projects!

Dart #2 Object Oriented Programming

질문!

Resolved

Written on

·

309

0

Idol blackpink = new Idol( ~~~ )

Idol blackpink = Idol( ~~ )

이렇게 둘 다 가능하다는 내용을 보았습니다.

const생성자를 적용시킬 경우, 후자는 const Idol( )을 하면 error가 발생하지 않는데, 전자의 경우에는 new const Idol( ) 을 해도, const new Idol()을 해도 error가발생하는데 어떻게 해야 할까요? 후자의 경우에는?

flutter클론코딩Flutter클론코딩

Answer 1

1

codefactory님의 프로필 이미지
codefactory
Instructor

안녕하세요!

new 키워드로 const 생성자를 사용할수는 없습니다. new 는 기본적으로 새로운 인스턴스를 생성시킨다는 뜻인데 Dart에서 똑같은 아규먼트로 인스턴스를 생성할경우 새로운 인스턴스를 생성하지 않습니다. 그렇기때문에 용어상 맞지 않아서 new 키워드 사용이 불가능 한 것 같습니다. 아래 예제를 참고해주세요.

void main() {

final test2 = const Test();

final test3 = const Test();

print(test2 == test3); // true

}

class Test{

const Test();

}

감사합니다!

tech's profile image
tech

asked

Ask a question