Flutter---类

类的基本结构
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 复制代码
...
相关推荐
网域小星球6 分钟前
C 语言从 0 入门(十六)|动态内存管理:malloc /free/calloc /realloc 精讲
c语言·开发语言·free·malloc·动态内存
雪的季节13 分钟前
qt信号槽跨线程使用时候的坑
java·开发语言·qt
AI应用实战 | RE17 分钟前
011、向量数据库入门:Embeddings原理与ChromaDB实战
开发语言·数据库·langchain·php
chh56319 分钟前
C++--内存管理
java·c语言·c++·windows·学习·面试
一直不明飞行29 分钟前
C++:string,写法s.find(‘@‘) != s.end()是否有问题
开发语言·c++·算法
白緢37 分钟前
嵌入式 Linux + 内核开发高频问题及排查
java·linux·运维
沐知全栈开发40 分钟前
C 预处理器
开发语言
daad77744 分钟前
WSL2_wifi驱动安装
开发语言·前端·javascript
juniperhan1 小时前
Flink 系列第4篇:Flink 时间系统与 Timer 定时器实战精讲
java·大数据·数据仓库·flink
超级大只老咪1 小时前
一维度前缀和解题通用模板(java)
java·开发语言·算法