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. k8s ingres 的安装与使用

    1. 安装. 从ingress的官网下载yaml文件. https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy ...

  2. Python30之文件2(文件系统)

    一.在python中对于文件系统的访问一般使用的是os模块.python是跨平台的,因此在使用os模块时,不需要关心是在什么系统下使用的 import os >>> os.listd ...

  3. (一)构建基于ubuntu docker MySQL 5.6 镜像并推送到Docker Hub

    一,创建目录二,文件准备三,构建四,使用五,在宿主机上连接docker 中的mysql六,推送镜像到Docker hub 一,创建目录 mkdir -p mysql/5.6 二,文件准备 注意执行脚本 ...

  4. 【并发】8、借助redis 实现多线程生产消费阻塞队列

    顾名思义这个就是再消费的时候,不是之前的那哥用yield进行线程切换的操作,而是用线程等待阻塞的方式去执行,说实话我感觉效率不一定有之前那个好, 因为我对这种阻塞队列使用的时候,之前有发现阻塞队列,塞 ...

  5. vue 下拉刷新数据的插件的使用:

    1.安装: npm i vue-scroller -S npm install vue-scroller -D 2.在需要加载的页面中引入,或在公共js文件中引入: import VueScrolle ...

  6. react组件懒加载

    组件懒加载方式-:react新增的lazy const Alert = lazy(() => import('./components/alert')); export default func ...

  7. 游记-NOI2019

    Day -18 被各路julao们轮番吊打-- Day -12 鸽子F发布了笔试题库,然而并没有 "MLE全场记零分" 的操作 Day -8 广二体育馆机器装配完毕,误闯开幕式表演 ...

  8. interface Part1(接口详解)

    1. 在日常生活中,手机.笔记本电脑.平板电脑等电子产品提供了不同类型的接口用于充电或者连接不同的设备. 不同类型接口的标准不一样,例如电压.尺寸等. 2. 在C#语言中,接口也会定义一种标准,如果需 ...

  9. LunHui 的生命观

    LunHui 的生命观 来源 https://www.zhihu.com/question/346510295 作者:齐天大圣链接:https://www.zhihu.com/question/346 ...

  10. python 循环结构(for-in)

    循环结构(for-in) 说明:也是循环结构的一种,经常用于遍历字符串.列表,元组,字典等 格式: for x in y: 循环体 执行流程:x依次表示y中的一个元素,遍历完所有元素循环结束 示例1: ...