catalogue
2. Class inheritance and overloading
3. Abstract classes and enumerations
1. Basic syntax of class
Dart uses the Class keyword to define a Class.
Like other languages, the calling form of class methods is "object. Method".
The example code is as follows: define a People class and assign a value to the object.
class People{ String name; int age; } void main() { var student = People(); student.name = "loser"; student.age = 18; print('Student name:'+student.name+'\t Age:\t'+student.age.toString()); }
The console print log is as follows:
Performing hot restart...
Syncing files to device iPhone 13 Pro Max...
Restarted application in 347ms.
Student Name: waste wood Age: eighteen
Another method used in Dart is to call class methods in the form of "object?. method name", so as to avoid exceptions in the program when the object is null.
class People{ String name; int age; } void main() { var student = People(); student.name = "loser"; student.age = 18; // student = null; // print(student.name) / / the program will report an exception. At this time, we can use student Name avoids this problem. print('Student name:'+student.name+'\t Age:\t'+student.age.toString()); }
2. Class inheritance and overloading
In dart, the extends keyword is used to represent the inheritance relationship. Dart supports multiple inheritance, and the with keyword can be used to realize multiple inheritance.
The code is as follows:
class People{ String name; int age; void basicInformation(){ print('Human information'); } } //The Student class uses the extends keyword to indicate that the Student class inherits from the People class class Student extends People{ String studentId; String studentIdentity; @override void basicInformation(){ print('Student information'); } } class Worker{ String workerId; String workerName; void workerInfo(){ print('Employee information'); } } // The myIdentify class uses with to inherit the Student and Worker classes, which can call the basicInformation method of the Student class, // You can also call the workerInfo method of the worker class class myIdentify extends Student with Worker{ void infor(){ print('My message'); } }
3. Abstract classes and enumerations
Dart uses the abstract keyword to declare a class as an abstract class, and define abstract methods, variables, etc. in the abstract class.
abstract class AbstractClass{ void abstractMethod();//Define abstract methods } class MyImplement extends AbstractClass{ @override void abstractMethod() { // TODO: implement abstractMethod print('I am an abstract method implementation class'); } } void main() { MyImplement().abstractMethod(); }
The console log is as follows:
Performing hot restart...
Syncing files to device iPhone 13 Pro Max...
Restarted application in 325ms.
Fluent: I'm an abstract method implementation class
4. Implementation interface
Dart supports multiple interface implementations and can use the implements keyword.
The example code is as follows:
class Student { Student(); String study(){ return 'I am a student,today i learn something'; } } class ImlClass implements Teacher,Student{ @override String teach() { // TODO: implement teach throw UnimplementedError(); } @override String study() { // TODO: implement study throw UnimplementedError(); } @override // TODO: implement _name get _name => throw UnimplementedError(); }
5. Methods in dart
1. Construction method
Constructors are usually used to initialize this class by passing some parameter classes. Constructors need to have the same name as the class.
The example code is as follows: we define a triangle class and initialize the triangle class by passing the width and height.
class TriangleSize { double width; double height; TriangleSize(double width,double height){ this.width = width; this.height = height; } } void main() { var triangle = TriangleSize(2,3); print(triangle.width); }
2. Static method
Static methods can be called directly by using class name + static method name.
The example code is as follows:
class StaticClass{ static void printMethod(){ print('I'm a static method class'); } } void main() { StaticClass.printMethod(); }
3. Optional parameter method
dart has optional parameter methods. The calling method is that we can pass only some parameters.
The example code is as follows:
class StaticClass{ static void printMethod({String name,String arguments2}){ print('I'm a static method class'); } } void main() { StaticClass.printMethod(name: '123'); StaticClass.printMethod(arguments2: 'arguments2'); StaticClass.printMethod(name: '123',arguments2: '2'); }
4.getter and setter methods
Setter and getter methods are automatically generated in Dart.
The assignment operation is equivalent to the setter method.
Value operation is equivalent to getter method.
5. Anonymous function
When we use fluent to create a project, the system will automatically generate a timer Demo. There is an anonymous function, and the code is as follows:
void _incrementCounter(){ setState({ _counter++; }); }
6. Generics
T is used in Dart to represent generics:
void setData<T>(String key, T value) { print("key=${key}" + " value=${value}"); } T getData<T>(T value) { return value; } void main(List<String> args) { setData("name", "hello dart!"); // string type setData("name", 123); // int type print(getData("name")); // string type print(getData(123)); // int type // print(getData<bool>("hello")); // Error constrains that the type is bool, but String is passed in, so the compiler will report an error }