This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021.

  • Dart’s class methods and object operators

1. Class methods

class staticClass{
  // Static attributes
  static int count = 0;
  // Static method
  static int sum(int a){
    returncount + a; }}Copy the code

We define a class, and we define its static methods which are our class methods in iOS

void staticDemo(){
  staticClass.count = 2;
  var sum = staticClass.sum(10);
  print(sum);
}
Copy the code

Just call it

We can’t do it in static methodsAccess non-static variablesIt is easy to understand that we are equivalent toThe class method calls the property variable of the instance objectNot at the momentThe instanceAn error is reported when converting data.

class staticClass{
  // Static attributes
  static int count = 0;
  int num = 1;
  // Static method
  static int sum(int a){
    return count + a ;
  }
  // Instance method
  int sum1(int a){
    return count +a +num; }}Copy the code

When we call it, we modify the static member variable of the class, because static member variables are only one copy in the entire memory, and they are only modified in one place, and they are modified globally.

void main(List<String> arguments) {
  print('Hello world! ');

  staticDemo();// Count is set to 2
  print(staticClass.count);/ / 2
  print(staticClass.sum(20));/ / 22
}
void staticDemo(){
  staticClass.count = 2;
  var sum = staticClass.sum(10);/ / 12
  print(sum);
}
Copy the code

2. Object operators

void staticDemo(){

  var class1;
  class1 = staticClass();
  print(class1.num);
  class1 = null;
  print(class1.num);
}
Copy the code

We run demo and the result is an error

Because we putObject is emptyAnd visit again laterObject properties, will report an error, we avoid this problem. You can use?To judge

void staticDemo(){

  var class1;
  class1 = staticClass();
  print(class1.num);
  class1 = null;
  print(class1? .num);
}
Copy the code

Run it again and return null

  • Cast casting
void staticDemo(){

  var class1 = Object(a); class1 = staticClass();print(class1.num);
}
Copy the code

The staticClass inherits from Object, but does not have num attributes, so it cannot be identified. This is a very common situation in iOS, where we assign a base class and then force the type we want.

print((class1 as staticClass).num);
Copy the code

If we give it again at this pointclass1Empty, this time directly errorThe Objcet object cannot be empty.

We can use IS for interpretation

if(class1 is staticClass){
  print(class1.num);
}
Copy the code

3. Chain grammar

In DART you can use.. Returns the object itself, so we can call its properties and methods directly.

void staticDemo(){

  var class1 = Object(a); class1 = staticClass();print((class1 as staticClass).num);
  if(class1 is staticClass){
    print(class1.num);
    var count = 
      class1
      ..num = 11
      ..sum1(20);
    print(count);
  }
  // print(class1? .num);
Copy the code