From entry to mastery

Static methods and static properties:

  • Static decorated methods and properties are called static methods and static properties. Note that static methods and static properties can only be accessed through class names, not through objects.
  • Static methods cannot access non static properties and non static methods, anyway
class Person{
  String name;
  int age;
  Person(this.name,this.age);
  printInfo(){
    //In the string, you can directly access the properties and methods of the class through ${}
    print('${this.name}---${this.age}');
  }
}

class Web extends Person{
  /**
    *super Is the constructor that calls the parent class
    *Constructors cannot inherit. When a subclass inherits a parent class, it needs super to implement the construction method of the parent class
    */
  Web(String name, num age) : super(name, age);
    
  // override is writable, not writable, recommended
  @override
  printInfo(){
    print('Subclass ${this.name}---${this.age}');
  }
}
main(){
  /**
   * Dart Middle object operator
   * ?Conditional operator
   * is Type judgement
   * ..Cascade operation
   */
  Person p1;
  // The following indicates that if p1 is empty, the following is not executed. If there is a value, the following is executed
  p1?.printInfo();
  Person p2=new Person('Zhang San',20);
  p2?.printInfo();

  p2..name='Li Si'
    ..age=20
    ..printInfo();
}  
Abstract classes: classes decorated with abstract

Abstract methods exist in abstract classes, but there is no method body. Subclasses that inherit abstract classes must implement the abstract methods of subclasses

abstract class Animal{
  // Abstract method, subclass must be honest abstract method
  eat(); 
}

class Dog extends Animal{
  @override
  eat() {
    print('I am a puppy.');
  }
}
main(){
  /**
   * Abstract class (abstract is mainly used to specify standards)
   * In dart, the definition of interface is the same as that of abstract class. In * * dart, the interface is not defined by interface**
   * Abstract class cannot be instantiated, only its subclass can be instantiated 
   * There must be all abstract methods in the interface, no ordinary methods, but there can be both ordinary methods and abstract methods in the abstract class
   */
  Dog d=new Dog();
  d.eat();
}  

A class can implement multiple interfaces, but it can only inherit abstract classes. If it imitates multiple inheritance, mixins can be used. As a mixins class, it can only inherit objects, not other classes. As a mixins class, it cannot have a constructor, as follows:

class A{
  printA(){
    print('A');
  }
}
class B{
  printB(){
    print('B');
  }
}
//If you mimic multiple inheritance, you can use mixins
class C with A,B{

}
main(){
  C c1=new C();
  c1.printB();
}  
Generics: can solve the problem of code reuse and unspecified type verification
// Generic methods, note that there are three places to add T
T getData<T>(T data){
  return data;
}
main(){
  
  print(getData<String>('My name is Tang Kaizhen'));
}  

Libraries in dart

import 'package:http/http.dart';
main(){
  /**
   *  library
   * dart The library is imported through import. Note that there is no from
   * Each dart file is a library
   * Library Classification:
   * 1,Custom library
   * 2,System built-in Library: import 'dart:xxx'
   * 3,Third party library, hosted in the pub package management system
   * Third party libraries can be found at the following address:
   * https://pub.dev/packages
   * 
   * Use the third-party library as follows:
   * 1,Create pubspec.yaml file
   * 2,Configure the pubspec.yaml file
   * 3,Run put get to get the remote library
   * 5,Use of document reference
   */
}  

con

Scan code attention to the public number, there are more exciting articles waiting for you.

Tags: Javascript

Posted on Mon, 04 Nov 2019 06:21:19 -0800 by xzilla