美文网首页Flutter
Dart独特的使用方式(session one)

Dart独特的使用方式(session one)

作者: M_PI_4 | 来源:发表于2019-07-20 17:45 被阅读0次

1.位置可选参数

Dart中的函数可以包含可选参数。

可选参数是函数调用者可以选择不提供的参数。可以在函数内检查可选参数是否提供了值。可选参数也可以具有默认值;因此,如果没有提供可选参数的值,可选参数仍然可以在函数中使用。这中不提供参数值在函数中仍然可用的通常是一种更合乎逻辑的方式。

有两种类型的可选参数:位置和命名。
当您希望使功能更加灵活,同时为基本用例维护简单版本时,可选参数非常有用。可选参数不是编写非常大的函数的借口。虽然具有许多可选参数的函数可以非常灵活,但它也可能是令人生畏的,它提供了所有选项。如果您发现自己编写了一个包含许多参数的函数,您可能需要考虑它是否更适合作为两个(或更多)函数。

英文原文:

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. 位置可选参数在函数定义中使用方括号定义。 例如,以下函数repeat()有一个名为"repetitions"的可选参数。
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. 这里使用 " != "运算符来检查是否提供了可选参数 "repetitions"。 如果没有为可选参数提供值,并且它没有默认值,则它将具有值 "null"。 在 repeat() 的情况下,如果提供可选参数 "repetitions",它将打印单词重复次数。 如果没有提供可选参数** "repetitions"**,它将只打印一次单词。 我们也可以通过使用提供可选参数 "repetitions"的默认值可以简化此功能。例如:
void repeat(String word, [int repetitions = 1]) { for (int i = 0; i < repetitions; i++) {
    print(word);
  }
 }

ps:这个repeat() 函数写法完全等同于上一个repeat() 函数的写法。

  • 逗号分隔多个可选参数的位置。 当调用具有可选参数的函数时,必须在调用时填写的参数位置与声明可选参数的位置顺序保持一致(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.命名可选参数

命名的可选参数与位置可选参数非常相似,但它们的定义和调用方式不同。 当调用带有命名可选参数的函数时,它们所提供的顺序无关紧要。 它们用大括号{}和冒号:定义,以将它们的名称与其默认值分开。 与位置可选参数一样,逗号将它们分开。 与位置可选参数一样,它们也不需要具有默认值。 如果命名的可选参数未提供值,则它们的默认值也为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
}

  • 尽管这种写法相较于位置可选参数更加冗长,不过因此可以为函数的可选参数提供更自由的使用方式,增加函数的可读性。

相关文章

网友评论

    本文标题:Dart独特的使用方式(session one)

    本文链接:https://www.haomeiwen.com/subject/grrlkctx.html