Menu
CoddyTech

암시적 인터페이스

Coddy의 Ruby 개발자를 위한 Dart 코스 레슨. 48개 중 34번째.

Ruby는 덕 타이핑에 흔히 의존합니다. 호출자는 해당 메서드를 지원하는 어떤 객체에서든 sound를 호출합니다. Dart 인터페이스는 명목적이므로 메서드가 우연히 일치하더라도 선언된 하위 유형 관계가 필요합니다. implements는 상속된 메서드 본문이 아니라 계약을 제공합니다. 구현을 재사용하려면 extends 또는 믹스인을 사용하세요.

모든 일반 Dart 클래스는 인스턴스 멤버로부터 암시적 인터페이스를 정의합니다. 비공개 멤버도 해당 인터페이스의 일부이지만, 해당 라이브러리 안에서만 볼 수 있습니다. implements는 구현 본문을 상속하지 않고 명목적 계약을 설정합니다.

완전한 Ruby 프로그램:

class Bell
  def sound; 'ding'; end
end
def listen(value); value.sound; end
puts listen(Bell.new)

완전한 Dart 프로그램:

abstract class Sound{String sound();}
class Bell implements Sound{String sound()=>'ding';}
void main(){Sound s=Bell();print(s.sound());}
challenge icon

챌린지

쉬움

Scorer 추상 클래스를 구현하세요. int score(String text)를 사용합니다. LengthScore implements Scorer는 ASCII 문자열 길이를 반환합니다. VowelScore implements Scorer는 소문자 a,e,i,o,u만 셉니다. functions.dart의 best(List<String> words,Scorer scorer)는 점수가 가장 높은 첫 번째 단어를 반환하며, 비어 있으면 null을 반환합니다. 입력은 length 또는 vowels로 시작하고 그 뒤에 ASCII 단어들이 옵니다. 가장 좋은 단어 또는 none을 출력합니다.

편집 가능한 파일에서 출력하지 마세요. 제공된 exercise.dart가 입력을 분석하고 출력을 반환합니다. 잠긴 드라이버는 공개 API를 독립적으로 테스트하고 결과를 줄바꿈 하나와 함께 출력합니다.

코드는 Scorer.dart, LengthScore.dart, VowelScore.dart, functions.dart, exercise.dart에 작성하세요. main.dart에는 제공된 입력/출력 코드가 있으며 편집할 수 없습니다.

직접 해보기

import 'dart:io';
import 'dart:convert';
import 'exercise.dart';
import 'Scorer.dart';
import 'LengthScore.dart';
import 'VowelScore.dart';
import 'functions.dart';
void check(bool condition){if(!condition)throw StateError('Public API contract failed');}
Future<void> main() async {
final raw=await stdin.transform(utf8.decoder).join();
final input=raw.replaceFirst(RegExp(r'\r?\n$'),'');
check(LengthScore().score('abc')==3 && VowelScore().score('area')==3);check(best(['x','aaa'],LengthScore())=='aaa');
stdout.writeln(solve(input));
}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

Ruby 개발자를 위한 Dart의 모든 레슨

직접 연습해 보세요: 온라인 Dart 컴파일러