Define the class
class Person{ String name; int age; Person(String name,int age){ this.name = name; this.age = age; }}Copy the code
shorthand
When the constructor total initializes a member variable, it can be simplified using the following notation
class Person{
String name;
int age;
Person(this.name,this.age);
}
Copy the code
If you need to deal with other variables, you can also operate on them separately
class Preson{ String name; int age; person(this.name,this.age,String address){ print(address); }}Copy the code
Note: Constructors cannot be overloaded.
Get with the set
class Person{
String userName;
Person(this.userName);
String get name{
return "user:" + this.userName;
}
set name(String name){
//do something
this.userName = name;
}
}
void main(){
var p = new Person("zsww");
print(p.name);
//user:zsww
p.name = 'bxg';
print(p.name);
//uesr:bxg
}
Copy the code