[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', 42, occupaction: 'WD')
   ..speak(); // My name is &_name. I'm 42 years old.
  Person jane = Person.fromJson({'name': 'Johnny', 'age': 42, 'occupation': 'WD'});
  print(jane == johnny); // true
}

Here '..speak()' the same as:

  Person johnny = Person('Johnny', 42, occupation: 'Pilot')
  johnny.speak()

 

--- 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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');
//   }
}

  

posted @   Zhentiw  阅读(211)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2016-08-25 [WebGL] Setting Up WebGL
2016-08-25 [Redux] Accessing Dispatch and State with Redux -- connect
2015-08-25 [Angular 2] 8. Better ES5 Code
点击右上角即可分享
微信分享提示