Introduction

Although a class in dart can only have one parent class, that is, single inheritance, dart provides a mixin syntax to bypass this restriction.

Today, let's discuss inheritance in the dart class with everyone.

Use extends

Like JAVA, you can define a parent class in dart, and then use extends to inherit him, and get a subclass, as shown below:

class Student{

}
class Boy extends Student{

}

In the subclass, you can use the super keyword to call the method of the superclass.

Abstract classes and interfaces

In addition to inheriting ordinary classes, dart can also inherit abstract classes and implement interfaces.

An abstract class is a class modified with abstract keywords, as shown below:

abstract class Student{
    String getName();
}

There are usually abstract methods in abstract classes, and abstract methods need to be implemented in subclasses.

Of course, abstract classes can also have concrete implementation methods, but abstract classes cannot be instantiated. If you want to instantiate objects in abstract classes, you can use the factory constructor we mentioned earlier.

Unlike java, there is no interface in dart, what he introduced is the concept of Implicit interfaces.

For each object, an interface containing all methods and properties in the class is implicitly defined.

Generally speaking, if an object contains the structure and methods of another object, but the content between them is different, you can use implements to implicitly implement the interface, as shown below:

class Student{

  String name;
  String get _name => name;
}

class Girl implements Student{
  @override
  String name;

  @override
  String get _name => "girls";
}

In dart, a class can implement multiple interfaces.

In the above example, we used the @override annotation, which means that the subclass overwrites the method or property of the parent class.

When using @override, we need to pay attention to the following restrictions on the implementation of the parent class by the subclass:

  1. The return value of the implementation method of the subclass must be the same as the return value of the parent class, or a subclass of the return value of the parent class.
  2. The parameters of the subclass's implementation method must be the same as the parent class method parameters, or the parent class of the parent class parameters.
  3. The number of parameters of the subclass method must be the same as the number of parameters of the parent class.

mixins

Although dart does not support multiple inheritance, you can use mixin to achieve functions similar to multiple inheritance.

To use mixins, you can use the keyword with, as shown below:

class Boy extends Student with Person {
  // ···
  name='boy';
  myName();
}

In dart, mixin is a special class, which is described by the keyword mixin. There is no constructor in the mixin class, as shown below:

mixin Person {
  String name='';

  void myName() {
      print('my name is:'+name);
  }
}

Useful methods and properties can be defined in mixin, and classes that inherit mixin can rewrite corresponding properties and methods to achieve custom functions.

In mixin we can also specify a specific class, that is to say, only a specific class can use mixin, you can use the keyword on, as shown below:

mixin Person on Boy{
  String name='';

  void myName() {
      print('my name is:'+name);
  }
}

Summarize

The above is the use of inheritance in dart, and methods can also be inherited in dart. This is an advanced application of dart. We will introduce it in a follow-up article, so stay tuned.

This article has been included in http://www.flydean.com/07-dart-extend/

The most popular interpretation, the most profound dry goods, the most concise tutorial, and many tips you don't know are waiting for you to discover!

Welcome to pay attention to my official account: "Program those things", know technology, know you better!


flydean
890 声望433 粉丝

欢迎访问我的个人网站:www.flydean.com