Dart 06 class language learning

Dart language learning 06

Author : ScorpioDong

1. Class

Dart is an object-oriented language. It supports object-oriented programming, such as classes, interfaces and the like. OOP aspects of the class is to create a blueprint for an object / template. Class encapsulates the data object. Dart provides built-in support for the concept of class.

1.1 Statement of class

  • Use classkeyword to declare a class in the Dart.
  • Class definition keyword classat the beginning, followed by the class name.
  • Class name recommend using a large hump nomenclature.
  • Class name followed by the class body wrapped in a pair of braces.
class Test {
    // Fields
    String name;
    // Getters and Setters
    // Constructors
    Test() {
        this.name = "张三";
    }
    // Functions
    void eat() {
        print("$name eatting...");
    }
}

Class body Fields, Getters and Setters, Constructors and Functions are optional, defined demand

  • Fields (fields) - field any variable declared in the class, field represents data related to the object.
  • setters and getters - allow the initialization value of the field and retrieve the class, the default getter / setter associated with each class. However, the default values ​​can be overridden by explicitly defining setter / getter.
  • Constructors (constructors) - responsible to allocate memory for the object class.
  • Function (functions) - function of the operation target can be taken, they are sometimes also referred to as methods.

Examples of function attributes and access class 1.2

Use newexamples of keyword followed by the class name of the class is created.

main() {
    var obj = new Test();
    print(obj.name);
    obj.eat();
}
class Test {
    // Fields
    String name;
    // Getters and Setters
    // Constructors
    Test() {
        this.name = "张三";
    }
    // Functions
    void eat() {
        print("$name eatting...");
    }
}

Properties and methods of an object instance of the class accessed by a class. Use .points to access the accumulated data members.

1.3 Constructor

  • A constructor is a special class of functions, it is responsible for variable initialization class.
  • Dart constructor is the same function as the class name.
  • The constructor parameter may be present. Constructor has no return value.
  • If not a constructor, Dart a default empty constructor with no arguments.
class Test {
    // Fields
    String name;
    // Getters and Setters
    // Constructors

    Test() { // <--构造函数,初始化了name变量
        this.name = "张三";
    }
    // Functions
    void eat() {
        print("$name eatting...");
    }
}

Dart named constructor provided, so that the class can define multiple constructors.
grammar类名.构造方法名(形参列表) {函数体}

class Test {
    // Fields
    String name;
    // Getters and Setters
    // Constructors

    // 构造函数,初始化了name变量
    Test() {
        this.name = "张三";
    }

    // 命名构造函数
    Test.nameTest(String name) {
        this.name = name;
    }
    // Functions
    void eat() {
        print("$name eatting...");
    }
}

1.4 this keyword

this keyword refers to the current instance of the class. Here, the parameter name and class name field is the same. Therefore, in order to avoid ambiguity, the class of the field with this keyword prefix.

class Test {
    // Fields
    String name;
    // Getters and Setters
    // Constructors

    // 构造函数,初始化了name变量
    Test() {
        this.name = "张三"; // this关键字的使用
    }

    // 命名构造函数
    Test.nameTest(String name) {
        // this关键字的使用,因为字段名和形参名一样,冲突
        this.name = name; 
    }
    // Functions
    void eat() {
        print("$name eatting...");
    }
}

1.5 Getter and Setter

Getters and Setter (also called an access unit and Change) allows the program initializes the value of each class and retrieve the field. Use get keyword defines getter or accessor. Setter or access is defined using the set keyword.

The default getter / setter associated with each class. However, the default values ​​can be overridden by explicitly defining setter / getter. getter has no parameters and returns a value, setter only one parameter but does not return a value.

main(List<String> args) {
  var a = new Test();
  a.test_name = "李四";
  print(a.test_name);  
}

class Test {
    // Fields
    String name;
    // Getters and Setters

    // 定义name 的 Getter
    String get test_name {
        return name;
    }

    // 定义name 的 Setter
    void set test_name(String name) {
        this.name = name;
    }

    // Constructors

    // 构造函数,初始化了name变量
    Test() {
        this.name = "张三"; // this关键字的使用
    }

    // 命名构造函数
    Test.nameTest(String name) {
        // this关键字的使用,因为字段名和形参名一样,冲突
        this.name = name; 
    }
    // Functions
    void eat() {
        print("$name eatting...");
    }
}

1.6 package

  • Dart is not private, public and other rights modifier.
  • You can use _an underscore to prefix identifiers marked as private.
class Test {
    // Fields
    String _name; //标记私有外部无法访问
    // Getters and Setters

    // 定义name 的 Getter
    String get test_name {
        return _name;
    }

    // 定义name 的 Setter
    void set test_name(String name) {
        this._name = name;
    }

    // Constructors

    // 构造函数,初始化了name变量
    Test() {
        this._name = "张三"; // this关键字的使用
    }

    // 命名构造函数
    Test.nameTest(String name) {
        // this关键字的使用,因为字段名和形参名一样,冲突
        this._name = name; 
    }
    // Functions
    void eat() {
        print("$_name eatting...");
    }
}

1.7 Inheritance

  • Dart supports the concept of inheritance, it is the ability of the program to create a new class from an existing class. Extended to create a new class called class than the parent / superclass. The newly created class called child / subclass.
  • A class using extendsthe keyword inherited from another class. Subclass inherits all the properties and methods other than the parent class's constructor.
  • Dart class supports only single inheritance model, which is a class can have only one parent.
main(List<String> args) {
  var obj = new B();
  print(obj.a);
}

class A {
  int a = 5;
}

class B extends A {

}

carried out

5
Method overrides
  • Subclasses override mechanism is a method to redefine methods of its parent class.
  • Method override @overrideflag.
  • Method override the parent class and method name of the method, the parameter list and return type to be consistent.
main(List<String> args) {
  var obj = new B();
  obj.F();
}

class A {
  int a = 5;
  F() {
    print("123");
  }
}

class B extends A {
  @override
  F() {
      print("456");
    }
}

carried out

456
Published 59 original articles · won praise 45 · views 20000 +

Guess you like

Origin blog.csdn.net/m0_37771142/article/details/105036412