class
Basic characteristics of face image object
1. Abstraction
In the object-oriented programming method, all kinds of things are called "objects", and the process of summarizing the common characteristics of the same thing is called abstraction, which includes data abstraction and process abstraction.
Data abstraction: for example, people have common characteristics such as nationality, skin color, name, gender and age.
Process abstraction: for example, people will eat, walk, speak, think and other common behaviors.
2. Packaging
Encapsulation is the organic combination of data (attribute) and source code of operation data (behavior) through some syntax form after abstracting a certain kind of things to form a "class".
Encapsulation can hide some properties and methods of an object.
class Person{ String name;//attribute String nation; void say(){};//Behavior, also known as method void eat(){}; }
3. Succession
Inheritance refers to the relationship and difference between two or more classes. The latter continues some aspects of the former. A new class of an object can be derived from the existing original class. This process is called inheritance. The new class is called a subclass or derived class, and the original class is called the parent or base class of the new class.
For example, the Chinese are a subclass of people, and the British are also a subclass of people. Both Chinese and British have nationality, skin color, name, gender, age and other attributes. They also eat, speak, walk and think, but their eating methods and speech languages are different.
class Chinese extends Person{ } class British extends Person{ }
4. Polymorphism
Polymorphism means that when different objects receive the same message at the same time, their actions are different and have a variety of forms.
For example, both Chinese and British people have ways to eat, but Chinese people eat with chopsticks and British people eat with knives and forks.
Polymorphism is usually implemented by overriding or overloading. Override is a method that redefines a close-up of a parent class in a subclass. Overloading means that multiple methods with the same name are allowed in a class, and the parameter tables of these methods are different (different number of parameters, different parameter types, or both).
class Chinese extends Person{ @override //Overwrite void eat(){ print('chopsticks'); } } class British extends Person{ @override //Overwrite void eat(){ print('knife and fork'); } }
Class definition and use
1. Definition of class
class Class name { Definition of attributes; ... Definition of method; ... }
When defining a class, if no initial value is provided for the member variable, the initial value of the member variable is null by default when creating an object; If an in class initial value of a member variable is specified, the in class initial value will be used to initialize the member variable when the object is created.
Define a Person class, including four attributes: name, gender, age, nationality and one speaking method:
class Person{ String name; //full name String sex; //Gender int age; //Age String nation; //nationality void say(){ print('Can say $(this.nation)language'); } }
Construction method
The constructor of a class is a special member method of a class, which is executed every time a new object of the class is created.
The constructor will not return any type, nor will it return void;
Construction methods can be used to set initial values for some member variables.
The construction methods in Dart language include default construction method, class name construction method and named construction method.
1. Default construction method
If no construction method is explicitly defined in the class, the system will define an empty construction method by default. The construction method has parameters, and its name is the same as the class name by default.
class Person{ Person(){};//Display definition, default construction method String name; int age; void say(){ print('say'); }
2. Class name construction method
Class name construction method means that the method name defined in the class is exactly the same as the class name. The definition format of class name construction method is as follows:
class Class name { Definition of attributes; ... Class name(Parameter type){ //Construction method body } Definition of method; ... }
Define a Person class, including four attributes: name, gender, age and nationality, one construction method and one speaking method:
Person( name, sex, age, nation){//Class name construction method this.name = name; this.sex = sex; this.age = age; this.nation = nation; }
3. Naming and construction method
A named constructor is a method whose method name defined in a class is not exactly the same as the class name. The definition format of the named construction method is as follows:
class Class name { Definition of attributes; ... Class name.identification(parameter list){ //Construction method body } Definition of method; ... }
Define and instantiate the Person class with the named constructor:
Person.create( name, sex, age, nation){//Named construction method this.name = name; this.sex = sex; this.age = age; this.nation = nation; } /*Instantiate class object*/ Person japanese = Person.create('Matsuda Okamoto','male',21,'Japan');
Memory and accessors
In Dart language, you can use the set keyword to define the memory (setter) and the get keyword to define the accessor (getter).
Memory is defined as follows:
set Attribute name(parameter){ //Function code }
Define accessors as follows:
return type get Attribute name{ return expression;//Return property value }
Define a class for calculating the area of a rectangle to realize that the accessor fetches the area value and the memory sets the height and width of the rectangle:
class Rect{ double width; double height; Rect(this.width,this.height); set newHeight(value){ this.height = value; } set newWidth(value){ this.width = value; } double get area { return this.width*this.height; } } /*call*/ Rect rect = Rect(1.2,4.5); print(rect.area); rect.newHeight = 12.0; print(rect.area);
Class inheritance
Definition of inheritance
Use the extends keyword to inherit a class. The subclass will inherit the properties and methods visible to the parent class and will not inherit the construction method. The definition form is as follows:
class Subclass name extends Parent class name { Subclass properties Subclass method }
Define a Shape class, including a name attribute and a show() method; Define a subclass Circle that inherits the subclass Shape class, including an r attribute and an area() method.
class Shape{ String name; //Name of the shape void show(){ print(thsi.name);//Output shape name } } class Circle extends Shape{ double r; //Radius of circle double area(){ //Calculate the area of a circle return 3.14*r*r; } }
Dart does not support multiple inheritance. Each class can inherit from at most one parent class and a class can inherit from another child class.
Override of parent method
The "@ override" code enables subclasses to override the methods, accessors (getters) and memory (getters) of the parent class.
Override the show() method of the Shape class in the Circle subclass:
class Circle extends Shape{ double r; //Radius of circle double area(){ //Calculate the area of a circle return 3.14*r*r; } @override void show(){ print(this.name + 'Radius:' + this.r.toString()); } }
When overridden, the number and type of the method must match, or the method of calling the parent class in the Circle subclass overwrite show() method.
super.show();
Polymorphism in inheritance
Polymorphism in Dart language means that the parent class defines a method that is not implemented at all or partially implemented, and lets the subclasses that inherit it implement it, so that each subclass can have different behavior.
class Rect{ double width; double height; double area() { return width*height; } @override void show(){ print(this.name + 'Width: $width,Height: $height,the measure of area:' + this.area().toString() ); } }
Call of construction method
1. If there is a parameterless constructor in the parent class, the constructor of the child class will call the parameterless constructor of the parent class by default
Define a Father class, which contains 1 parameterless construction method; Define a Child class that contains 1 parameterless constructor:
void main(){ Child child = Child(); } class Father{ Father(){ print('Father......'); } } class Child extends Father{ Child(){ print('Child......'); } }
2. If the parent class does not have a parameterless constructor, you need to explicitly call the user-defined constructor of the parent class with ":" after constructing the method parameter.
Define a Father class, which contains 1 construction method with parameters; Define a Child class that contains a constructor with parameters.
void main(){ Child child = Child('kate'); } class Father{ String firstName; Father(this.firstName){ print('Father\'s firstName: ......$firstName'); } } class Child extends Father{ Child(String firstName): super(firstName){ print('Child\'s firstName: ......$firstName'); } }