Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities
Class method
Dart class methods are static methods that are accessed using the class instead of creating instance variables.
class StaticClass {
/ / static
static int count = 1;
static int sum(int a){
returna + count; }}Copy the code
Static methods are equivalent to class methods:
Non-static members cannot be accessed in static methods:
class StaticClass {
/ / static
static int count = 1;
int currentCount = 0;
static int sum(int a){
return a + count + this.currentCount; }}Copy the code
However, non-static members can be accessed as well as static members within instance methods
class StaticClass {
/ / static
static int count = 1;
int currentCount = 0;
static int sum(int a){
return a + count ;
}
int sum1(int a) {
returncurrentCount + a + count; }}Copy the code
Because static methods are called from class objects, the instance object may not have been constructed before the class object is called, so the member variable may not exist. So non-static members cannot be accessed in class methods. A static variable has only one copy in the entire memory, and if it is modified in one place, it is modified globally
void main() {
staticDemo(); /// 22
print(StaticClass.count); /// 2
print(StaticClass.sum(20)); /// 22
}
void staticDemo() {
StaticClass.count = 2;
print(StaticClass.sum(20));
}
Copy the code
Object operator
We know that when an object is set to NULL and an attribute is accessed again, an error is reported. What about avoidance in Dart?
void staticDemo() {
var s1;
s1 = StaticClass();
print(s1.currentCount);
s1 = null;
print(s1.currentCount); ///An access error is reported
}
Copy the code
Can we use nullable types?
void staticDemo() {
var s1;
s1 = StaticClass();
print(s1.currentCount);
s1 = null;
print(s1? .currentCount); }Copy the code
Cast casting
If we write it this way we’re going to get an error because currentCount is not in Object
void staticDemo() {
var s1 = Object(a); s1 = StaticClass();/// peptides
print(s1.currentCount);
s1 = null;
print(s1? .currentCount); }Copy the code
The cast is called where it is called, and there is no problem after the cast.
void staticDemo() {
var s1 = Object(a); s1 = StaticClass();/// peptides
print((s1 as StaticClass).currentCount);
}
Copy the code
Assigning s1 to null again also generates an error because the first assignment was Object, and Object cannot be null.
void staticDemo() {
var s1 = Object(a); s1 = StaticClass();/// peptides
print((s1 as StaticClass).currentCount);
s1 = null;
}
Copy the code
S1 is a StaticClass
void staticDemo() {
var s1 = Object(a); s1 = StaticClass();/// peptides
print((s1 as StaticClass).currentCount);
if (s1 is StaticClass) {
print(s1.currentCount); }}Copy the code
The chain of grammar
The chained syntax can be used in Dart.. The object itself is returned
void staticDemo() {
var s1 = Object(a); s1 = StaticClass();/// peptides
print((s1 as StaticClass).currentCount);
if (s1 is StaticClass) {
print(s1.currentCount);
varresult = s1.. currentCount =15..sum1(20); /// The chain of grammar
print(result); }}Copy the code