通過代碼比較一下
class Person {
bool say(String msg) {
print(msg);
return msg == null ? false : true;
}
void setPeople() {
/**
* People.setSay1 參數需要是一個bool值
* say("im people")帶括號就是返回一個bool值
* 所以問題
* */
new People.setSay1(isSay: say("im people"));
/**
* People.setSay2 參數是需要 bool Function(String)
* say("im people")帶括號就是返回一個bool值
* 所以會報錯
* The argument type 'bool' can't be assigned to
* the parameter type 'bool Function(String)'.dartargument_type_not_assignable
* */
new People.setSay2(methodSay: say("im people"));
/**
* 直接用 say方法,不帶括號不傳參數,就代表的了say方法本身
* say方法 結構就是bool Function(String)
* 所以沒問題
*/
new People.setSay2(methodSay: say);
}
}
typedef MethodSay = bool Function(String msg);
class People {
String peopleSayMsg;
bool isSay;
People.setSay1({bool isSay}) {
this.isSay = isSay;
}
People.setSay2({MethodSay methodSay}) {
methodSay(peopleSayMsg);
}
}
總結:
方法名( 參數 ) 這樣使用就相當於調用方法後的得到的值,這個方法返回類型是什麼 就是一個什麼類型的值
方法名 直接用,不加括號 就是這個方法本身結構傳過去 ,而不是其運算後的返回值