[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 类-方法 类-抽 ...
随机推荐
- Python35之包的创建
包(package) 一.创建一个文件夹,用于存放相关的模块,文件夹的名字即包的名字 二.在文件夹中创建一个__init__.py的模块文件,内容可以为空 三将相关的模块放入文件夹中 这样就相当于创建 ...
- 简单的鼠标操作<一个填充格子的小游戏>
#include "graphics.h" #include "conio.h" void main(){ // 初始化界面 initgraph(, ); ; ...
- PAT(B) 1062 最简分数(Java)
题目链接:1062 最简分数 (20 point(s)) 题目描述 一个分数一般写成两个整数相除的形式:N/M,其中 M 不为0.最简分数是指分子和分母没有公约数的分数表示形式. 现给定两个不相等的正 ...
- dotnet Core学习之旅(二):安装IDE
[重要:文中所有外链不能确保永久有效] >开发工具 高效的开发必然需要一个优秀的集成开发环境(IDE) 对于.NET Core 2.x可以使用包括但不限于以下IDE来进行开发. Visual S ...
- Singer House CodeForces - 830D (组合计数,dp)
大意: 一个$k$层完全二叉树, 每个节点向它祖先连边, 就得到一个$k$房子, 求$k$房子的所有简单路径数. $DP$好题. 首先设$dp_{i,j}$表示$i$房子, 分出$j$条简单路径的方案 ...
- Aop 打印参数日志时,出现参数序列化异常。It is illegal to call this method if the current request is not in asynchron
错误信息: nested exception is java.lang.IllegalStateException: It is illegal to call this method if the ...
- metasploit情报收集
1.msf连接数据库 service postgresql start(postgresql默认用户名scott,密码tiger) msf > db_connect 用户名:密码@127.0.0 ...
- python之函数基本使用
函数的定义: 函数是一段具有特定功能的.可重用的语句组,用函数名来表示并通过函数名进行功能调用. 使用函数主要有两个目的:降低编程难度和代码重用. python定义一个函数是通过使用def保留字的方式 ...
- FreeRTOS config开始的宏
FreeRTOSConfig.h系统配置文件中可以自定义,FreeRTOS.h中定义默认值 configAPPLICATION_ALLOCATED_HEAP 默认情况下FreeRTOS的堆内存是由编译 ...
- 虹软人脸识别 - Android Camera实时人脸追踪画框适配
在使用虹软人脸识别Android SDK的过程中 ,预览时一般都需要绘制人脸框,但是和PC平台相机应用不同,在Android平台相机进行应用开发还需要考虑前后置相机切换.设备横竖屏切换等情况,因此在人 ...