[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 类-方法 类-抽 ...
随机推荐
- oracle的jdbc的版本与jdk对应关系
连接类型:1. JDBC OCI: oci是oracle call interface的缩写,此驱动类似于传统的ODBC 驱动.因为它需要Oracle Call Interface and Net8, ...
- Linux基础-09-磁盘分区、挂载及文件系统管理
1. 硬件设备与文件名的对应关系 1) 在Linux系统中,每个设备都被当初一个文件来对待. 2) 各种设备在Linux中的文件名 2. 硬盘的结构及硬盘分区 1) 为什么要进行硬盘分区: a) 更容 ...
- 从零开始学Flask框架-005
表单 Flask-WTF 项目结构 pip install flask-wtf 为了实现CSRF 保护,Flask-WTF 需要程序设置一个密钥.Flask-WTF 使用这个密钥生成加密令牌,再用令牌 ...
- golang之结构体使用注意事项和细节
1. 结构体的所有字段在内在中是连续的 2. 结构体是用户单独定义的类型,和其它类型进行转换时需要有完全相同的字段(名字.个数和类型) 3. 结构体进行type重新定义(相当于取别名),Golang认 ...
- 如何将本地的项目添加到github上
参考链接:http://note.youdao.com/noteshare?id=d0b7990a83b024b0172b6d5c5617a8d0&sub=659F216B9046420991 ...
- 微软商店一直安装不上Intel Media SDK DFP
具体表现为一直安装失败,但是下载进度条一直在,无法去除. 此方法来自 https://answers.microsoft.com/en-us/windows/forum/all/error-code- ...
- 异常:[vue/no-parsing-error] Parsing error:x-invalid-end-tag
- 网页调试js时,如何知道某个事件对应哪段js代码?
有时候我们需要知道某个事件对应的js代码,比如点击一个div元素时,出现下拉框,我想知道这个功能对应的js代码,那就可以按下图操作: 勾选click事件,重新运行,那么就会在每个click事件那里设置 ...
- 怎样在python中写多行语句
一般来说, 一行就是一条语句, 但有时语句过长不利于阅读, 一般会写成多行的形式, 这时需要在换行时使用反斜杠: \ name = "Lilei" age = 23 gender ...
- [转载]Linux下非root用户如何安装软件
[转载]Linux下非root用户如何安装软件 来源:https://tlanyan.me/work-with-linux-without-root-permission/ 这是本人遇到的实际问题,之 ...