首页 扑腾书籍 飞镖学徒

3
开奖结果3d& Operations 由Jonathan Sande撰写& Matt Galloway

生活充满了品种,品种表达了不同开奖结果3d的。你用什么开奖结果3d的牙膏?薄荷?肉桂?你的血型是什么?一个? B? o +?你喜欢什么开奖结果3d的冰淇淋?香草?草莓?辣椒粉乳冻漩涡?拥有所有这些不同的东西的名称可以帮助您智能地谈论它们。它还可以帮助您识别某些事情不合适时。毕竟,没有人用辣椒粉乳冻漩涡刷牙。虽然它确实很好。

编程开奖结果3d与现实生活开奖结果3d一样有用。它们帮助您对您在代码中使用的所有不同开奖结果3d的数据进行分类。

在第2章中,您学习了如何使用变量命名数据,并介绍DART数据开奖结果3d。在本章中,您将学习更多关于开奖结果3d以及与他们可以执行的内容。将特别关注字符串,用于表示文本。

DART中的数据开奖结果3d

在飞镖中,一个 开奖结果3d 是一种告诉编译器如何使用一些数据的方式。到这本书中,您已经遇到了以下开奖结果3d:

  • int
  • double
  • num
  • dynamic
  • String

The last one in that list, String, is the type used for text like 'Hello, Dart!'.

就像你没有用冰淇淋刷牙一样,Dart开奖结果3d让您尝试使用乘以乘以文本或从数字中删除空白的愚蠢的东西。

Dart has even more built-in types than just the ones listed above. The basic ones, such as int, double, and num will serve you adequately in a great variety of programming scenarios, but when working on projects with specific needs, it’ll be convenient to create custom types instead. A weather app, for example, may need a Weather type, while a social media app may need a User type. You’ll learn how to create your own types in Chapter 4 and Chapter 6.

As you learned in Chapter 2, the root of all types is the Object type. This type defines a few core operations, such as testing for equality and describing the object in text. Every other type in Dart is a subtype of Object, and as a subtype, shares Object’s basic functionality.

开奖结果3d推断

在上一章中,您还在Dart中的开奖结果3d推断下潜行峰值,但您现在需要一些时间稍微看到它。

显式注释变量

总是明确地添加的很好 开奖结果3d注释 声明变量时。这意味着在变量名之前写入数据开奖结果3d。

int myInteger = 10;
double myDouble = 3.14;

创建常量变量

Declaring variables the way you did above makes them mutable. If you want to make them immutable, but still keep the type annotation, you can add const or final in front.

const int myInteger = 10;
const double myDouble = 3.14;
final int myInteger = 10;
final double myDouble = 3.14;

让编译器推断出开奖结果3d

While it’s permissible to include the type annotation as in the example above, it’s redundant. You’re smart enough to know that 10 is an int and 3.14 is a double, and it turns out the Dart compiler can deduce this as well. The compiler doesn’t need you to explicitly tell it the type every time — it can figure the type out on its own through a process called 开奖结果3d推断。并非所有编程语言都有开奖结果3d推断,但Dart DO - 它是DART作为一种语言的关键组成部分。

const myInteger = 10;
const myDouble = 3.14;

检查VS代码中的推断开奖结果3d

有时,检查可变或常数的推断开奖结果3d可能是有用的。通过将鼠标指针悬停在变量名称上,您可以在VS代码中执行此操作。 VS代码将显示像这样的漏东:

在运行时检查开奖结果3d

Your code can’t hover a mouse pointer over a variable to check the type, but Dart does have a programmatic way of doing the same thing: the is keyword:

num myNumber = 3.14;
print(myNumber is double);
print(myNumber is int);
true
false
print(myNumber.runtimeType);

开奖结果3d转换

有时,您将有一种开奖结果3d的数据,但需要将其转换为另一个开奖结果3d。尝试的天真方式是如此:

var integer = 100;
var decimal = 12.5;
integer = decimal;
A value of type 'double' can't be assigned to a variable of type 'int'.
integer = decimal.toInt();

具有混合开奖结果3d的操作员

到目前为止,您只看到运营商独立在整数或双打上行动。但是,如果你有一个用双倍乘以一个整数的整数怎么办?

const hourlyRate = 19.5;
const hoursWorked = 10;
const totalCost = hourlyRate * hoursWorked;
const totalCost = (hourlyRate * hoursWorked).toInt();
Const variables must be initialized with a constant value.
final totalCost = (hourlyRate * hoursWorked).toInt();

确保某种开奖结果3d

有时您希望定义一个常量或变量,并确保它仍然是某种开奖结果3d,即使您分配给它的开奖结果3d是不同开奖结果3d的。您之前看到了如何从一种开奖结果3d转换为另一个开奖结果3d。例如,考虑以下内容:

const wantADouble = 3;
final actuallyDouble = 3.toDouble();
const double actuallyDouble = 3;
const wantADouble = 3.0;

铸造

At other times, you may have a variable of some general supertype, but you need functionality that is only available in a subtype. If you’re sure that the value of the variable actually is the subtype you need, then you can use the as keyword to change the type. This is known as 铸造铸造.

num someNumber = 3;
print(someNumber.isEven);
The getter 'isEven' isn't defined for the type 'num'.
final someInt = someNumber as int;
print(someInt.isEven);
num someNumber = 3;
final someDouble = someNumber as double;
_CastError (type 'int' is not a subtype of type 'double' in type cast)
final someDouble = someNumber.toDouble();

迷你练习

  1. Create a constant called age1 and set it equal to 42. Create another constant called age2 and set it equal to 21. Check that the type for both constants has been inferred correctly as int by hovering your mouse pointer over the variable names in VS Code.
  2. Create a constant called averageAge and set it equal to the average of age1 and age2 using the operation (age1 + age2) / 2. Hover your mouse pointer over averageAge to check the type. Then check the result of averageAge. Why is it a double if the components are all int?

数字对于编程至关重要,但它们不是您在应用程序中使用的唯一开奖结果3d的数据开奖结果3d。文本也是一个常见的数据开奖结果3d,代表了人们的名称,地址,甚至是书籍的完整文本。所有这些都是应用程序可能必须处理的文本的示例。

计算机如何代表字符串

计算机将字符串视为个人集合 人物。数字是CPU的语言,并且所有代码在每个编程语言中都可以减少到原始数字。字符串没有什么不同。

unicode.

在孤立中,计算机可以自由选择任何字符集映射它喜欢的映射。如果电脑想要这封信 a to equal the number 10, then so be it. But when computers start talking to each other, they need to use a common character set.

使用箭的字符串

Dart, like any good programming language, can work directly with strings. It does so through the String data type. In this section, you’ll learn about this data type and how to work with it.

字符串和字符

您已经使用了第1章中的DART字符串,其中您打印了字符串的内容:

print('Hello, Dart!');
var greeting = 'Hello, Dart!';
print(greeting);
var greeting = 'Hello, Dart!';
greeting = 'Hello, Flutter!';

获得人物

笔记:下面的代码示例包含的表情符号的字符可能难以输入你的键盘上。你可以找到所有研究方便复制和粘贴通过打开 起动器/箱/起动器.DART 在第3章本书的补充材料。

const letter = 'a';
var salutation = 'Hello!';
print(salutation.codeUnits);
[72, 101, 108, 108, 111, 33]
const dart = '🎯';
print(dart.codeUnits);
// [55356, 57263] 
const dart = '🎯';
print(dart.runes);
// (127919) 

unicode. Grapheme集群

不幸的是,语言是凌乱的,unicode也是如此。看看这个例子:

const flag = '🇲🇳';
print(flag.runes);
// (127474, 127475)   
const family = '👨‍👩‍👧‍👦';
print(family.runes);
// (128104, 8205, 128105, 8205, 128103, 8205, 128102)           
const family = '👨‍👩‍👧‍👦';

family.length;           // 11
family.codeUnits.length; // 11
family.runes.length;     // 7            

添加字符包

这是一个尝试第一个酒吧包的良机机会。在项目的根文件夹中,打开 pubspec.yaml..

dependencies:
  characters: ^1.0.0
dart pub get
import 'package:characters/characters.dart';
const family = '👨‍👩‍👧‍👦';
family.characters.length; // 1          

单引号与双引号

Dart允许您使用单引号或双引号来表示字符串文字。这两种都很好:

'I like cats'
"I like cats"
"my cat's food"
'my cat\'s food'

级联

你可以做更多的不是建立简单的字符串。有时你需要操作一个String,而要做到想一个常用的方法是将其与另一个字符串相结合。这就是所谓的 级联...与上述史内灵无关。

var message = 'Hello' + ' my name is ';
const name = 'Ray';
message += name;
// 'Hello my name is Ray'
final message = StringBuffer();
message.write('Hello');
message.write(' my name is ');
message.write('Ray');
message.toString();
// "Hello my name is Ray"

插值

您还可以使用搭建字符串 插值,这是一个特殊的飞镖语法,允许您以容易阅读您的代码的方式轻松构建字符串:

const name = 'Ray';
const introduction = 'Hello my name is $name';
// 'Hello my name is Ray'
const oneThird = 1 / 3;
const sentence = 'One third is $oneThird.';
One third is 0.3333333333333333.
final sentence = 'One third is ${oneThird.toStringAsFixed(3)}.';
One third is 0.333.

多线字符串

Dart有一种简洁的方式来表达包含多行的字符串,当您需要在代码中使用非常长的字符串时,这可能相当有用。

const bigString = '''
You can have a string
that contains multiple
lines
by
doing this.''';
print(bigString);
You can have a string
that contains multiple
lines
by
doing this.
const oneLine = 'This is only '
    'a single '
    'line '
    'at runtime.';
const oneLine = 'This is only ' +
    'a single ' +
    'line ' +
    'at runtime.';
This is only a single line at runtime.
const twoLines = 'This is\ntwo lines.';
This is
two lines.
const rawString = r'My name \n is $name.';
My name \n is $name.

从其代码插入字符

Similar to the way you can insert a newline character into a string using the \n escape sequence, you can also add Unicode characters if you know their codes. Take the following example:

print('I \u2764 Dart\u0021');

print('I love \u{1F3AF}');

迷你练习

  1. Create a string constant called firstName and initialize it to your first name. Also create a string constant called lastName and initialize it to your last name.
  2. Create a string constant called fullName by adding the firstName and lastName constants together, separated by a space.
  3. Using interpolation, create a string constant called myDetails that uses the fullName constant to create a string introducing yourself. For example, Ray Wenderlich’s string would read: Hello, my name is Ray Wenderlich.

对象和动态开奖结果3d

Dart摆脱了解决Javascript中固有的一些问题的愿望。 JavaScript是A. 动态开奖结果3d 语。动态意味着某种东西可以改变,并且对于JavaScript表示开奖结果3d可以在运行时更改。

var myVariable = 42;
myVariable = "hello";
var answer = myVariable * 3; // runtime error
var myVariable = 42;
myVariable = 'hello'; // compile-time error
dynamic myVariable = 42;
myVariable = 'hello'; // OK
var myVariable; // defaults to dynamic
myVariable = 42;      // OK
myVariable = 'hello'; // OK
Object myVariable = 42;
myVariable = 'hello'; // OK

挑战

搬家之前,这里是测试你的开奖结果3d和操作的知识无意识的挑战。如果你尝试自行解决天马这是最好的,但如果您遇到问题的解决方案可供选择。

挑战1:教师的分级

您是老师,在您的课堂上,出勤率价值20%的成绩,作业价值30%,考试价值50%。您的学生为她的出席时间90分,她的家庭作业80分,考试有94分。计算她的成绩作为整数百分比圆润的百分比。

挑战2:相同,但不同

此字符串有两个标志,看起来相同。但他们不是!其中一个是乍得的旗帜,另一个是罗马尼亚的旗帜。

const twoCountries = '🇹🇩🇷🇴';      

挑战3:多少?

鉴于以下字符串:

const vote = 'Thumbs up! 👍🏿';    

挑战4:找到错误

以下代码有什么问题?

const name = 'Ray';
name += ' Wenderlich';

挑战5:什么开奖结果3d?

What’s the type of value?

const value = 10 / 2;

挑战6:总结

What is the value of the constant named summary?

const number = 10;
const multiplier = 5;
final summary = '$number \u00D7 $multiplier = ${number * multiplier}';

关键点

  • 开奖结果3d转换允许您将一个开奖结果3d的值转换为另一个开奖结果3d。
  • When doing operations with basic arithmetic operators (+, -, *, /) and mixed types, the result will be a double.
  • 开奖结果3d推断允许您省略DART可以弄清楚的开奖结果3d。
  • unicode.是标准表示为数字映射字符。
  • Dart使用称为代码单元的UTF-16值来编码Unicode字符串。
  • unicode.中的单个映射称为代码点,称为DART中的符文。
  • 用户感知的字符可以由一个或多个代码点组成,并且称为图形字符。
  • 您可以使用加法运算符组合字符串。
  • 您可以使用三个单引号或双引号进行多线字符串。
  • 您可以使用字符串插值来构建一个字符串。
  • Dart is an optionally-typed language. While it’s preferable to choose statically-typed variables, you may write Dart code in a dynamically-typed way by explicitly adding the dynamic 开奖结果3d注释 in front of variables.

有一个技术问题?想报告一个错误吗? 您可以向官方书籍论坛中的书籍作者提出问题和报告错误 这里.

有反馈分享在线阅读体验吗? 如果您有关于UI,UX,突出显示或我们在线阅读器的其他功能的反馈,您可以将其发送到设计团队,其中表格如下所示:

© 2021 Razeware LLC

您可以免费读取,本章的部分显示为 混淆了 文本。解锁这本书,以及我们整个书籍和视频目录,带有Raywenderlich.com的专业订阅。

现在解锁

要突出或记笔记,您需要在订阅中拥有这本书或自行购买。