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 小时前
Java多线程:CompletableFuture使用详解(超详细)
java·开发语言·spring boot·python·spring·spring cloud
千里马-horse6 小时前
BigInt
开发语言·bigint·napi·addon
Robot侠6 小时前
从 Python 到 Ollama:将微调后的 Llama-3/Qwen 一键导出为 GGUF
开发语言·python·llama·qwen
刺客-Andy6 小时前
JS中级面试题 50道及答案
开发语言·javascript·ecmascript
I'm Jie6 小时前
Gradle 多模块依赖集中管理方案,Version Catalogs 详解(Kotlin DSL)
android·java·spring boot·kotlin·gradle·maven
Java小白笔记6 小时前
BigDecimal用法示例
java·开发语言·spring boot
l1t6 小时前
Python 字符串反转方法
linux·开发语言·python
Eiceblue6 小时前
使用 Python 写入多类型数据至 Excel 文件
开发语言·python·excel
luquinn6 小时前
用canvas切图展示及标记在原图片中的位置
开发语言·前端·javascript