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 复制代码
...
相关推荐
躺平大鹅34 分钟前
Java面向对象入门(类与对象,新手秒懂)
java
初次攀爬者1 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺2 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端
Derek_Smart3 小时前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
NE_STOP4 小时前
MyBatis-mybatis入门与增删改查
java
孟陬7 小时前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端
想用offer打牌7 小时前
一站式了解四种限流算法
java·后端·go
华仔啊8 小时前
Java 开发千万别给布尔变量加 is 前缀!很容易背锅
java
也些宝9 小时前
Java单例模式:饿汉、懒汉、DCL三种实现及最佳实践
java