Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money.
📝 [Flutter] learning to form a record, [programmer essential knowledge]
📔 — Class methods and object operators in Flutter Dart
1. Write at the front
After introducing factory constructs & singleton objects & initializer lists in Dart in the previous article, let’s move on to learn about Dart’s class methods and object operators.
The Apple Mac is configured with the Flutter development environment
Android Studio installs a third-party simulator for Flutter — netease MuMu
Failed to find Build Tools Revision 29.0.2
Android license status unknown. Run ‘Flutter doctor — Android – Licenses’
How to create a Flutter project and run your first Flutter project
Dart uses var, final, and const
Dart Indicates the num of the data type
Dart String of data type
Dart data type list&Map
Dart method and arrow function
Dart’s method passes optional parameters and methods as parameters
Dart, Dart, Dart, Dart
Dart classes and objects in Flutter
Dart constructor of Flutter
Dart factory constructor & singleton & Initializer list
2. Class methods
When we learn about OC, there are instance methods and class methods. OC class methods start with a plus sign “+” and can only be called by the class name, but not by the object.
class StaticClass {
// Static attributes
static String name = "reno";
int height = 180;
int count = 0;
// Static method
static setName(String name1){
name = name1;
return name;
}
// Instance method
int sum(int a) {
return a + count + height;// Instance methods can be accessed with both non-static and static members
}
Copy the code
Instance methods can access both non-static and static members. Object methods are static methods in DART. Static methods cannot access static member properties, which are instance members, otherwise an error will be reported.
Dart Analysis is available for specific error information as follows:
❎ : error: Instance members can’t be email exchange from a static method
Error: Cannot access instance member from static method.
So why can’t static methods access instance members?
Come to think of it, static methods are called by class names, and you don’t even have instances of your classes, do you? So you’re not allowed to visit; But for instance method calls, the instance must exist, so access to the instance object must be possible.
Class methods are called as follows:
Invoke class methods and static properties directly from the class name.
- Definition of constant
static const int id = 30061111111888888;
Copy the code
A constant is in the constant area. A constant has only one copy and must be static.
- Object operator
var s; s = StaticClass(); print(StaticClass.name); s = null; print(s? .sum(10));
Copy the code
Student: Used here? To prevent errors, because s is dynamic, it might be empty, add? That means it can be empty.
- Object conforms to AS
In this case, s is the object type, but the exact type is not known, so the call to sum fails, and the type conversion is required.
Type conversions can use as, which means the same as Swift, as follows:
Type conversions are made wherever they are needed.
- Object complies with IS
There is also a form of if judgment, which is to determine whether it is a certain type
void main(a) {
var s = Object();
s = StaticClass();
// print((s as StaticClass).sum(10));
if (s is StaticClass) {
print(s.sum(10)); }}Copy the code
There is also an object operator dot (..) This is quite unexpected.
Instead of printing the result of a method call, it returns the class itself, which is essentially chained programming.
Chain programming can go on and on and on, but DART is two points, and the principle is the same, return class, keep calling, keep returning, keep calling.
5. Write in the back
Follow me, more content continues to output
- CSDN
- The Denver nuggets
- Jane’s book
🌹 if you like, give it a thumbs up 👍🌹
🌹 feel harvest, can come to a wave of collection + attention, so as not to find you next time I 😁🌹
🌹 welcome everyone to leave a message exchange, criticism and correction, forwarding please indicate the source, thank you for your support! 🌹