dart中class类

在这里插入图片描述

Dart面向对象的介绍 以及Data内置对象

  • 面向对象编程(OOP)的三个基本特征是:封装、继承、多态

    • 封装:封装是对象和类概念的主要特性。封装,把客观事物封装成抽象的类,并且把自己的部分属性和方法提供给其他对象调用, 而一部分属性和方法则隐藏。

    • 继承:面向对象编程 (OOP) 语言的一个主要功能就是“继承”。继承是指这样一种能力:它可以使用现有类的功能,并在无需重新编写原来的类的情况下对这些功能进行扩展。

    • 多态:允许将子类类型的指针赋值给父类类型的指针, 同一个函数调用会有不同的执行效果 。

  • Dart所有的东西都是对象,所有的对象都继承自Object类。

  • Dart是一门使用类和单继承的面向对象语言,所有的对象都是类的实例,并且所有的类都是Object的子类

    一个类通常由属性和方法组成。


void main() {
    
    
  List list = new List();
  list.isEmpty;
  list.add('香蕉');
  list.add('苹果');

  Map m = new Map();
  m["username"] = "张三";
  m.addAll({
    
    "age": 20});
  m.isEmpty;

  Object a = 123;
  Object v = true;
  print(a); // 123
  print(v); // true
}

Dart中创建义类使用类

  • Dart是一门使用类和单继承的面向对象语言,所有的对象都是类的实例,并且所有的类都是Object的子类

class Person {
    
    
  String name = '雏田';
  int age = 18;
  void getInfo() {
    
    
    // print("$name  ------  $age"); // 第一种写法
    print("${this.name} ----  ${this.age}"); // 第二种写法
  }

  void setinfo(int age) {
    
    
    this.age = age;
  }
}

void main() {
    
    
  // 实例化
  var p1 = new Person();
  print(p1.name); // 雏田
  p1.getInfo(); // 雏田 ----  18

  print('------------------------------------');

  Person p2 = new Person();
  p2.setinfo(26);
  p2.getInfo(); // 雏田 ----  26
}

Dart中自定义类的默认构造函数

class Person {
    
    
  String name = '雏田';
  int age = 18;

  // 默认构造函数
  Person() {
    
    
    print('这是构造函数里面的内容  这个方法在实例化的时候触发');
  }

  void printInfo() {
    
    
    print("${this.name} ----  ${this.age}"); // 第二种写法
  }
}

class Persons {
    
    
  String name;
  int age;
  // 默认构造函数
  /*  Persons(String name, int age) {
    this.name = name;
    this.age = age;
  } */
  //默认构造函数的简写
  Persons(this.name, this.age);

  void printInfo() {
    
    
    print("${this.name} --- ${this.age}");
  }
}

void main() {
    
    
  Person p1 = new Person();
  p1.printInfo();
  /* 
    这是构造函数里面的内容  这个方法在实例化的时候触发
    雏田 ----  18
   */

  Persons p2 = new Persons('五更琉璃', 16);
  p2.printInfo();
  /* 
    五更琉璃 --- 16
   */

  Persons p3 = new Persons('嘉怡', 25);
  p3.printInfo();
  /* 
      嘉怡 --- 25
  */
}

Dart中自定义类的命名构造函数

  • Dart中自定义类的命名构造函数
  • Dart里面构造函数可以写多个

class Person {
    
    
  String name;
  int age;

  // 默认构造函数的简写
  Person(this.name, this.age);

  Person.now() {
    
    
    print('我是命名构造函数');
  }

  Person.setInfo(String name, int age) {
    
    
    this.name = name;
    this.age = age;
  }

  void printInfo() {
    
    
    print("${this.name} ---- ${this.age}");
  }
}

void main() {
    
    
  var time = new DateTime.now(); // 实例化DateTime调用它的命名构造函数
  print(time); // 2021-01-24 22:48:26.483371

  Person p1 = new Person.setInfo('雏田', 18); // 默认实例化类的时候调用的是 默认构造函数
  p1.printInfo(); // 雏田 ---- 18

  Person p2 = new Person.now(); //命名构造函数
  /* 
    我是命名构造函数
   */
}

Dart中把类单独抽离成一个模块

Person.dart


class Person {
    
    
  String name;
  int age;
  // 默认构造函数的简写
  Person(this.name, this.age);

  Person.now() {
    
    
    print('我是命名构造函数');
  }

  Person.setInfo(String name, int age) {
    
    
    this.name = name;
    this.age = age;
  }
  void printInfo() {
    
    
    print("${this.name} ----- ${this.age}");
  }
}

index.dart


import 'lib/Person.dart';

void main() {
    
    
  Person p1 = new Person.setInfo('兔子零', 10000);
  p1.printInfo(); // 兔子零 ----- 10000
}

Dart中的私有方法 和私有属性

Animal.dart


class Animal {
    
    
  String _name; //私有属性
  int age;
  // 默认构造函数的简写
  Animal(this._name, this.age);

  void printInfo() {
    
    
    print("${this._name} ---- ${this.age}");
  }

  String getName() {
    
    
    // 获取私有属性
    return this._name;
  }

  void _run() {
    
    
    print('这是一个私有方法');
  }

  execRun() {
    
    
    // 调用私有方法
    this._run(); // 类里面方法的相互调用
  }
}


index.dart

  • Dart和其他面向对象语言不一样,Data中没有 public private protected这些访问修饰符合
    但是我们可以使用_把一个属性或者方法定义成私有。
import 'lib/Animal.dart';

void main() {
    
    
  Animal a = new Animal('大黄', 3);
  print(a.getName()); // 大黄
  a.execRun(); // 这是一个私有方法
}

类中的getter和setter修饰符的用法

class Rect {
    
    
  int height;
  int width;

  Rect(this.height, this.width);

  getArea() {
    
    
    return this.height * this.width;
  }
}

class Rect1 {
    
    
  int height;
  int width;

  Rect1(this.height, this.width);

  // 类似vue种的计算属性 computed
  get area {
    
    
    return this.height * this.width;
  }

  set areaHeight(value) {
    
    
    this.height = value;
  }
}

void main() {
    
    
  Rect r = new Rect(10, 2);
  print("面积:${r.getArea()}"); // 面积:20

  Rect1 r1 = new Rect1(20, 2); // 面积:40
  print("面积:${r1.area}"); //注意调用直接通过访问属性的方式访问area

  r1.areaHeight = 200;
  print("面积:${r1.area}"); // 面积:400
}

类中的初始化列表


class Rect {
    
    
  int height;
  int width;
  Rect()
      : height = 10,
        width = 3 {
    
    
    print("${this.height} ---- ${this.width}");
  }

  getArea() {
    
    
    return this.height * this.width;
  }
}

void main() {
    
    
  Rect r = new Rect();
  print(r.getArea());
  /* 
    10 ---- 3
    30
   */
}


猜你喜欢

转载自blog.csdn.net/weixin_43764814/article/details/113100931