[Dart] Understand Classes and Inheritance in Dart
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的更多相关文章
- [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 ...
- Dart语言学习( 一) 为什么学习Dart?
为什么学习Dart? Google及全球的其他开发者,使用 Dart 开发了一系列高质量. 关键的 iOS.Android 和 web 应用. Dart 非常适合移动和 web 应用的开发. 高效 D ...
- [dart学习]第三篇:dart变量介绍 (二)
本篇继续介绍dart变量类型,可参考前文:第二篇:dart变量介绍 (一) (一)final和const类型 如果你不打算修改一个变量的值,那么就把它定义为final或const类型.其中:final ...
- dart系列之:安全看我,dart中的安全特性null safety
目录 简介 Non-nullable类型 Nullable List Of Strings 和 List Of Nullable Strings !操作符 late关键字 总结 简介 在Dart 2. ...
- 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 ...
- Dart 基础重点截取 Dart 2 20180417
官网教程 https://www.dartlang.org/guides/language/language-tour dart是一个单线程的语言,没有多线程 Final and const If y ...
- 用Dart写的黑白棋游戏
2013年11月,Dart语言1.0稳定版SDK发布,普天同庆.从此,网页编程不再纠结了. 在我看来,Dart语法简直就是C#的升级版,太像了.之所以喜欢Ruby的一个重要理由是支持mixin功能,而 ...
- Flutter学习笔记(5)--Dart运算符
如需转载,请注明出处:Flutter学习笔记(5)--Dart运算符 先给出一个Dart运算符表,接下来在逐个解释和使用.如下: 描述 ...
- Dart语法学习
Dart语法学习 目录 参考资料 语言特性 关键字 变量与常量 数据类型 运算符 operators 控制流程语句 异常 Exceptions 函数 Function 类 Class 类-方法 类-抽 ...
随机推荐
- IdentityServer4 学习三
ClientCredentials客户端类型实现 客户端应用向IdentityServer请求AccessToken,IdentityServer验证通过把AccessToken返回给客户端应用,客户 ...
- 【模拟】Clock
Clock 题目描述 wls有一个钟表,当前钟表指向了某一个时间.又有一些很重要的时刻,wls想要在钟表上复现这些时间(并不需要依次复现).我们可以顺时针转动秒针,也可以逆时针转动秒针,分针和时针都会 ...
- 案例(1)-- OOM异常
问题描述: 1.系统在执行某个操作时,必现OOM异常. 问题的定位: 1.排查代码,未发现问题. 2.在虚拟机启动时,添加参数:-XX:+HeapDumpOnOutOfMemoryError(当发生o ...
- DG环境恢复同步遇到报错ORA-00353ORA-00334以及ORA-00600[2619], [47745]
问题说明 客户环境主库4节点RAC11.2.0.4,单实例DG环境,DG由于空间不足,导致同步中断,由于DG备库未应用的归档主库都再,本次恢复的方式,是开启dg mrp进程,自动同步追上主库. 以下遇 ...
- JAVA 插入注解处理器
JDK1.5后,Java语言提供了对注解(Annotation)的支持 JDK1.6中提供一组插件式注解处理器的标准API,可以实现API自定义注解处理器,干涉编译器的行为. 在这里,注解处理器可以看 ...
- SD-定义定价用途的条件表(Condition Table)
https://www.fenginfo.com/815.html 条件表(Condition Table)是SAP条件技术体系中最基础的数据源,它们是一群特定的数据表,表名是以特定字母开头 + 三位 ...
- 2019.9.27,SAP成都研究院数字创新空间团队建设,射箭和游泳
2019年9月27日,秋高气爽,SAP成都研究院数字创新团队全体成员又迎来了一次团队建设活动.这次的主题是:射箭. 在正式活动之前,大家先享用了一顿泰式海鲜火锅: 吃饱喝足之后,我们来到了名为&quo ...
- 关于MUI页面之间传值以及刷新的问题
一.页面刷新问题 1.父页面A跳转到子页面B,B页面修改数据后再跳回A页面,刷新A页面数据 (1).父页面A代码 window.addEventListener("pageflowrefre ...
- spark 机器学习 ALS原理(一)
1.线性回归模型线性回归是统计学中最常用的算法,当你想表示两个变量间的数学关系时,就可以用线性回归.当你使用它时,你首先假设输出变量(相应变量.因变量.标签)和预测变量(自变量.解释变量.特征)之间存 ...
- PHP危险函数的持续学习
记录下遇到过的PHP危险函数 0x01 escapeshellarg()与escapeshellsmd()联合 先给出官方的定义: escapeshellarg ( string $arg ) : s ...