Dart 类

2022/11/14 Dart基础

本文介绍Dart的使用方法,并且介绍了类作为类型接口的使用和实现了类的继承

# 定义

  • class Car {
      num length;
      String color;
    
      Car(this.length, this.color);
    }
    
    1
    2
    3
    4
    5
    6

# 初始化列表

  • class Car {
      num length;
      String color;
      Map na1, na2;
    
      Car(this.length, this.color)
          : na1 = {'na': length},
            na2 = {'na': color};
    }
    final car = Car(110, 'red');
    print(car.na1);
    print(car.na2);
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

# 命名构造函数

  • class Car {
      Map na1, na2;
      // Map na3, na4;
    
      Car.first1(Map json)
          : na1 = {'first1': json['length']},
            na2 = {'first1': json['color']};
    
      Car.first2(Map json)
          : na1 = {'first2': json['length']},
            na2 = {'first2': json['color']};
    }
    final carFirst = new Car.first1({'length': 111, 'color': 'red'});
    final carFirst2 = new Car.first2({'length': 111, 'color': 'red'});
    print(carFirst.na1);
    print(carFirst2.na1);
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17

# callable

  • class Car {
      call(String name) {
        print('Name is $name');
      }
    
      cell(num nums) {
        call(nums.toString());
      }
    }
    final car = Car();
    car('123');
    car.call('123');
    car.cell(222123);
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14

# set 和 get

实际的业务场景就是可以在获取值的同时,去加工当前类的属性。

  • class Car {
      String? _color;
    
      Car();
    
      set color(String val) => _color = val;
    
      String get info => 'Car is $_color';
    }
    final car = Car();
    car.color = '123';
    print(car.info);
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

# 静态

  • class Car {
      static String carName = 'mustang';
      static void getCarName() => {print('car is name: $carName')};
    }
    print(Car.carName);
    Car.getCarName();
    
    1
    2
    3
    4
    5
    6

# abstract 类

  • abstract 类

    abstract类无法实例化

    abstract class Car {
      String carName = '';
      void getCarName();
    }
    
    1
    2
    3
    4

# 继承

  • 继承

    abstract class Car {
      String carName = '';
      void getCarName();
    }
    
    class Ferrari extends Car {
      
      String carName;
    
      Ferrari(this.carName);
    
      
      void getCarName() {
        print('car name is $carName');
      }
    }
    
    
    final ferrari = Ferrari('法拉利');
    print(ferrari.carName);
    ferrari.getCarName();
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
  • 父类调用

    class Car {
      String carName;
      Car(this.carName);
    
      void getCarName() {
        print('this is $carName');
      }
    }
    
    class Ferrari extends Car {
      Ferrari(super.carName);
    
      
      void getCarName() {
        super.getCarName();
        print('重写父类');
      }
    }
    final f = Ferrari('法拉利');
    print(f.carName);
    f.getCarName();
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
  • 调用父类构造

    class Car {
      String carName;
      Car(this.carName);
    
      void getCarName() {
        print('this is $carName');
      }
    }
    
    class Mustang extends Car {
      Mustang(String carName) : super(carName);
    }
    
    final m = Mustang('野马');
    print(m.carName);
    m.getCarName();
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
  • 多继承

    mixin Plane {
      void getCarName() {
        print('this is Plane');
      }
    }
    
    // 限定依赖
    mixin Car on Plane{
      void getName() {
        print('this is Car');
      }
    
      
      void getCarName() {
        print('this is Car');
      }
    }
    
    mixin Train {
      void getName() {
        print('this is Train');
      }
    
      void getTrainName() {
        print('this is Train');
      }
    }
    
    // with混入之前要根据依赖顺序混入
    class Mustang with Plane, Car, Train {
      String carName;
      Mustang(this.carName);
    }
    
    final m = Mustang('野马');
    print(m.carName);
    m.getCarName();
    m.getTrainName();
    // 如果函数重名则以后注入的为主
    m.getName();
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
  • 工厂

    • 调用子类

      abstract class Car {
        void call();
        factory Car(String type) {
          switch (type) {
            case "ferrari":
              return Ferrari();
            case "mustang":
              return Mustang();
            default:
              throw "The '$type' is not an animal";
          }
        }
      }
      
      class Ferrari implements Car {
        
        void call() {
          print('ferrari Calling...');
        }
      }
      
      class Mustang implements Car {
        
        void call() {
          print('mustang Calling...');
        }
      }
      
      void main() {
        var ferrari = Car('ferrari');
        var mustang = Car('mustang');
      
        ferrari.call();
        mustang.call();
      }
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
    • 单例模式

      class Car {
        static final Car _carName = Car._internal();
      
        Car._internal();
      
        factory Car() {
          // carName = name;
          return _carName;
        }
      
        void getCarName() {
          print('car $_carName');
        }
      }
      
      var ferrari = Car();
      var mustang = Car();
      
      print(identical(ferrari, mustang));
      
      Car().getCarName();
      Car().getCarName();
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
    • 减少重复实例对象

      class Car {
        String carName;
        Car(this.carName);
      
        factory Car.handleName(String name) => Car(name);
      
        void getCarName() {
          print('car $carName');
        }
      }
      
      void main() {
        var ferrari = Car('ferrari');
        var mustang = Car('mustang');
      
        ferrari.getCarName();
        mustang.getCarName();
      }
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18

# 类型

  • 类型
    abstract class Car {
      String carName = '';
      void getCarName();
    }
    
    class Mustang implements Car {
      
      String carName;
    
      Mustang(this.carName);
    
      
      void getCarName() {
        print('car name is $carName');
      }
    }
    
    final mustang = Mustang('野马');
    print(mustang.carName);
    mustang.getCarName();
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
最后更新时间: 2022/11/29 10:59:07