Summary of Dart basic functions, classes, objects, static members, operators, inheritance and abstract class knowledge points

Functions in Dart 1. Knowledge points related to functions: (1) Definition of function (2) Optional parameters - > (3...

Functions in Dart

1. Knowledge points related to functions: (1) Definition of function (2) Optional parameters - > (3) Default parameters (4) Named parameters (5) Arrow function (6) Anonymous function (7) Closure, etc 2. Built in method print(), etc. 3. User defined method Basic format: Return type (void, etc.) method name (funcName) (parameter 1, parameter 2,...){ Method body Return return value; } 4. Optional parameter age String printUserInfo(String username, [int age, type xxx, ...]){ return "Name: $username -- age: $age"; } print(printUserInfo('zhang San ', 20)); / / argument 5. Naming parameters String printuserinfo (string username, ){ return "Name: $username -- age: $age"; } print(printUserInfo('zhang San ', age:20,sex: "female)); / / argument

Classes and objects in Dart

1. Object oriented programming( OOP)The three basic characteristics are encapsulation, inheritance and polymorphism //Encapsulation: encapsulation is the main feature of object and class concepts. Encapsulate, encapsulate objective things into abstract classes, and provide some of their own attributes and methods to others //Inheritance: it can use the functions of existing classes, //Polymorphism: it is allowed to assign the pointer of subclass type to the pointer of parent type. The same function call will have different execution effects 2. Dart All things are objects, all objects are inherited from Object class 3. Dart Is an object-oriented language using classes and single inheritance. All objects are instances of classes, and all classes are Object Subclasses 4. A class is usually composed of properties and methods //An example of defining a base class: class Person{ String name = 'Zhang San'; int age = 23; void getInfo(){ print("$ --- $"); } } void main() { var p = new Person(); print(p.name); // Zhang San p.getInfo(); // Zhang San 23 } //Define classes with construction methods: class Person{ String name = 'Zhang San'; int age = 23; // Constructor Person(String name,int age){ this.name = name; this.age = age; this.getInfo(); // Wool --- 25 } // Another constructor shorthand for assignment // Person(this.name,this.age); void getInfo(){ print("$ --- $"); } } void main() { var p = new Person('Mao Mao',25); print(p.name); // Mao Mao } //Define a named constructor: class Person{ String name = 'Zhang San'; int age = 23; // Constructor Person(String name,int age){ this.name = name; this.age = age; this.getInfo(); // Wool --- 25 } // Named constructor Person.now(){ print('I'm a named constructor'); } // Get user information void getInfo(){ print("$ --- $"); } } void main() { var p = new Person.now(); // I'm a named constructor print(p.name); // Zhang San }

Inheritance of class static member operator class in Dart

Dart Static members in 1. Use static Key to implement class level variables and functions 2. Static methods can only access static members, non static methods can access static members //Code example: class Person{ int age = 20; String nameP = 'Zhang San'; // Static attribute static String name = 'Zhang San'; // Static method static void printInfo(){ print('This\'s a static function named printInfo.' ); } // Get user information static void getInfo(){ // this is not accessible in static static methods // print("$name --- $age"); // error Getter not found: 'age'. print(name); // Zhang San printInfo(); // This's a static function named printInfo. } } void main() { Person.getInfo(); }

Object operators in Dart

? Conditional operator // Person p; P as Type converter // If (P is person) p is Object is Type judgement // Judgement type .. Cascade operation (concatenation, similar to chain call operation) p1..name = "Zhang San 222" ..age = 30 ..printInfo();

Inheritance of classes in Dart:

1. The children use the extends keyword to inherit the parent class 2. The subclass will inherit the properties and methods visible in the parent class, but will not inherit the constructor 3. The subclass copies the getter and setter methods of the parent class Use of super keyword:
class Person { int age = 20; String nameP = 'Zhang San'; Person(this.nameP, this.age); //... } class Child extends Person{ Child(String name, num age) : super(name, age){ } } void main() { Child c = new Child('Li Si', 23); c.getInfo(); }
Method to copy the parent class:
class Child extends Person{ Child(String name, num age) : super(name, age); @override void printInfo(){ print("xxx xxx xxx xxx."); } }
Call the method of the parent class in the child class:
super.funcName();

Abstract class, polymorphism and interface in Dart

Dart abstract class

Dart abstract classes are mainly used to define standards. Subclasses can inherit abstract classes or implement abstract class interfaces.

1. Abstract class is defined by abstract keyword 2. Abstract methods in Dart cannot be declared abstractly. Methods without method bodies in Dart are called abstract methods 3. If a subclass inherits an abstract class, it must implement the abstract methods in it 4. If the abstract class is implemented as an interface, all the properties and methods implemented in the abstract class must be implemented 5. An abstract class cannot be instantiated. Only the subclass that inherits it can The difference between extensions abstract class and implements: 1. If we want to reuse the methods in the abstract class and constrain ourselves with the abstract methods, we will use extends to inherit the abstract class 2. If we only regard abstract classes as standards, we will use implements to implement abstract classes Basic use of abstract classes:
// Define abstract class Animal abstract class Animal { eat(); // Abstract method } // Define Dog class to inherit abstract class Animal class Dog extends Animal{ @override eat(){ // TODO: implement eat print('dog eat something.'); return null; } run(){ print('run'); } } void main() { Animal dog = new Dog(); dog.eat(); // ok dog.run(); // error Dog d = new Dog(); d.eat(); // ok d.run(); // ok }
Polymorphism in Dart 1. It is allowed to assign the pointer of subclass type to the pointer of parent type. The same function call will have different execution effects 2. The instance of the subclass is assigned to the reference of the parent class 3. Polymorphism (a way of expression) is that the parent class defines a method not to be implemented, and lets its inherited subclass to be implemented. Each subclass has different performance Dart also has an interface, but it is different from Java (interface: specification, Convention)
1. First, dart No interface for interface Keyword defines an interface, but ordinary or abstract classes can be implemented as interfaces. 2. Equally applicable implements Keyword implementation 3. however dart The interface of is a little strange. If the implemented class is a normal class, it will copy all the methods of the properties in the normal class and the abstract 4. Because abstract classes can define abstract methods, ordinary classes can't, so in general, if the implementation is like Java In the way of interface, abstract classes are usually used 5. It is recommended to use abstract classes to define interfaces code: abstract class Db { // As an interface: Convention and specification String uri; add(); save(); delete(); } class Mysql implements Db{ @overrive String uri; @overrive add() { //... } @overrive save() { //... } @overrive delete() { //... } } class Mongo implements Db{ @overrive String uri; @overrive add() { //... } @overrive save() { //... } @overrive delete() { //... } } //...
My Xiaoying is short Blogger 240 original articles published, 95 praised, 380000 visitors+ His message board follow

6 February 2020, 01:57 | Views: 9575

Add new comment

For adding a comment, please log in
or create account

0 comments