Static properties and static methods

In Dart, you can define static properties and static methods using the keyword static. It is important to note: static method cannot access non-static properties 【 conclusion 】 non-static methods can access static members 【 conclusion 】 2 us through the following a piece of code to explain 【 conclusion 】 and 【 conclusion 】 2 access static attributes or call a static method Direct access through the classCopy the code

Access static properties and static methods

Void main() {// call the static method personinfo.showinfo (); Print (personinfo.name); } class PerSonInfo {static String name = 'span '; int age; Static void showInfo() {【 conclusion 1】 // print(age); print(name); } show() {print(' name $name'); Print (' age $age); }}Copy the code

The cascading operator.. The use of

Void main() {var p = new PerSonInfo(' span ', 10); // p.name = 'li si '; // p.age = 50; // p.show(); // Equivalent to the above code p.. Name = 'li si'.. age = 50 .. show(); } class PerSonInfo { String name; int age; PerSonInfo(this.name, this.age); show() { print(this.name); print(this.age); }}Copy the code

Inheritance in DART

We all know that the dart an object-oriented language So the object-oriented language Inheritance, encapsulation and polymorphism, inherit the three basic characteristics: use a subclass extends keyword to inherit the parent class a subclass inherits the attributes and methods of the visible in the parent class But don't inherit the parent class constructor Subclasses can be to rewrite the parent class methodCopy the code
So let's write a Grils class that inherits PerSonInfo from the parent class even though there's nothing written in the Grils class let's see if we can inherit properties and methods from the parent class, okayCopy the code
void main() { var g = new Grils(); g.show(); } class PerSonInfo {String name=' three '; int age=10; show() { print(this.name); print(this.age); } } class Grils extends PerSonInfo {}Copy the code

Points to watch out for when inheriting

Class PerSonInfo {String name = 'PerSonInfo '; int age = 10; PerSonInfo(this.name,this.age); show() { print(this.name); print(this.age); }} class extends PerSonInfo {}Copy the code
Problem encountered: If we don't have a constructor for the PerSonInfo class, how does the PerSonInfo class only output information about one personCopy the code

A subclass calls the constructor super of its superclass

Void main() {var g = new Grils(' li si ',30); g.show(); } class PerSonInfo {String name = 'three '; int age = 10; PerSonInfo(this.name,this.age); show() { print(this.name); print(this.age); }} class Grils extends PerSonInfo {//super extends PerSonInfo {Grils(String name, int age) : super(name, age); }Copy the code

Subclasses can have their own properties and methods

Void main() {var g = new Grils(' li si ', 30, 'female '); g.show(); g.say(); } class PerSonInfo {String name = 'three '; int age = 10; PerSonInfo(this.name, this.age); show() { print(this.name); print(this.age); }} class Grils extends PerSonInfo {// class Grils extends PerSonInfo; Grils(String name, int age, String sex) : super(name, age) { this.sex = sex; } // Subclasses have their own method say() {print(' hello '); }}Copy the code

Call the subclass method first if the subclass is not looking for the superclass

void main() { var g = new Grils(); g.say(); } class PerSonInfo {say() {print(' hello, I'm a parent '); }} class Grils extends PerSonInfo {say() {print(' hello, I'm a subclass '); }}Copy the code

Pass arguments to the named constructor

Void main() {var g = new Grils(' li si ', 30, 'female '); g.show(); g.say(); } class PerSonInfo {String name = 'three '; int age = 10; PerSonInfo(this.name, this.age); PerSonInfo.mingFun(this.name, this.age); show() { print(this.name); print(this.age); } say() {print(' hello, I'm a parent '); } } class Grils extends PerSonInfo { String sex; Grils(String name, int age, String sex) : super.mingFun(name, age) { this.sex = sex; } say() {print(' hello, I'm a subclass '); }}Copy the code

Overwrite a method in a superclass

In the actual development of the project, the methods in the parent class may no longer be applicable and at that point we can override the methods in the parent class so that the code can be extended to override the methods in the parent class, without adding the semicolon @overrideCopy the code
Void main() {var g = new Grils(' li si ', 30, 'female '); g.say(); } class PerSonInfo { String name; int age; PerSonInfo(this.name, this.age); Say () {print(' hello, I'm a parent '); } } class Grils extends PerSonInfo { String sex; Grils(String name, int age, String sex) : super(name, age) { this.sex = sex; } @override say() {print(' hello, I'm a subclass '); }}Copy the code