class in dart

Insert picture description here

Dart object-oriented introduction and Data built-in objects

  • The three basic characteristics of object-oriented programming (OOP) are: encapsulation, inheritance, and polymorphism

    • Encapsulation: Encapsulation is the main feature of object and class concepts. Encapsulation, encapsulate objective things into abstract classes, and provide some of its attributes and methods to other objects to call, while some of the attributes and methods are hidden.

    • Inheritance: One of the main functions of object-oriented programming (OOP) languages ​​is "inheritance." Inheritance refers to the ability: it can use the functions of an existing class and extend these functions without rewriting the original class.

    • Polymorphism: It is allowed to assign a pointer of a subclass type to a pointer of a parent type, and the same function call will have different execution effects.

  • Everything in Dart is an object, and all objects inherit from the Object class.

  • Dart is an object-oriented language that uses classes and single inheritance. All objects are instances of classes, and all classes are subclasses of Object.

    A class usually consists of attributes and methods.


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
}

Create Sense Class and Use Class in Dart

  • Dart is an object-oriented language that uses classes and single inheritance. All objects are instances of classes, and all classes are subclasses of 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
}

The default constructor of custom classes in 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
  */
}

Named constructor of custom class in Dart

  • Named constructor of custom class in Dart
  • You can write multiple constructors in 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(); //命名构造函数
  /* 
    我是命名构造函数
   */
}

Separate classes into a module in 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
}

Private methods and private properties in 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 is different from other object-oriented languages. There is no public private protected access modifier in Data,
    but we can use _ to define a property or method as private.
import 'lib/Animal.dart';

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

Usage of getter and setter modifiers in classes

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
}

Initialization list in the class


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
   */
}


Guess you like

Origin blog.csdn.net/weixin_43764814/article/details/113100931