类的基本结构
Dart
// 1.1 最简单的类
class Person {
//共有属性
String name;
int age;
//私有属性(以下划线开头)
double _balance = 0;
double get balance => _balance;//提供方法给外部访问私有属性(读)
set balance(double value){ //提供方法给外部修改私有属性(写)
if(value >= 0 ){
_balance = value;
}
}
// 构造函数
Person(this.name, this.age,this._balance);
// 方法
void introduce() {
print('我叫$name,今年$age岁');
}
}
构造函数类型
默认构造函数
Dart
class Point {
double x, y;
Point(this.x, this.y);
}
命名构造函数
Dart
普通构造函数:类名(参数...)
命名构造函数:类名.名称(参数...)
Dart
class Point {
double x, y; //类的属性
Point(this.x, this.y);//普通构造函数
//命名构造函数1:Point.origin()
//创建一个圆点(0,0)
Point.origin() : x = 0, y = 0;
//命名构造函数2:Point.fromJson()
//从Json数据创建Point对象
//表示从名为json的Map中提取key为x和y的值作为一个Point数据
Point.fromJson(Map<String, double> json)
: x = json['x']!, //感叹号!表示非空断言
y = json['y']!;
}
为什需要命名构造函数?
场景1:创建特殊实例
Dart
class Rectangle {
double width, height;
// 普通构造函数
Rectangle(this.width, this.height);
// 命名构造函数:创建正方形
Rectangle.square(double side)
: width = side,
height = side;
// 命名构造函数:创建单位矩形
Rectangle.unit()
: width = 1,
height = 1;
}
void main() {
var rect1 = Rectangle(3, 4); // 普通矩形
var rect2 = Rectangle.square(5); // 正方形
var rect3 = Rectangle.unit(); // 单位矩形
}
场景2:从不同数据源创建
Dart
class User {
String name;
int age;
User(this.name, this.age);
// 从Map创建
User.fromMap(Map<String, dynamic> map)
: name = map['name'],
age = map['age'];
// 从JSON字符串创建
User.fromJson(String jsonString) {
var map = jsonDecode(jsonString);
name = map['name'];
age = map['age'];
}
}
重定向构造函数
自己不执行初始化。而是调用同一个类的另一个构造函数来完成任务
Dart
class Circle {
double radius;
//主构造函数
Circle(this.radius);
// 重定向构造函数 - 自己不初始化,委托给主构造函数
Circle.unit() : this(1.0); // 调用 Circle(1.0)
}
========================执行流程==============================
// 当你调用:
var c = Circle.unit();
// 实际执行:
1. Circle.unit() 被调用
2. Circle.unit() 说:"我不干活,去找 Circle(1.0)"
3. Circle(1.0) 被调用
4. Circle(1.0) 执行初始化:radius = 1.0
为什么需要重定向构造函数
场景1:提供默认值
Dart
class Rectangle {
double width, height;
// 主构造函数
Rectangle(this.width, this.height);
// 重定向:创建正方形
Rectangle.square(double side) : this(side, side);
// 重定向:创建单位矩形
Rectangle.unit() : this(1.0, 1.0);
// 重定向:创建默认矩形
Rectangle.defaultSize() : this(100.0, 50.0);
}
场景2:参数转换/验证
Dart
class Person {
String name;
int age;
Person(this.name, this.age);
// 重定向:确保年龄非负
Person.withAgeCheck(String name, int age)
: this(name, age >= 0 ? age : 0);
}
重定向构造函数与命名构造函数的区别
Dart
class Point {
double x, y;
Point(this.x, this.y);
// 方式1:命名构造函数(自己初始化)
Point.origin1() : x = 0, y = 0; // 自己初始化
// 方式2:重定向构造函数(委托初始化)
Point.origin2() : this(0, 0); // 委托给 Point(0, 0)
// 两个方式效果相同,但实现方式不同
}
常量构造函数
(const构造函数)用于创建编译时常量对象
Dart
class ImmutablePoint {
final double x, y;
const ImmutablePoint(this.x, this.y);
}
void main() {
// 使用const创建对象
var p1 = const ImmutablePoint(1, 2);
var p2 = const ImmutablePoint(1, 2);
var p3 = const ImmutablePoint(2, 3);
print(p1 == p2); // true - 相同的编译时常量,指向同一内存
print(p1 == p3); // false - 值不同
// 不使用const(即使调用const构造函数)
var p4 = ImmutablePoint(1, 2);
var p5 = ImmutablePoint(1, 2);
print(p4 == p5); // false - 不同对象
print(p1 == p4); // false - 常量 vs 非常量
}
继承与多态
Dart
...