We will look at how we can create classes and explore some various features. Dart adopts a single-inheritance model, meaning that you can only extend a single class. We will therefore extend our class example by creating an Employee subclass of a parent Person class.

A normal class:

class Person {
String name;
int age; Person(name, age) {
this.name = name;
this.age = age;
}
}

We can also use short hand syntax:

class Person {
String name;
int age; Person(this.name, this.age);
}

We can have optional arguements, override operator, and getter, setter:

class Person {
// private name
String _name;
int age;
String occupaction; // optional {this.occupation}
Person(this._name, this.age, {this.occupaction});
// optional [this.occupaction]
Person.fromJson(Map json, [this.occupaction]) {
_name = json['name'];
age = json['age'];
} // override == operator, define a custom one
bool operator ==(dynamic b) => _name == b.name && age == b.age && occupaction == b.occupation; String get name => _name;
void set name(String updateName) => _name = updateName; speak() {
print("My name is &_name. I'm $age years old.");
}
}

Use:

void main() {
Person johnny = Person('Johnny', , occupaction: 'WD')
..speak(); // My name is &_name. I'm 42 years old.
Person jane = Person.fromJson({'name': 'Johnny', 'age': , 'occupation': 'WD'});
print(jane == johnny); // true
}

Here '..speak()' the same as:

  Person johnny = Person('Johnny', , occupation: 'Pilot')
johnny.speak()

---

void main() {
Person johnny = Person('Johnny', 42, occupation: 'Pilot')
..speak()
..name = 'Big Johnny'
..speak(); print(johnny.name);
print(johnny.occupation); Person jane = Person.fromJson({'name': 'Jane', 'age': 39}, 'Web Developer');
jane.speak();
print(jane.occupation); print(johnny == jane);
Person jane2 = Person('Jane', 39, occupation: 'Web Developer');
print(jane == jane2); var bob = Employee('Bob', 23, DateTime.now());
bob.speak();
} class Employee extends Person {
Employee(String name, int age, this.joinDate): super(name, age); final DateTime joinDate; @override
speak() {
print('My name is $name. I joined on $joinDate');
}
} class Person {
Person(this._name, this.age, {this.occupation});
Person.fromJson(Map json, [this.occupation]) {
_name = json['name'];
age = json['age'];
} String _name;
int age;
String occupation; String get name => _name;
void set name(String updatedName) => _name = updatedName; // If overriding the == operator, you should also override the Object's `hashCode` getter
// Learn more at https://www.dartlang.org/guides/libraries/library-tour#implementing-map-keys
bool operator ==(dynamic b) => _name == b.name && age == b.age && occupation == b.occupation; speak() {
print("My name is $_name. I'm $age years old.");
} // void _hiddenMethod() {
// print('This method is hidden');
// }
}

  

[Dart] Understand Classes and Inheritance in Dart的更多相关文章

  1. [Dart] Understand Variables and Constants in Dart

    In this lesson, we will look at how to create variables and constants. These are containers that sto ...

  2. Dart语言学习( 一) 为什么学习Dart?

    为什么学习Dart? Google及全球的其他开发者,使用 Dart 开发了一系列高质量. 关键的 iOS.Android 和 web 应用. Dart 非常适合移动和 web 应用的开发. 高效 D ...

  3. [dart学习]第三篇:dart变量介绍 (二)

    本篇继续介绍dart变量类型,可参考前文:第二篇:dart变量介绍 (一) (一)final和const类型 如果你不打算修改一个变量的值,那么就把它定义为final或const类型.其中:final ...

  4. dart系列之:安全看我,dart中的安全特性null safety

    目录 简介 Non-nullable类型 Nullable List Of Strings 和 List Of Nullable Strings !操作符 late关键字 总结 简介 在Dart 2. ...

  5. Week 5: Object Oriented Programming 9. Classes and Inheritance Exercise: int set

    class intSet(object): """An intSet is a set of integers The value is represented by a ...

  6. Dart 基础重点截取 Dart 2 20180417

    官网教程 https://www.dartlang.org/guides/language/language-tour dart是一个单线程的语言,没有多线程 Final and const If y ...

  7. 用Dart写的黑白棋游戏

    2013年11月,Dart语言1.0稳定版SDK发布,普天同庆.从此,网页编程不再纠结了. 在我看来,Dart语法简直就是C#的升级版,太像了.之所以喜欢Ruby的一个重要理由是支持mixin功能,而 ...

  8. Flutter学习笔记(5)--Dart运算符

    如需转载,请注明出处:Flutter学习笔记(5)--Dart运算符 先给出一个Dart运算符表,接下来在逐个解释和使用.如下:                            描述       ...

  9. Dart语法学习

    Dart语法学习 目录 参考资料 语言特性 关键字 变量与常量 数据类型 运算符 operators 控制流程语句 异常 Exceptions 函数 Function 类 Class 类-方法 类-抽 ...

随机推荐

  1. 【转帖】linux date 显示指定时区的时间 借助TZ 环境变量 export TZ=Asia/Shanghai 或 America/New_York

    linux date 显示指定时区的时间 借助TZ 环境变量 export TZ=Asia/Shanghai 或 America/New_York 2015-02-10 10:58:22 youcha ...

  2. zblog常用到的几个标签介绍

    文章归档 <divclass="function"id="divArchives"> <h3><#ZC_MSG028#>&l ...

  3. ~request库的使用

    官方文档: (中文)http://cn.python-requests.org/zh_CN/latest/ (英文)https://2.python-requests.org//en/master/a ...

  4. Python 重点知识整理(基于Python学习手册第四版)

    字节型编译 如果Python在系统中有写的权限,当程序运行时Python会把源码编译成字节码(与系统环境无关)存在一个.pyc扩展名文件中,如果没有修改源码而重新运行程序时,不会进行编译的步骤而使用字 ...

  5. Springboot入门及配置文件介绍(内置属性、自定义属性、属性封装类)

    目的: 1.Springboot入门 SpringBoot是什么? 使用Idea配置SpringBoo使用t项目 测试案例 2.Springboot配置文件介绍 内置属性 自定义属性 属性封装类 Sp ...

  6. ClickOnce 部署 API 以编程方式检查应用程序更新

    private void InstallUpdateSyncWithInfo() { UpdateCheckInfo info = null; if (ApplicationDeployment.Is ...

  7. 关于Ad-hoc

    Ad-hoc是wifi的一个模式,依托普通无线局域网802.11家族.网络中所有结点的地位平等,无需设置任何的中心控制结点,省去了无线中介设备AP.比如一台电脑建立了一个网络,这个时候加入了两台电脑. ...

  8. 4.JUC之AQS框架

    一.简介 1.AQS AQS是AbstractQueuedSynchronizer的简写,直白的翻译:抽象队列同步器,jdk1.5后出现 Provides a framework for implem ...

  9. WebService 的发布和调用

    WebService 四种发布方式总结 :https://blog.csdn.net/zl834205311/article/details/51612207 调用webService的几种方式 ht ...

  10. SAP ETL DS

    如何设置Dataservices使用FTP传输方式_连接SAP系统 [如果DS的目标数据库选择使用Oracle,请务必确认以下数据库设置] If SAP R3 contents CJK charact ...