在這裏看原文。ide
看這些教程的時候最好是打開dartpad。直接在裏面把這些代碼輸入進去看結果。這是dart官方提供的一個練習dart的地方。邊看邊練事半功倍。code
class interface_nasme { //... } class class_name implements interface_name { //... }
例如:繼承
class Person { void walk() { print("Person can walk"); } void talk() { print("Person can talk"); } } class Jay implements Person { @override void walk() { print("Jay can walk"); } @override void talk() { print("Jay can talk"); } } main() { Person person = new Person(); Jay jay = new Jay(); person.walk(); person.talk(); jay.walk(); jay.talk(); }
輸出:教程
Person can walk Person can talk Jay can walk Jay can talk
Dart不支持多繼承,可是能夠實現多個接口。接口
class class_name implements intgerface1, interface2 //,...
例如:get
class Person { String name; void ShowName() { print("My name is $name"); } void walk() { print("Person can walk"); } void talk() { print("Person can talk"); } } class Viveki { String profession; void ShowProfession() { print("from class Viveki my profession is $profession"); } } class Jay implements Person, Viveki { @override String profession; @override void ShowProfession() { print("from class Jay my Profession is $profession"); } @override String name; @override void ShowName() { print("From class Jay my name is $name"); } @override void walk() { print("Jay can walk"); } @override void talk() { print("Jay can talk"); } } main() { Person person = new Person(); Jay jay = new Jay(); Viveki viveki = new Viveki(); person.walk(); person.talk(); person.name = "Person"; person.ShowName(); print(""); jay.walk(); jay.talk(); jay.name = "JAY"; jay.profession = "Software Development"; jay.ShowProfession(); jay.ShowName(); print(""); viveki.profession = "Chemical Engineer"; viveki.ShowProfession(); }
output:io
Output Person can walk Person can talk My name is Person Jay can walk Jay can talk from class Jay my Profession is Software Development From class Jay my name is JAY from class Viveki my profession is Chemical Engineer