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 复制代码
...
相关推荐
我命由我123452 分钟前
Android 项目路径包含非 ASCII 字符问题:Your project path contains non-ASCII characters
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
ytttr8735 分钟前
基于MATLAB解决车辆路径问题(VRP)
开发语言·matlab
沛沛老爹7 分钟前
Web开发者突围AI战场:Agent Skills元工具性能优化实战指南——像优化Spring Boot一样提升AI吞吐量
java·开发语言·人工智能·spring boot·性能优化·架构·企业开发
Full Stack Developme10 分钟前
Redis 实现主从同步
java·redis·spring
一只爱学习的小鱼儿10 分钟前
在QT中使用饼状图进行数据分析
开发语言·qt·数据分析
yangminlei11 分钟前
Spring Boot 响应式 WebFlux 从入门到精通
java·spring boot·后端
曹轲恒16 分钟前
SpringBoot配置文件
java·spring boot
亓才孓28 分钟前
[认识异常和错误]java
java·开发语言
码农水水35 分钟前
中国电网Java面试被问:流批一体架构的实现和状态管理
java·c语言·开发语言·面试·职场和发展·架构·kafka