Dart study notes

For the first time, I posted a blog. The whole layout is not very beautiful. Recently, I have been studying Dart (the soul of fluent. I think it doesn't make much sense to just do components). It may be that there are too many scientific Internet access, and Google is a little deep. Therefore, make some useful things into a document, write some core things, and gradually improve them, Finally, a complete learning document is made, which is worthy of learning dart. Which other God can popularize what dart said on its official website that this ghost can be combined with the Internet of things. There should be no example yet

metadata

  • Metadata starts with @ to mark the code with some additional information
  • Metadata can be used in front of libraries, classes, constructors, functions, fields, parameters, and variable declarations

@override

  • When a method adds this annotation, it means that it overrides the method with the same name in the parent class

@Required (required)

  • The named parameter in Dart can be annotated with @ required to indicate that it is a required parameter

@deprecated

  • If a class or method is annotated, it means that the method or class is no longer recommended
void main(){
    Phone p=Phone();
    p.active();
}

class Phone{
    @deprecated
    active(){
        turnOn();
    }
    
    turnOn(){
        print('Power on');
    }
}

inherit

  • According to the order of classes, classes can be divided into parent and child classes

  • The subclass inherits the parent class through the extends keyword

    • After inheritance, the subclass can use the visible contents (properties and methods) in the parent class
  • In subclasses, you can mark "override" methods through @ override metadata

    • Override method: a method in a subclass with the same name as in the parent class
  • In a subclass, you can use the super keyword to reference the visible content in the parent class

    • attribute
    • Method (normal constructor, named constructor)
//Simple inheritance
void main(){
    var f=Father();
    print(f.name);
    
    var s=Son();
    print(s.name);
    s.say();
    
}
class Father{
    String name="Liu Bei";
    num money=1000;
    say(){
        print('I am $name');
    }
}
class Son extends Father{
    @override
    say(){
        print('I'm Liu Chan');
    }
}
//Access to private properties and methods, Father class
class Father{
	String name='Liu Bei';
	num _money=1000;
	
	say(){
		print('My name is $name');
	}
	
	get getMoney(){
		print('Treasury has $_money');
	}
}

//Private property and method access, Son class
class Son extends Father{
	@override
	say(){
		print('I'm Liu Chan');
		super.getMoney();
	}
	
}
void main(){
    var f=Father();
    var s=Son();
    s._getMoney;  
}

Static properties and methods

  • Static members in Dart
    • Use the static keyword to implement class level variables and functions

    • Static methods cannot access non static members, and non static methods can access static members (and do not use this to specify)

    • Advantages and disadvantages of static method:

      Features: 1. static modified methods and variables are the methods and variables of the class. They will not be destroyed when the object is destroyed, so the life cycle is long.

      2. The content modified by static will be loaded with the loading of the class, which takes precedence over the existence of the object. Member variables and member methods can be called directly through the class name and shared by all objects under the class.

      3. In the static method, this, super cannot appear because it takes precedence over the object.

      Usage: in the same class, when a certain data is shared by all instantiated objects, it can be modified with static.

void main(){
    Person p=Person();
    p.get();
    Person.getInfo();
}

class Person{
    static String name;
    static void getInfo(){
        print('This is a static method');
    }
    void get(){
        getInfo();
    }
}

Object operator

Object operators in Dart:

  • ? Conditional operator
  • as type conversion
  • is type judgment
  • ... cascade operation
class Person{
    String name;
    num age;
    Person(this.name,this.age);
    void printInfo(){
        print('${this.name}-----${this.age}');
    }
}

void main(){
    Person p=Person('Dongbao',1);
    p.printInfo();//Because p only specifies the type and does not instantiate, an error will be reported. The application of conditional operators can be used for judgment
    p?.printInfo();//No error will be reported at present. Use? The conditional operator first determines whether p is empty (that is, whether it is instantiated). If it is empty, the method is not called
    (It's useless. The new version doesn't work)
        
    if(p is Person){
        p.name='winlong';
    }
    //Cascade operators to simplify the reassignment of operands
    p..name='xuena'
     ..age=12
     ..printInfo();
}

Class inheritance

  • Three characteristics of object-oriented: encapsulation, inheritance and polymorphism
  • Class inheritance in Dart:
    • Subclasses use the extends keyword to inherit from the parent class
    • Subclasses inherit the properties and methods visible in the parent class, but do not inherit constructors
    • Subclasses can duplicate the methods, getter s and setter s of the parent class
    • The format of subclass calling parent method is super xxx();
class Person{
    String name;
    num age;
    
    void printInfo(){
        print('${this.name}-----${this.age}');
    }
}

class Student extends Person{
    
}

void main(){
    var s=Student();
    print(s.name);
    s.printInfo();
}
class Person {
  String name;
  num age;

  Person(this.name, this.age);

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

class Student extends Person {
  String sex;
  Student(name, age,sex) : super(name, age){
      this.sex=sex;
  };//Inherit the constructor of the parent class and pay attention to the mutual transmission of parameters. At the same time, the construction method of the child class can also create its own properties and pass values
}

void main() {
  Student s = Student('Guozhen', 59);//Instantiate a subclass and pass parameters to the parent class
  print(s.name);
  s.printInfo();
}

abstract class

Abstract classes in Dart: Dart abstract classes are mainly used to define standards, and subclasses can inherit

Abstract classes can also implement the interface of abstract classes

  • Abstract classes are defined by the abstract keyword
  • Abstract methods in Dart cannot be declared with abstract. Methods without method body in Dart are called abstract methods
  • If a subclass inherits an abstract class, it must implement the abstract methods in it
  • If you regard an abstract class as an interface implementation, you must implement all the properties and methods defined in the abstract class
  • Abstract classes cannot be instantiated, only subclasses that inherit them can

Differences between extensions abstract class and implements:

  • If we want to reuse the methods in the abstract class and use the abstract method to constrain our own class, we will inherit the abstract class with extensions
  • If we only take abstract classes as the standard, we will use implements to implement abstract classes

Case: defining an Animal class requires that its subclass must contain the eat method

abstract class Animal {
  eat();
}

class Dog extends Animal {
  @override
  eat() {
    print('Dog is eating');
  }

  run() {
    print('This is a common method');
  }
}

void main(List<String> args) {
  var d = Dog();//An abstract class cannot be instantiated directly, only its subclasses can be instantiated
  d.eat();
  d.run();
}

polymorphic

Polymorphisms in Dart:

  • It is allowed to assign the pointer of the subclass type to the pointer of the parent type. The same function call will have different execution results

  • An instance of a subclass is assigned a reference to the parent class

  • Polymorphism means that the parent class defines a method not to be implemented, and lets its inheriting subclasses implement it. Each subclass has a different performance

abstract class Animal {
  eat();
}

class Cat extends Animal {
  @override
  eat() {
    print('Kittens eat fish');
  }

  run() {
    print('The kitten runs fast');
  }
}

void main(List<String> args) {
  Animal c = Cat();
  c.eat();
  //c.run(); Can't access. Because the subclass instance is assigned to the parent class reference, C of Animal type can only use the methods of the parent class
}

Interface

Like Java, dart has interfaces, but it is different from Java.

First of all, dart's interface does not have the interface keyword to define the interface, but ordinary classes or abstract classes can be implemented as interfaces

It is also implemented using the implements keyword

But dart's interface is a little strange. If the implemented class is an ordinary class, all the methods of the attributes in the ordinary class and the abstraction will be used

It needs to be rewritten

Because abstract classes can define abstract methods, ordinary classes cannot. Therefore, if you want to implement a Java interface, you will generally use abstract classes

It is recommended to use abstract classes to define interfaces

Interfaces are conventions and specifications

Requirement: define a DB database that supports MySQL, msSQL and MongoDB

MySQL, msSQL and MongoDB all have the same method

abstract class DB {
  connection();
}

class MySQL implements DB {
  @override
  connection() {
    print("connect");
  }
}

void main(List<String> args) {
  var m = MySQL();
  m.connection();
}

A class implements multiple interfaces

In OOP, a class can implement multiple interfaces. For example, a computer can plug in several USB devices (mobile hard disk, USB flash disk, e-book, etc.), and all methods in the interface must be rewritten (that is, you can identify and make these devices available when you plug in many devices in your computer). However, note that multiple classes cannot implement one interface at the same time, For example, a computer cannot use a USB flash drive at the same time.

abstract class Run{
    jogging();
    crashing();
}

abstract class Eat{
    slowlyEat();
    quicklyEat();
}

class Person implements Run,Eat{
    @override
    jogging(){
        print('take a walk');
    }
    @override
    crashing(){
        print('sprint');
    }
    @override
    slowlyEat(){
        print('Chew and swallow slowly');
    }
    @override
    quicklyEat(){
        print('Devour');
    }
}
void main(){
    var p=Person();
    p.jogging();
    p.crashing();
    p.slowlyEat();
    p.quicklyEat();
}


Keywords: OOP Polymorphism encapsulation abstract class dart

Added by fooDigi on Sat, 22 Jan 2022 21:41:17 +0200