1.标准构造函数,可选参数,默认参数,初始化列表,命名构造函数,工厂构造函数,命名工厂构造函数,get,set,静态方法,抽象方法,抽象类
//所有的类都继承自Object
class Person {
String name;
int age;
Person(this.name, this.age); //标准构造方法
@override
String toString() {
// 重写了父级的toString的方法
return "name:$name,age:age";
}
}
class Student extends Person {
String _school; //_表示是私有变量
String city; //城市
String country; //国家
String name;
//如果继承的父类没有默认构造方法(无参数的),就要调用父类的构造方法
//:后面的表示式就是初始化列表
//this._school初始化自由参数
//name,age交由父级初始化
//{}里面的就是可选参数,或者用[]
//=就是默认参数
//初始化列表多个表示式的时候,用,隔开
//构造方法有方法体的,用{}
//这个事标准构造方法
Student(this._school, name, int age, {this.city, this.country = '中国'})
: name = "$country-$city",
super(name, age) {
print("构造方法体不是必须的");
}
//命名构造方法 [类名+.+方法名]
//可以多个,但是标准构造方法只能一个
//也可以有方法体
Student.per(Student stu) : super(stu.name, stu.age);
//命名工厂构造方法: factory [类名+.+方法名]
//这种形式不需要把final的变量作为参数,比较灵活
//如果事上面的模式,有final的