在这里看原文。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