Methods in Dart

  • A method is also an object: the scene is passed as a parameter
  • The return value and parameter types can be omitted
  • The arrow function => can be used when the method execution statement is one sentence long
  • Optional parameters: Parameters must be passed with the name of the parameter
  • Anonymous method: a method without a method name
  • Closure closure
    • A function defined within a function is a closure, and a closure is also an object.
    • What closures do: Access local variables of external functions.
void testClosure() { var func = funA(); func(); func(); func(); func(); var func2 = funA(); func2(); func2(); func2(); func2(); } funA() { int count = 0; Return () => print(count++); // This anonymous function is a closure}Copy the code

The execution result

The Dart OOP

End creates an OC based Flutter project, Flutter create-objc dart_OOP

Classes and objects

  • useclassThe keyword declares a class
  • You can usenewKeep up with constructors
  • All objects inherit from the Object class by default
  • Getter and setter methods are generated by default in Dart
  • Properties and methods go through point syntax. access
  • finalThe modified property must define an initial value
  • Method name is the same, parameter is not the same, method cannot overload!

Class constructor

  • Once a constructor is defined, the default constructor is invalidated
  • When all member attributes of an object arefinalThe object can be created as a constant object
  • To the constructorconst, constant object
void main() { JHPerson p = JHPerson(); p.name = 'Jack'; p.age = 22; p.run(); } class JHPerson { final String name2 = 'Jack2' String? name; int? age; int _weight; // Final height cannot be accessed by external files. JHPerson(int age, String name, int weight) {name = name; age = age; _weight = weight; final height; JHPerson(this.name, this.age, this._weight, this.height); // Name the constructor jhPerson.bigname (this.name, this._weight); Const JHPerson(this.name, this._weight); // Const JHPerson(this.name, this._weight); Void run() {print(' age $name in 🏃🏻♀️')} void Go () {print(' weight $name in Go')} void printP() {_go(); } void addAge(int age) { this.age = age; }}Copy the code

Dart’s factory constructs & singleton objects & initializer list

factory_class.dart

Class FactoryClass {// Need a single column object static FactoryClass? _instance; factory FactoryClass() => _instance ?? = FactoryClass._init(); //{ // if (_instance == null) { // _instance = FactoryClass._init(); // } // // return _instance! ; // / The private named constructor factoryClass._init (); }Copy the code

Initialization list

  • Objective:
  • Assign to the final variable
  • Validates passed parameter values
void personDemo() { Person p = Person('jack', -1, -2); } class Person { String name; init age; final height; Person(this.name, this.age, int h) : height = h, assert (h >= 0), assert (age >= 0 ){ print('name:$name age:$age height:$height'); }}Copy the code

Class methods and object operators

  • Class method, static
  • Static properties are accessed by class names
void staticDemo(){ //StaticClass st; //st.count; StaticClass.count = 12; print(StaticClass.sum(1)); StaticClass st = StaticClass(); st.currentCount = 10; StaticClass.count = 11; Print (st.sum2(20)); StaticClass st2 = StaticClass(); st2.currentCount = 1; print(st2.sum2(20)); } class StaticClass {// static int count = 1; Int currentCount = 0; static const String str = 'jack'; Static int sum(int a) {return a + count; Int sum2(int a) {return currentCount + a + count; } var s1 = Object(); s1 = StaticClass(); print((s1 as StaticClass).sum2(10)); // s1 = null; print(s1.sum2(10)); if (s1 is StaticClass) { print(s1.sum2(10)); print(s1.. currnetCount = 15.. sum2(20)); // chain programming, continuous call. The return value is the object itself}}Copy the code

The succession of the Dart

  • Extends a class using extends
  • Subclasses inherit properties and methods in addition to the constructor
  • Dart is single-inherited
  • Inheriting Object by default
  • Inherited subclasses inherit the default constructor
  • If the constructor has arguments/names, call it actively
  • The initialization list should be written insuperIn front of the
void extendsDemo() { Student st = Student(); st.run(); st.sutdy(); st.name = 'Jack'; st.age = 18; print(st.isFree); Person st1 = Studyent(); st1.run(); if (st1 is Student) { st1.sutdy(); st1.age = 17; print(st1.isFree); print(st1); } } class Student extends Person { final String subName; Student.withName(String? name) : subName = name! , super.withName(name); Student() : super.init(); Study () {print(' write homework '); } @override bool get isFree => age! The < 18; @override run() {print(' student is running '); } @override } class Person { Person.init(); String? age; int? age; int? _height; bool get isFree => _height! < 120; Run () {print(' run '); }}Copy the code

Dart’s abstract class interface

  • It’s kind of like a protocol interface
  • Is a class that cannot be instantiatedThe abstract modifier
  • To inherit an abstract class, you must implement its abstract methods
abstractDemo() { AbstractClass as = SubClass(); as.sum(10, 20); SubClass1 sc = SubClass1(); sc.sum(10, 20); sc.sum1(10, 20); Abstract class AbstractClass {int sum(int a, int b); Abstract class AbstractClass1 {} abstract class AbstractClass1 {} sum1(int a, int b); Abstract class AbstractClass2 {// abstract method, not implement int sum2(int a, int b); } class SubClass extends AbstractClass { @override int sum(int a, int b) { print('a + b = ${a + b}'); return a + b; } } class SubClass1 implements AbstractClass, AbstractClass1, AbstractClass2 { @override int sum(int a, int b) { print(subClass.. sum); return 0; } @override int sum1(int a, int b) { print(subClass.. sum1); return 0; } @override int sum2(int a, int b) { print(subClass.. sum2); return 0; }}Copy the code

Dart (multiple inheritance)Mixins are mixed in

  • Mixin, similar to multiple inheritance
  • As a mixin class, constructors cannot be implemented.
mixinDemo() { D d = D(); d.a(); d.b(); d.c(); } operatorDemo() { OperatorClass op1 = OperatorClass(18); OperatorClass op2 = OperatorClass(19); print(op1 > op2); } class OperatorClass { int age; OperatorClass(this.age); bool operator >(OperatorClass other) => this.age > other.age; } class A { a() => print('a.... '); } class B { b() => print('b.... '); } class C { a() => print('c.... '); // classD extends A with B,C {} classD= A with B,C; / / shortCopy the code