1 Dart中类 中无论函数还是方法都不支持重载,方法名一样,参数不同会报错。
class A{
A(){
}
A(int a){
//这个地方会报错,说A()函数已经存在
}
A.eat(){
//构造函数
}
A.sing(int food){
//构造函数
}
A.sleep(){
//构造函数
}
}
2 Getter 和 Stter 方法
class D{
int num1,num2;
// Getter方法
// int表示返回值类型,可以省略不写
// get 声明是Get方法
int get sum => num1+num2 ;
// Setter方法
// setter方法不需要声明返回值类型
// set 声明是Setter方法
set reduce(int num3) => num3-num2;
}
3 final 和 const 的比较
class D{
final boa;
// const声明的变量必须初始化,不初始化会报错;
// final声明的变量可以不初始化, 但必须在构造函数体执行之前初始化
// Const 变量是类级别的,需要标记为 static const,不声明static会报错
static const String bob = "bob";
void initBofamily(){
// 这个地方的Const 变量是方法里面,就不用必须声明static
const boc = "boc";
final bod = "bod";
}
D(int boaVaule):boa = boaVaule{
//此处为D的构造函数,boa初始化为boaVaule
//是在构造函数执行之前进行
}
}
还有一个区别是:实例变量可以是 final 类型但不能是 const 类型
首先来看一下什么是实例变量
class Student{
String name = "lucy";//实例变量
static int age = 26; //静态变量,它是公有的
}
main(){
Student studentA = new Student();
studentA.name = "bob";
Student.age = 32;
print("studentA name: ${studentA.name} , age: ${Student.age}");
Student studentB = new Student();
print("studentB name: ${studentB.name} , age: ${Student.age}");
}
打印结果:
观察可以发现 studentA 修改了name ,age 两个地方 打印结果两个地方都被改变
但是,studentB 没有修改任何数据,但是打印出来 name是默认初始化的lucy没有改变,age却不是原来初始化的值,
而是studentA 修改后的数值。
是因为name是实例变量,只要 new 一个Student对象出来 name就是初始化的默认值,studentA 对象修改了name,但是对studentB并不起作用,除非studentB自己进行修改。
age变量被studentA 修改以后,studentB也被修改了。
扫描二维码关注公众号,回复:
12396639 查看本文章
再来看这句话:实例变量可以是 final 类型但不能是 const 类型
就是说上例中name可以用final修饰,只是不可再被修改,但不能用const修饰,
因为类变量要被const修饰要加static,而加了static就变成常量变量变成和age一样,不再是实例变量。
4 Dart 可选参数 { ...... } 形式 和 [ ..... ] 形式比较
/**
{ ... }形式
*/
class ContainerView{
ContainerView({int key,String text,int color}){
}
ContainerView.requiredView({@required int key,String text,int color}){
}
}
main(){
//调用时可选所以可以选择所传个数,但必须是 (参数名:参数值) 形式,例如:key: 234
//例1:可以不传任何参数
new ContainerView();
//例2:可以只传部分参数
new ContainerView(key:1,text:"contentMsg");
//例3:参数位置可以不按顺序,位置可换因为调用时会用key:vaule的形式,所以位置不必对应
new ContainerView(text:"contentMsg",key: 234);
//例4:如果只传值 不使用 参数名:参数值(例如:key: 234) 的形式
//会报错
new ContainerView("contentMsg",234);
//例5:如果可选参数里面某个参数要必须传,可以用@required修饰
//此时调用必须传所修饰参数,否则报错
//而其他参数可选,
new ContainerView.requiredView(key: 1);//key:1 必须传
}
/**
[ .... ] 形式
*/
class ContainerView{
void sayMethod([String text,int time,bool isMan]){
}
void sayMethod2(String text,[int time,bool isMan]){
}
}
main(){
ContainerView containerView = new ContainerView();
//例1:可以不传任何参数
containerView.sayMethod();
//例2:可以只传部分参数,但是必须按参数顺序传值
//sayMethod 中顺序为 String int bool
//若 containerView.sayMethod("textVaule",false,2);就会报错
//还有像sayMethod(参数1,参数2,参数3)有三个参数
//调用时可sayMethod(参数1,参数2)但sayMethod(参数2,参数3)忽略参数1会报错
containerView.sayMethod("textVaule",2,false);
//例3:参数放在[]外的参数为必须传的参数,否则报错
containerView.sayMethod2("textRequired");
}//main()
总结:
{ ....... } 形式:
参数可选择是否传值(被@required 修饰参数除外);
但是必须用 key:vaule 的形式;
位置可以不按照参数顺序
[ ....... ] 形式:
参数可选择是否需要传值,但是必须按参数顺序,不可跳过前面参数,可不传后面参数
不必使用 key:vaule 的形式;
位置必须按照参数顺序,若果有多个参数
5 集合遍历 map()和forEach()如何选择
//map() 是带有返回值的,返回一个Iterable<T>类型
/**
* Returns a new lazy [Iterable] with elements that are created by
* calling `f` on each element of this `Iterable` in iteration order.
*
* This method returns a view of the mapped elements. As long as the
* returned [Iterable] is not iterated over, the supplied function [f] will
* not be invoked. The transformed elements will not be cached. Iterating
* multiple times over the returned [Iterable] will invoke the supplied
* function [f] multiple times on the same element.
*
* Methods on the returned iterable are allowed to omit calling `f`
* on any element where the result isn't needed.
* For example, [elementAt] may call `f` only once.
*/
Iterable<T> map<T>(T f(E e)) => MappedIterable<E, T>(this, f);
//forEach()是不带返回值的
/**
* Applies the function [f] to each element of this collection in iteration
* order.
*/
void forEach(void f(E element)) {
for (E element in this) f(element);
}