Dart獨(dú)特的使用方式(session one)

1.位置可選參數(shù)

Dart中的函數(shù)可以包含可選參數(shù)。

可選參數(shù)是函數(shù)調(diào)用者可以選擇不提供的參數(shù)??梢栽诤瘮?shù)內(nèi)檢查可選參數(shù)是否提供了值??蛇x參數(shù)也可以具有默認(rèn)值;因此,如果沒有提供可選參數(shù)的值,可選參數(shù)仍然可以在函數(shù)中使用。這中不提供參數(shù)值在函數(shù)中仍然可用的通常是一種更合乎邏輯的方式。

有兩種類型的可選參數(shù):位置和命名。
當(dāng)您希望使功能更加靈活,同時為基本用例維護(hù)簡單版本時,可選參數(shù)非常有用??蛇x參數(shù)不是編寫非常大的函數(shù)的借口。雖然具有許多可選參數(shù)的函數(shù)可以非常靈活,但它也可能是令人生畏的,它提供了所有選項。如果您發(fā)現(xiàn)自己編寫了一個包含許多參數(shù)的函數(shù),您可能需要考慮它是否更適合作為兩個(或更多)函數(shù)。

英文原文:

Functions in Dart can have optional parameters. Optional parameters are parameters that the function caller can choose not to supply. It can be checked from within a function whether an optional parameter was supplied with a value or not. Optional parameters can also have default values; therefore, if values for them are not supplied, they will still be usable within the function. This is often a more logical way to go vs. having the necessity to check if an optional parameter was supplied with a value. There are two types of optional parameters: positional and named.
Optional parameters are useful in instances when you want to make a function more flexible while at the same time maintaining a simple version for basic-use cases. Optional parameters are not an excuse to write very large functions. While a function with many optional parameters can be very flexible, it can also be intimidating, with all of the options that it provides. If you find yourself writing a function with many parameters, you may want to consider whether it would be better served as two (or more) functions.

    1. 位置可選參數(shù)在函數(shù)定義中使用方括號定義。 例如,以下函數(shù)repeat()有一個名為"repetitions"的可選參數(shù)。
void repeat(String word, [int repetitions]) { // repetitions is optional if (repetitions != null) { // check if repetitions was supplied
for (int i = 0; i < repetitions; i++) { print(word);
}
} else { // repetitions was not supplied, so just print once
    print(word);
  }
}

    1. 這里使用 " != "運(yùn)算符來檢查是否提供了可選參數(shù) "repetitions"。 如果沒有為可選參數(shù)提供值,并且它沒有默認(rèn)值,則它將具有值 "null"。 在 repeat() 的情況下,如果提供可選參數(shù) "repetitions",它將打印單詞重復(fù)次數(shù)。 如果沒有提供可選參數(shù)** "repetitions"**,它將只打印一次單詞。 我們也可以通過使用提供可選參數(shù) "repetitions"的默認(rèn)值可以簡化此功能。例如:
void repeat(String word, [int repetitions = 1]) { for (int i = 0; i < repetitions; i++) {
    print(word);
  }
 }

ps:這個repeat() 函數(shù)寫法完全等同于上一個repeat() 函數(shù)的寫法。

  • 逗號分隔多個可選參數(shù)的位置。 當(dāng)調(diào)用具有可選參數(shù)的函數(shù)時,必須在調(diào)用時填寫的參數(shù)位置與聲明可選參數(shù)的位置順序保持一致(PS:需按序填寫,不可省略)。
void repeat(String word, [int repetitions = 1, String exclamation = ""]) { for (int i = 0; i < repetitions; i++) {
    print(word + exclamation);  // the + operator can concatenate strings
  }
}
void main() {
  repeat("Dog");  // legal
  repeat("Dog", 2, "!");  // legal
  repeat("Dog", 2);  // legal
  repeat("Dog", "!");  // ILLEGAL
  repeat("Dog", "!", 2);  // ILLEGAL
}

此處的 “+”號作用為:拼接前后兩個字符串,形成一個新的字符串。

print(word + exclamation); // the + operator can concatenate strings

PS:最后兩種寫法非法。

2.命名可選參數(shù)

命名的可選參數(shù)與位置可選參數(shù)非常相似,但它們的定義和調(diào)用方式不同。 當(dāng)調(diào)用帶有命名可選參數(shù)的函數(shù)時,它們所提供的順序無關(guān)緊要。 它們用大括號{}和冒號:定義,以將它們的名稱與其默認(rèn)值分開。 與位置可選參數(shù)一樣,逗號將它們分開。 與位置可選參數(shù)一樣,它們也不需要具有默認(rèn)值。 如果命名的可選參數(shù)未提供值,則它們的默認(rèn)值也為null


void repeat(String word, {int repetitions: 1, String exclamation: ""}) { 
 for (int i = 0; i < repetitions; i++) {
    print(word + exclamation);  // the + operator can concatenate strings
  }
}

void main() {
  repeat("Dog"); // legal
  repeat("Dog", repetitions: 2, exclamation: "!"); // legal
  repeat("Dog", repetitions: 2); // legal
  repeat("Dog", exclamation: "!"); // legal, even without repetitions repeat("Dog", exclamation: "!",   repetitions: 2); // legal, even out of order
}

  • 盡管這種寫法相較于位置可選參數(shù)更加冗長,不過因此可以為函數(shù)的可選參數(shù)提供更自由的使用方式,增加函數(shù)的可讀性。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容