09 Object classes in Dart

09-1 object classes

  • Object-oriented programming (OOP) is characterized by encapsulation, inheritance, and polymorphism

    • Encapsulation: Encapsulation is the main feature of object and class concepts. Encapsulate, encapsulate objective things into abstract classes, and provide part of their attributes and methods to other objects to call, and part of the attributes and methods are hidden.
    • Inheritance: A major feature of object-oriented programming (OOP) languages is inheritance. Inheritance is the ability to take the functionality of an existing class and extend it without having to rewrite the original class.
    • Polymorphism: Allows assigning a pointer of a subclass type to a pointer of a superclass type. The same function call may have different execution effects.
  • Everything in Dart is an Object, and all objects inherit from the Object class.

  • Dart is an object-oriented language that uses classes and single inheritance. All objects are instances of classes, and all classes are subclasses of Object

  • A class usually consists of attributes and methods.

  class Person {
    // String name = 'zhang3 ';
    // int age = 32;
    String name;
    int age;

    // Constructor is fired on instantiation
    // Person() {
      // print(' constructor fires on instantiation ');
    // }

    // Person(String name, int age) {
      // this.name = name;
      // this.age = age;
    // }

    Person(this.name, this.age);

    // Name the constructor
    Person.setInfo(String name, int age) {
      this.name = name;
      this.age = age;
    }

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

    void setInfo(int age) {
      this.age = age; }}void main() {
    Person p2 = new Person('Hiraku'.29);
    p2.setInfo(28);
    p2.getInfo();

    Person p3 = new Person.setInfo('Hiraku'.29);
    p3.getInfo();
  }
Copy the code

09-2 Private Properties

  • Dart has no private properties or private methods, unlike other object-oriented languages that have modifiers such as public Private protected
  • Separate the classes into a folder and add_Represents a private property

Setters and getters in class 09-3

  • setter
class Reac {
  num height;
  num width;

  Reac(this.height, this.width);

  get area {
    return this.height * this.width; }}void main() {
  Reac r = new Reac(8.16);

  print('the area:${r.area}');
}
Copy the code
  • getter
class Reac {
  num height;
  num width;

  Reac(this.height, this.width);

  set reacHeight(num val) {
    this.height = val;
  }

  get area {
    return this.height * this.width; }}void main() {
  Reac r = new Reac(8.16);
  r.reacHeight = 1;

  print('the area:${r.area}');
}
Copy the code

Initializers in class 09-4

class Reac {
  num height;
  num width;

  Reac():height = 20, width = 30{
    print('The ${this.height}= = =The ${this.width}');
  }

  set reacHeight(num val) {
    this.height = val;
  }

  get area {
    return this.height * this.width; }}void main() {
  Reac r = new Reac();

  print('the area:${r.area}');
}
Copy the code

10 Class static member operator class inheritance in Dart

Static members in the 10-1 Dart:

  • 1. Use the static keyword to implement class-level variables and functions
  • 2. Static methods cannot access non-static members. Non-static methods can access static members
class Person {
  static String name = 'Joe';
  static void show() {
    print(name); }}void main() {
  print(Person.name);
  Person.show();  
}
Copy the code
class Person {
  static String name = 'Joe';
  int age=20;
  
  static void show() {
    print(name);
  }
  void printInfo() {  /* Non-static methods can access both static and non-static members */
    // print(name); // Access static properties
    // print(this.age); // Access non-static properties

    show();   // Call static methods
  }
  static void printUserInfo() {// Static method
    print(name);   // Static attributes
    show();        // Static method

    // print(this.age); // Static methods cannot access non-static attributes
    // this.printInfo(); // Static methods cannot access non-static methods
    // printInfo();}}void main() {
  // print(Person.name);
  // Person.show(); 

  // Person p = new Person();
  // p.printInfo(); 

  Person.printUserInfo();
}
Copy the code

10-2 Object operators in Dart

  • ?Conditional operators (Understanding)
  • asType conversion
  • isType judgment
  • .Cascading operations (concatenation) (remember)
  class Person {
    String name;
    int age;
    Person(this.name, this.age);

    printInfo() {
      print('The ${this.name}--The ${this.age}'); }}void main() {
    Person p;
    p.printInfo(); / / an errorp? .printInfo();print(p is Person); // true
    print(p is Object); // true
  }

  void main() {
    var p1;
    p1 = ' ';
    p1 = new Person();
    (p1 as Person).printInfo();
  }

  void main() {
    Person p1 = new Person('Hiraku'.29);
    p1.printInfo(); // Hiraku--29

    p1.name = 'Ma Hong Qin';
    p1.age = 18;
    pi.printInfo(); // Ma Hongqin --18p1.. name ='Ma Hong Qin'
      ..age = 18
      ..pringInfo(); // Ma Hongqin --18
  }
Copy the code

10-3 Three main features of object orientation: encapsulation, inheritance and polymorphism

  • Class inheritance in Dart:
    • 1, subclass useextendsKeyword to inherit from the parent class
    • Subclasses inherit properties and methods visible from their parent class, but not constructors
    • 3. Subclasses can duplicate methods of their parent classesgettersetter

class Person {
  String name;
  int age;

  Person(this.name, this.age);
  Person.work(this.name) { // Name the constructor
    print('The ${this.name} is working');
  };

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

  void isJoking() {
    print('The ${this.name}---is joking'); }}class Web extends Person {
  String sex;

  Web(String name, int age, String sex) : super(name, age) {
    this.sex = sex;
  }

  void printFun() {
    print('The ${this.name}--The ${this.age}--The ${this.sex}'); }}class Design extends Person {
  String sex;

  Design(String name, int age, String sex) : super.work(name) {
    this.age = age;
    this.sex = sex;
  }

  void printFun() {
    super.isJoking(); // Subclasses call methods of their parent class
    print('The ${this.name}--The ${this.age}--The ${this.sex}');
  }

  @overage
  void printInfo() { // override the superclass method
    print('s name,The ${this.name}'); }}void main() {
  Web w = new Web('Hiraku'.29.'woman');
  Design d = new Design('Hiraku'.29.'woman');

  w.printFun();
  d.printFun();
}
Copy the code

11 Abstract class polymorphism and interfaces in Dart

Abstract classes in the 11-1 Dart

  • Abstract Classes in Dart: Dart abstract classes are used to define standards. Subclasses can inherit abstract classes or implement abstract class interfaces.

    • 1. The abstract class passesabstractKeyword to define
    • 2. Dart abstract methods cannot be declared with abstract. Dart methods without a method body are called abstract methods.
    • If a subclass inherits an abstract class, it must implement its abstract methods
    • If you use an abstract class as an interface implementation, you must implement all properties and methods defined in the abstract class.
    • 5. An abstract class cannot be instantiated, only subclasses that inherit it can
  • The difference between extends and implements:

    • 1. Extends extends an abstract class if we want to reuse methods from an abstract class and constrain our classes with abstract methods
    • 2. Implements abstract classes, if only as a standard

Polymorphisms in 11-2 Dart

  • It is possible to assign a pointer of a subclass type to a pointer of a superclass type. The same function call may have different execution effects
  • An instance of a subclass is assigned to a reference to the parent class
  • Polymorphism is when a parent class defines a method and does not implement it, leaving it to its subclasses, each of which behaves differently
abstract class Animal { // Abstract classes are defined by abstract
  Animal();
  eat(); Abstract methods, which are used to constrain subclasses, cannot be defined using abstract definitions. Instead, abstract methods are defined without a method body
  run();

  printCommonFunc() {
    print('I'm an abstract method of an abstract class'); }}class Dog extends Animal {
  @override
  eat() { // Abstract methods must be implemented
    print('The dog is eating a bone');
  }

  @override
  run() {
    print('The dog is walking');
  }

  printInfo() {
    print('I'm a puppy'); }}class Cat extends Animal {
  @override
  eat() {
    print('The kitten is eating cat food');
  }

  @override
  run() {
    print('Kitty is walking');
  }

  printInfo() {
    print('I'm a kitten'); }}void main() {
  Dog d = new Dog();
  d.eat();
  d.run();

  Cat c = new Cat();
  c.eat();
  c.run();

  Animal animalDog = new Dog();
  animalDog.eat();
  animalDog.printCommonFunc();
  // animalDog.printInfo(); // An error was reported because the instance of the subclass was assigned to a reference of the parent class

  Animal animalCat = new Cat();
  animalCat.eat();
}
Copy the code

11-4 Dart interface

  • Like Java, DART has interfaces, but there are differences
    • First, the DART interface does not have the interface keyword to define the interface. Instead, ordinary or abstract classes can be implemented as interfaces
    • The implements keyword is also used
    • Dart’s interface, however, is a bit odd. If you implement a generic class, you’ll need to overwrite all the methods of the generic class and the attributes in the abstraction
    • And because abstract classes can define abstract methods that ordinary classes can’t, it’s common to use abstract classes if you want to implement things like Java interfaces
    • It is recommended to define interfaces using abstract classes
abstract class Db {  // as an interface: specification
  String uri;  // Database connection address
  add();
  save();
  delete();
}

class Mysql implements Db {
  @override
  String uri;

  @override
  add() {
    // TODO: implement add
    return null;
  }

  @override
  delete() {
    // TODO: implement delete
    return null;
  }

  @override
  save() {
    // TODO: implement save
    return null; }}class MongoDb implements Db {
  @override
  String uri;

  @override
  add() {
    // TODO: implement add
    return null;
  }

  @override
  delete() {
    // TODO: implement delete
    return null;
  }

  @override
  save() {
    // TODO: implement save
    return null; }}Copy the code

12 A class in Dart implements multiple interfaces and Mixins in Dart

12-1 Implements multiple interfaces

abstract class A {
  String name;
  printA();
}

abstract class B {
  printB();
}

class C implements A.B {  
  @override
  String name;  
  @override
  printA() {
    print('printA');
  }
  @override
  printB() {
    // TODO: implement printB
    return null; }}void main(){
  C c=new C();
  c.printA();

}
Copy the code

12-2 Implements multiple interfaces

  • Mixins, which means to mixin other functions into a class
  • Mixins can be used in Dart to achieve something like multiple inheritance
  • Because the conditions for mixins change with Dart releases, here are the conditions for using mixins in ART2. x:
    • A mixins class can only inherit from Object, not other classes
    • 2. Classes that are mixins cannot have constructors
    • A class can mixins multiple mixins classes
    • Mixins are not inheritance, nor are they interfaces, but rather a completely new feature
  class Animal {
  String name;
  int age;
  Animal(this.name, this.age);

  funcAnimal() {
    print('The ${this.name}--The ${this.age}'); }}class A {
  String info = 'This is a attribute in A';

  funcA() {
    print('I'm A'); }}class B {
  funcB() {
    print('I'm B'); }}class C extends Animal with A.B {
  C(String name, int age) : super(name, age);
  funcC() {
    print('I am the method of C'); }}void main() {
  C c = new C('the cat'.3);
  c.funcA();
  c.funcB();
  c.funcC();
  c.funcAnimal();
  print(c.info);

  print(c is A); // true
  print(c is Animal); // true
}
Copy the code