# 7.4 disengagement class file as a single file to create a new, separate storage class, for example: Person class file detached to person.dart
class Person {
final String name;
final num age;
final String gender;
const Person(this.name, this.age, this.gender);
}
The introduction of the Person class
import './person.dart';
void main() {
var p = new Person("xiaoqiang", 18, "男");
print(p.gender);
}
Value of # 7.5. Initialization list initialization list is a series of initialization operation performed before the body construction method, commonly used in the final set of variables
void main() {
// 注意:new关键字可以省略
// var p = new Person("xiaoqiang", 12, "男");
// print(p.name);
// 创建对象的时候,可以使用fn构造方法
var p2 = new Person.fn("xiaohong", 12);
}
class Person {
String name;
num age;
final String gender;
Person(this.name, this.age, this.gender){
print(this.name);
print(this.age);
print(this.gender);
}
// 在构造方法后面加冒号来设置初始化列表,每个初始化变量之间用逗号隔开
Person.fn(name, age):gender="男", name="xiaoli"{
print(name);
print(age);
}
}
# 7.6. Static members can use the static keyword to achieve class-level variables and functions, popular to say that static members can be called directly by the class name
void main() {
print(Person.name);
Person.showName();
}
class Person {
static String name;
static num age;
static void showName () {
name = "xiaoqiang";
print(name);
}
void showAge () {
// 非静态成员是可以直接使用静态成员
age = 10;
print(age);
}
}
Note: static member can not access non-static member
void main() {
print(Person.name);
Person.showName();
var p = new Person();
p.showAge();
}
class Person {
static String name;
static num age;
String gender = '男';
static void showName() {
name = "xiaoqiang";
print(name);
// 报错 静态方法不能访问非静态的属性
print(gender);
}
void showAge() {
// 非静态成员是可以直接使用静态成员
age = 10;
print(age);
}
}
Note 2: definition of static constant need to use static const, for example: static const gender = "M", the defined constants can be accessed in a static method
# 7.7 Object member access operator condition?.
void main() {
var p = new Person();
// 如果p为空就不执行showName方法 如果p不为空就会执行
p?.showName();
}
class Person {
String name;
static num age;
showName() {
print(name);
}
}
is 和 is!
void main() {
var p = new Person();
if (p is Person) {
p.showName();
} else {
print("不属于这个类");
}
}
class Person {
String name;
static num age;
showName() {
print(111);
print(name);
}
}
Cascade operation ..
void main() {
new Person()
..name = "小强"
..age = 20
..showName();
}
class Person {
String name;
num age;
showName() {
print(111);
print(name);
}
}
The method defined internal call # 7.8.call method as classes, then the object can be called as a method
void main() {
var p = new Person();
p();
p.showName();
}
class Person {
String name;
num age;
showName() {
print(111);
print(name);
}
call() {
print('call 方法被执行了');
}
}
Screw classroom video lessons address: http: //edu.nodeing.com