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. RabbitMQ基础命令rabbitmqctl

    官网文档 https://www.rabbitmq.com/rabbitmqctl.8.html 一般操作命令后台管理页面都有的,部分没有(应用程序管理,和集群管理). 直接使用命令,必须配置环境变量 ...

  2. linux服务器搭建lnmp php 微擎环境备用

    以前的时候装个php环境各种的配置麻烦啊,于是乎我就像搜搜一键安装php环境,果然 lamp 和phpstudy 两个环境软件都支持,最后发现lamp 还合胃口就选择了lamp https://lnm ...

  3. SpringBoot之分页插件PageHelper的使用

    在springboot中使用PageHelper插件有两种较为相似的方式,接下来我就将这两种方式进行总结. 方式一:使用原生的PageHelper 1.在pom.xml中引入依赖 <depend ...

  4. PAT(B) 1071 小赌怡情(Java)

    题目链接:1071 小赌怡情 (15 point(s)) 题目描述 常言道"小赌怡情".这是一个很简单的小游戏:首先由计算机给出第一个整数:然后玩家下注赌第二个整数将会比第一个数大 ...

  5. Go实战--golang中使用redis(redigo和go-redis/redis)

    开源库redigo的使用 github地址: https://github.com/garyburd/redigo 文档地址: http://godoc.org/github.com/garyburd ...

  6. PB自动换行

    1.在DataWindow Painter中打开DataWindow; 2.在需设定自动折行的列上单击, 查看右侧的属性窗口: 3.点击Position标签, 选中Autosize Height 多选 ...

  7. hdu 1242 不用标记数组的深搜

    #include<stdio.h>#include<string.h>char mapp[220][220];int m,n,mmin;void dfs(int x,int y ...

  8. iOS - The file “XXX.app” couldn’t be opened because you don’t have permission to view it.

    当引入第三方的框架的时候 容易产生以下问题: The file “XXX.app” couldn’t be opened because you don’t have permission to vi ...

  9. BeautifulSoup库的安装与使用

    BeautifulSoup库的安装 Win平台:“以管理员身份运行” cmd 执行 pip install beautifulsoup4 演示HTML页面地址:http://python123.io/ ...

  10. djangoNotes

    学员管理 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...