-
Notifications
You must be signed in to change notification settings - Fork 0
/
Learn Kotlin in 12 Minutes - 2022.srt
2040 lines (1632 loc) · 52.9 KB
/
Learn Kotlin in 12 Minutes - 2022.srt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1
00:00:00,640 --> 00:00:02,399
在这个视频中我将教会你 Kotlin 编程语言的基础
in this video i'll teach you the core of
2
00:00:02,399 --> 00:00:04,240
在这个视频中我将教会你 Kotlin 编程语言的基础
the Kotlin programming language
3
00:00:04,240 --> 00:00:06,000
Kotlin 是一种这几年很流行的通用的静态类型语言
Kotlin is a general purpose statically
4
00:00:06,000 --> 00:00:07,759
Kotlin 是一种这几年很流行的通用的静态类型语言
typed language that has become extremely
5
00:00:07,759 --> 00:00:09,840
Kotlin 是一种这几年很流行的通用的静态类型语言
popular in the last few years
6
00:00:09,840 --> 00:00:11,679
Kotlin 既可以用在客户端上
Kotlin can be used on both the client
7
00:00:11,679 --> 00:00:13,519
也可以用在服务端上,这意味着用 Kotlin 写的代码
and the server which means the code can
8
00:00:13,519 --> 00:00:15,280
既可以运行在用户的设备(客户端)上
be run either on the user's device
9
00:00:15,280 --> 00:00:17,119
也可以运行在云服务器(服务端)上用以回应客户端的请求
the client or on some computer in the
10
00:00:17,119 --> 00:00:18,800
也可以运行在云服务器(服务端)上用以回应客户端的请求
cloud called the server
11
00:00:18,800 --> 00:00:20,640
也可以运行在云服务器(服务端)上用以回应客户端的请求
which responds to requests from the
12
00:00:20,640 --> 00:00:21,920
也可以运行在云服务器(服务端)上用以回应客户端的请求
client
13
00:00:21,920 --> 00:00:24,400
在客户端方面,Kotlin 现在被广泛应用于安卓开发
on the client Kotlin is now widely used
14
00:00:24,400 --> 00:00:25,519
在客户端方面,Kotlin 现在被广泛应用于安卓开发
for Android development
15
00:00:25,519 --> 00:00:27,680
——为 25 亿台运行安卓操作系统的设备编写应用程序
to write apps for the 2.5 billion
16
00:00:27,680 --> 00:00:29,679
——为 25 亿台运行安卓操作系统的设备编写应用程序
devices running the Android operating
17
00:00:29,679 --> 00:00:30,720
——为 25 亿运行安卓操作系统的设备编写应用程序
system
18
00:00:30,720 --> 00:00:32,960
近几年 Kotlin 的人气大增
Kotlin has seen a huge rise in
19
00:00:32,960 --> 00:00:33,920
近几年 Kotlin 的人气大增
popularity
20
00:00:33,920 --> 00:00:35,440
我能向你保证未来的几年中它只会变得更受欢迎
and i can promise you it will only
21
00:00:35,440 --> 00:00:38,879
我能向你保证未来的几年中它只会变得更受欢迎
become more popular in the years to come
22
00:00:38,879 --> 00:00:40,399
Kotlin 是一种编程语言,是我们与计算机交流的一种方式
Kotlin is an example of a programming
23
00:00:40,399 --> 00:00:42,320
Kotlin 是一种编程语言,是我们与计算机交流的一种方式
language which is a way for me and you
24
00:00:42,320 --> 00:00:44,239
Kotlin 是一种编程语言,是我们与计算机交流的一种方式
to communicate with computers
25
00:00:44,239 --> 00:00:45,920
归根结底,计算机只能理解二进制的数字——也就是 0 和 1
at the end of the day computers only
26
00:00:45,920 --> 00:00:48,480
归根结底,计算机只能理解二进制的数字——也就是 0 和 1
understand binary zeros and ones
27
00:00:48,480 --> 00:00:50,559
所以这些年我们创造了很多种
so over the years we've created many
28
00:00:50,559 --> 00:00:52,640
不同的方法来告诉计算机
different ways to instruct the computer
29
00:00:52,640 --> 00:00:54,719
我们希望它做什么,每一种方法
what we'd like it to do each of these
30
00:00:54,719 --> 00:00:56,239
都被称为一种编程语言
approaches is called a programming
31
00:00:56,239 --> 00:00:57,039
都被称为一种编程语言
language
32
00:00:57,039 --> 00:00:58,399
你可能已经听说过他们中的一些
and you've probably heard of some of them
33
00:00:58,399 --> 00:01:01,920
例如 C 语言、Java 和 JavaScript
such as C or Java or JavaScript
34
00:01:01,920 --> 00:01:03,440
由于 Kotlin 诞生得更晚,它从早期的编程语言中汲取了教训
since Kotlin was created much more recently
35
00:01:03,440 --> 00:01:05,519
由于 Kotlin 诞生得更晚,它从早期的编程语言中汲取了教训
it has taken the lessons from
36
00:01:05,519 --> 00:01:06,640
由于 Kotlin 诞生得更晚,它从早期的编程语言中汲取了教训
the earlier languages
37
00:01:06,640 --> 00:01:08,560
因此它不仅功能更强大,也更易用
to make something that is both powerful
38
00:01:08,560 --> 00:01:10,400
因此它不仅功能更强大,也更易用
and easy to use
39
00:01:10,400 --> 00:01:11,760
Java 和 Kotlin 有一层特殊的关系——它们彼此之间互相兼容
Java and Kotlin have a special
40
00:01:11,760 --> 00:01:13,680
Java 和 Kotlin 有一层特殊的关系——它们彼此之间互相兼容
relationship since they're compatible
41
00:01:13,680 --> 00:01:14,720
Java 和 Kotlin 有一层特殊的关系——它们彼此之间互相兼容
with each other
42
00:01:14,720 --> 00:01:16,560
如果 Java 是今天被设计的,它就会长得像 Kotlin 那样
Kotlin is what Java would look like if
43
00:01:16,560 --> 00:01:18,320
如果 Java 是今天被设计的,它就会长得像 Kotlin 那样
it was designed today
44
00:01:18,320 --> 00:01:19,840
在这个教程中我会使用名叫 IntelliJ IDEA 的集成开发环境(IDE)来编写 Kotlin 代码
to write Kotlin code i'm going to be
45
00:01:19,840 --> 00:01:21,680
在这个教程中我会使用名叫 IntelliJ IDEA 的集成开发环境(IDE)来编写 Kotlin 代码
using a program called intelliJ which
46
00:01:21,680 --> 00:01:22,880
你可以在互联网上免费下载它(jetbrains.com/idea/download)
you can download for free from the
47
00:01:22,880 --> 00:01:23,840
你可以在互联网上免费下载它(jetbrains.com/idea/download)
internet
48
00:01:23,840 --> 00:01:25,680
首先,我创建了一个名为 KotlinMinutes 的空文件
to start out with i created an empty
49
00:01:25,680 --> 00:01:27,840
首先,我创建了一个名为 KotlinMinutes 的空文件
file called KotlinMinutes
50
00:01:27,840 --> 00:01:29,600
Kotlin 文件的扩展名是 .kt
the file extension for Kotlin files is dot kt
51
00:01:29,600 --> 00:01:31,680
我们将在 main 这个特殊的函数中编写代码
and the code we write will be
52
00:01:31,680 --> 00:01:32,400
我们将在 main 这个特殊的函数中编写代码
inside this
53
00:01:32,400 --> 00:01:34,560
我们将在 main 这个特殊的函数中编写代码
special function called main
54
00:01:34,560 --> 00:01:36,000
关于函数,我们之后会讲更多
we'll talk more about functions later
55
00:01:36,000 --> 00:01:37,600
老规矩,我们要做的第一件事就是输出「你好,世界」
in classic fashion the first thing we'll
56
00:01:37,600 --> 00:01:39,040
老规矩,我们要做的第一件事就是输出「你好,世界」
do is print out "Hello world!"
57
00:01:39,040 --> 00:01:40,640
我们在这里调用 println() 函数并在双引号中输入 「Hello World!」
so we'll use this function called
58
00:01:40,640 --> 00:01:42,720
我们在这里调用 println() 函数并在双引号中输入 「Hello World!」
println and type in "Hello world!" in
59
00:01:42,720 --> 00:01:43,680
我们在这里调用 println() 函数并在双引号中输入 「Hello World!」
double quotes
60
00:01:43,680 --> 00:01:45,439
现在我们可以点击这个绿色箭头来编译并运行我们刚刚写好的代码
now we'll execute our code by hitting
61
00:01:45,439 --> 00:01:47,680
现在我们可以点击这个绿色箭头来编译并运行我们刚刚写好的代码
this green arrow which will compile and
62
00:01:47,680 --> 00:01:48,640
现在我们可以点击这个绿色箭头来编译并运行我们刚刚写好的代码
run our code
63
00:01:48,640 --> 00:01:50,479
编译的意思是用你写的代码生成计算机能执行的二进制文件
compiling just means taking the symbols
64
00:01:50,479 --> 00:01:51,920
编译的意思是用你写的代码生成计算机能执行的二进制文件
and keywords you write and turning that
65
00:01:51,920 --> 00:01:53,280
编译的意思是用你写的代码生成计算机能执行的二进制文件
into something that the computer can
66
00:01:53,280 --> 00:01:54,320
编译的意思是用你写的代码生成计算机能执行的二进制文件
actually execute
67
00:01:54,320 --> 00:01:56,240
你可以在弹出的运行工具窗口中看到程序的运行结果
you can see the output in this run tool
68
00:01:56,240 --> 00:01:57,920
在这里它输出了「Hello World!」,符合我们的预期
window that pops up where it says "Hello
69
00:01:57,920 --> 00:01:59,280
在这里它输出了「Hello World!」,符合我们的预期
world!" as we expect
70
00:01:59,280 --> 00:02:00,719
让我们从变量开始我们的 Kotlin 之旅
let's start our tour of Kotlin with variables
71
00:02:00,719 --> 00:02:03,040
变量是一组有名字的数据
a variable is a piece of data
72
00:02:03,040 --> 00:02:03,920
为了创建一个变量,你需要为它指定一个变量名和类型
which has a name and a type
73
00:02:03,920 --> 00:02:06,240
例如 string(字符串)类型,它由一串字符组成
for example strings are a
74
00:02:06,240 --> 00:02:07,520
例如 string(字符串)类型,它由一串字符组成
sequence of characters
75
00:02:07,520 --> 00:02:09,598
所以我的名字可以用 string 类型的变量来储存
so my first name would be stored as a as
76
00:02:09,598 --> 00:02:10,800
所以我的名字可以用 string 类型的变量来储存
a string type
77
00:02:10,800 --> 00:02:12,400
要定义一个变量,我们需要使用 val 关键字
the way we'll declare the variable is
78
00:02:12,400 --> 00:02:14,400
要定义一个变量,我们需要使用 val 关键字
with a special keyword called val
79
00:02:14,400 --> 00:02:15,840
从它在编辑器中被标以不同的颜色你就能知道,它是一个关键字
you can tell it's a keyword since it'll
80
00:02:15,840 --> 00:02:17,520
从它在编辑器中被标以不同的颜色你就能知道,它是一个关键字
turn a different color in the editor
81
00:02:17,520 --> 00:02:19,840
在 Kotlin 中,每个变量都必须属于一种类型
in Kotlin every variable must have a
82
00:02:19,840 --> 00:02:21,120
所以我们说 Kotlin 是静态类型语言
type that's why we say
83
00:02:21,120 --> 00:02:23,040
在这个例子中,这个变量名为 firstName
Kotlin is statically typed so in this
84
00:02:23,040 --> 00:02:24,959
在这个例子中,这个变量名为 firstName
example the name of the variable is
85
00:02:24,959 --> 00:02:27,200
类型为 string
firstName and the type is string
86
00:02:27,200 --> 00:02:28,720
因为我们在定义变量的同时就给这个变量赋了值「Rahul」(一个字符串)
because we're setting the value of the
87
00:02:28,720 --> 00:02:30,640
因为我们在定义变量的同时就给这个变量赋了值「Rahul」(一个字符串)
variable equal to Rahul right away
88
00:02:30,640 --> 00:02:32,319
我们其实不需要专门把这个变量定义为 string 类型
we actually don't need to specify that
89
00:02:32,319 --> 00:02:33,680
这就是 Kotlin 的实用功能之一——类型识别
this is of type string
90
00:02:33,680 --> 00:02:35,360
这就是 Kotlin 的实用功能之一——类型识别
this is a nice feature of Kotlin called
91
00:02:35,360 --> 00:02:37,040
这就是 Kotlin 的实用功能之一——类型识别
type inference where
92
00:02:37,040 --> 00:02:39,040
如果我们赋的值类型很明确,就不需要单独地定义它了
if it's obvious what the type is we
93
00:02:39,040 --> 00:02:40,480
如果我们赋的值类型很明确,就不需要单独地定义它了
don't need to explicitly
94
00:02:40,480 --> 00:02:42,160
如果我们赋的值类型很明确,就不需要单独地定义它了
indicate it there's also an important
95
00:02:42,160 --> 00:02:43,760
还有一个重要概念是只读变量和可写变量
concept of whether a variable is
96
00:02:43,760 --> 00:02:44,720
还有一个重要概念是只读变量和可写变量
read-only or
97
00:02:44,720 --> 00:02:46,959
还有一个重要概念是只读变量和可写变量
readable and writable if the value of a
98
00:02:46,959 --> 00:02:48,000
如果一个变量的值需要在它被初始化后被修改
variable can change
99
00:02:48,000 --> 00:02:49,760
如果一个变量的值需要在它被初始化后被修改
after it's initialized then we have to
100
00:02:49,760 --> 00:02:51,200
我们就需要使用 var 这个关键字来定义它(而不是 val)
declare it with this var
101
00:02:51,200 --> 00:02:53,519
每到假期我的体重都会增加,因为我吃了太多曲奇
keyword since my weight goes up every
102
00:02:53,519 --> 00:02:54,879
每到假期我的体重都会增加,因为我吃了太多曲奇
holiday season because of how many
103
00:02:54,879 --> 00:02:55,920
每到假期我的体重都会增加,因为我吃了太多曲奇
cookies i'm eating
104
00:02:55,920 --> 00:02:58,319
我们要用 var 来定义我的体重,但是我的名字
we'll use var for that but my first name
105
00:02:58,319 --> 00:02:59,280
永远不会变
will never change
106
00:02:59,280 --> 00:03:01,519
所以我们要用 val 来定义
so that'll be a val there are a few
107
00:03:01,519 --> 00:03:02,959
除了刚刚讲的字符串和整型外,Kotlin 还内置了几种数据类型
other built-in types in Kotlin in
108
00:03:02,959 --> 00:03:04,800
除了刚刚讲的字符串和整型外,Kotlin 还内置了几种数据类型
addition to string and integer which we
109
00:03:04,800 --> 00:03:05,519
除了刚刚讲的字符串和整型外,Kotlin 还内置了几种数据类型
just saw
110
00:03:05,519 --> 00:03:08,080
例如 double 是用来存储像 2.5 这样的小数的
for example double is for decimals like
111
00:03:08,080 --> 00:03:09,040
例如 double 是用来存储像 2.5 这样的小数的
2.5
112
00:03:09,040 --> 00:03:11,680
而布尔类型只有两种值——真或者假
and booleans only have two values either
113
00:03:11,680 --> 00:03:12,400
而布尔类型只有两种值——真或者假
true
114
00:03:12,400 --> 00:03:15,200
编写代码的过程中,为后人留下些备注有时是很有帮助的
or false as you write more code it's
115
00:03:15,200 --> 00:03:16,560
编写代码的过程中,为后人留下些备注有时是很有帮助的
sometimes helpful to leave yourself a
116
00:03:16,560 --> 00:03:18,159
它可以告诉将来的自己和别人你为什么这样写这段代码
note about why you wrote the code in a
117
00:03:18,159 --> 00:03:18,959
它可以告诉将来的你和别人你为什么这么写这段代码
certain way
118
00:03:18,959 --> 00:03:20,560
这些备注就叫注释,你可以用双斜杠(//)来让这行代码作为注释被计算机(编译器)忽略
these are called comments and you can
119
00:03:20,560 --> 00:03:23,280
这些备注就叫注释,你可以打出两个斜杠(//)来让这一行作为注释被计算机(编译器)忽略
leave a comment by using a double slash
120
00:03:23,280 --> 00:03:24,959
这些备注就叫注释,你可以打出两个斜杠(//)来让这一行作为注释被计算机(编译器)忽略
commented lines are ignored by the
121
00:03:24,959 --> 00:03:26,879
说完了变量,我们来讲讲运算符
computer now that we've talked about
122
00:03:26,879 --> 00:03:27,519
说完了变量,我们来讲讲运算符
variables
123
00:03:27,519 --> 00:03:29,360
说完了变量,我们来讲讲运算符
let's look at operators which allow us
124
00:03:29,360 --> 00:03:31,440
运算符我们对这些变量进行操作
to manipulate these variables
125
00:03:31,440 --> 00:03:33,680
例如,我们可以用加号(+)来把两个字符串连成一个更长的字符串
for example we can combine two strings
126
00:03:33,680 --> 00:03:35,440
例如,我们可以用加号(+)来把两个字符串连成一个更长的字符串
into a longer string with a plus sign
127
00:03:35,440 --> 00:03:37,599
这种操作被称为字符串串接
which is called concatenation here we're
128
00:03:37,599 --> 00:03:39,519
在这里我们把字符串 s1 和 s2 串接的结果存储在变量 combined 中并打印了出来
printing out the value of s1 plus
129
00:03:39,519 --> 00:03:41,840
在这里我们把字符串 s1 和 s2 串接的结果存储在变量 combined 中并打印了出来
s2 in this variable called combined so
130
00:03:41,840 --> 00:03:43,120
所以我们运行这个程序的时候我们就会得到“call me maybe”的输出
when we run the program we get the(Carly Rae Jepsen 的一首歌)
131
00:03:43,120 --> 00:03:44,640
所以我们运行这个程序的时候我们就会得到“call me maybe”的输出
result of call me maybe which is a
132
00:03:44,640 --> 00:03:46,080
它是 "call me " 和 "maybe" 串接的结果
concatenation of call me
133
00:03:46,080 --> 00:03:48,480
它是 "call me " 和 "maybe" 串接的结果
and maybe this is an example of a binary
134
00:03:48,480 --> 00:03:50,400
这是一种二目运算符,因为它需要输入两个值
operator because it takes in two inputs
135
00:03:50,400 --> 00:03:51,840
如你所料,还有几种用于数字的二目运算符
there are several different binary
136
00:03:51,840 --> 00:03:53,360
如你所料,还有几种用于数字的二目运算符
operators for numbers as you might
137
00:03:53,360 --> 00:03:54,000
如你所料,还有几种用于数字的二目运算符
expect
138
00:03:54,000 --> 00:03:55,920
我们可以把这里的两个整数 9 和 4 相加得到和 13
with two integers here having value nine
139
00:03:55,920 --> 00:03:57,280
我们可以把这里的两个整数 9 和 4 相加得到和 13
and four we can add them together and
140
00:03:57,280 --> 00:03:58,480
我们可以把这里的两个整数 9 和 4 相加得到和 13
get thirteen
141
00:03:58,480 --> 00:04:01,840
我们也可以用 n1 减去 n2、乘上 n2、除以 n2
we can also subtract them multiply them
142
00:04:01,840 --> 00:04:04,560
或使用百分号(%)获取 n1 除以 n2 的余数
divide them or find the remainder after
143
00:04:04,560 --> 00:04:06,319
或使用百分号(%)获取 n1 除以 n2 的余数
the division with this percent sign
144
00:04:06,319 --> 00:04:08,879
在这里取模运算的结果会是 1
that would be 1 in this case let's talk
145
00:04:08,879 --> 00:04:10,239
是时候再拓展一下字符串相关的知识了
a bit more about strings
146
00:04:10,239 --> 00:04:11,840
字符串是一个字符序列
strings are a sequence of characters and
147
00:04:11,840 --> 00:04:12,959
你可以用它实现很多有用的功能,例如通过下标取回字符串中特定位置的字符
there are a bunch of useful things you
148
00:04:12,959 --> 00:04:14,319
你可以用它实现很多有用的功能,例如通过下标取回字符串中特定位置的字符
can do with them for example
149
00:04:14,319 --> 00:04:16,160
你可以用它实现很多有用的功能,例如通过下标取回字符串中特定位置的字符
retrieving a character in the series by
150
00:04:16,160 --> 00:04:17,600
你可以用它实现很多有用的功能,例如通过下标取回字符串中特定位置的字符
indexing into the string
151
00:04:17,600 --> 00:04:19,680
Kotlin 的下标和大多数编程语言一样从 0 开始
like this in Kotlin like most other
152
00:04:19,680 --> 00:04:21,279
Kotlin 的下标和大多数编程语言一样从 0 开始
programming languages we start counting
153
00:04:21,279 --> 00:04:23,120
所以第一个字符K在下标为0的位置上,第二个字符O在下标为1的位置上
at zero so the first character K
154
00:04:23,120 --> 00:04:24,560
所以第一个字符K在下标为0的位置上,第二个字符O在下标为1的位置上
will be at position zero and the second
155
00:04:24,560 --> 00:04:26,400
所以第一个字符K在下标为0的位置上,第二个字符O在下标为1的位置上
character O will be position one in the
156
00:04:26,400 --> 00:04:28,000
我们可以看到KOTLIN的前两个字母被打印出来了
output we can see the first two
157
00:04:28,000 --> 00:04:30,639
我们也可以用 isEmpty() 方法来检查字符出是否为空
letters of Kotlin we can also check if a
158
00:04:30,639 --> 00:04:32,320
我们也可以用 isEmpty() 方法来检查字符出是否为空
string is empty using the method is
159
00:04:32,320 --> 00:04:34,000
它的返回值为布尔类型,即真或假
empty which returns a boolean true or
160
00:04:34,000 --> 00:04:34,880
它的返回值为布尔类型,即真或假
false value
161
00:04:34,880 --> 00:04:36,800
我们还可以在变量名后面加上 .length 来获取一个字符串的长度,返回值为整形
or we can get the length of a string
162
00:04:36,800 --> 00:04:38,880
我们还可以在变量名后面加上 .length 来获取一个字符串的长度,返回值为整形
which is an integer by using the dot
163
00:04:38,880 --> 00:04:39,919
我们还可以在变量名后面加上 .length 来获取一个字符串的长度,返回值为整形
length property
164
00:04:39,919 --> 00:04:42,160
运行时我们可以得知我的字符串不为空,因为其中有字符
if we run this we can see that my string
165
00:04:42,160 --> 00:04:43,919
运行时我们可以得知我的字符串不为空,因为其中有字符
Kotlin is not empty because it has
166
00:04:43,919 --> 00:04:44,560
运行时我们可以得知我的字符串不为空,因为其中有字符
characters
167
00:04:44,560 --> 00:04:46,080
字符串中有 6 个字母,因此字符串的长度为 6。
and there are six characters which is
168
00:04:46,080 --> 00:04:47,759
字符串中有 6 个字母,因此字符串的长度为 6。
why the length is six
169
00:04:47,759 --> 00:04:49,759
substring() 方法则会根据我们提供的起止下标从字符串中取出子串
another method is substring which will
170
00:04:49,759 --> 00:04:51,680
substring() 方法则会根据我们提供的起止下标从字符串中取出子串
extract a portion of the string between
171
00:04:51,680 --> 00:04:53,919
substring() 方法则会根据我们提供的起止下标从字符串中取出子串
the start and end index that we provide
172
00:04:53,919 --> 00:04:57,280
在这里 substring(2, 4) 会返回 TL
so substring with parameters 2 and 4
173
00:04:57,280 --> 00:05:00,080
因为我们从下标为 2 的 T 开始
will output TL because we will start at
174
00:05:00,080 --> 00:05:00,479
因为我们从下标为 2 的 T 开始
T
175
00:05:00,479 --> 00:05:04,080
往后取两位,取到下标为 3 的 L 为止,不包括下标为 4 的 I
which is index 2 include index 3 the l
176
00:05:04,080 --> 00:05:07,280
往后取两位,取到下标为 3 的 L 为止,不包括下标为 4 的 I
and then go up 2 but not include index 4
177
00:05:07,280 --> 00:05:08,560
往后取两位,取到下标为 3 的 L 为止,不包括下标为 4 的 I
the i
178
00:05:08,560 --> 00:05:10,080
当你写了更多 Kotlin 代码之后,有个实用的建议:
one really handy tip as you start
179
00:05:10,080 --> 00:05:12,000
当你写了更多 Kotlin 代码之后,有个实用的建议:
writing more Kotlin is to explore
180
00:05:12,000 --> 00:05:13,520
在你的变量名后面输入英文句点(.)来探索更多可用的方法
other methods available to you by
181
00:05:13,520 --> 00:05:15,199
在你的变量名后面输入英文句点(.)来探索更多可用的方法
hitting the dot or period
182
00:05:15,199 --> 00:05:17,280
在你的变量名后面输入英文句点(.)来探索更多可用的方法
after writing the name of your variable
183
00:05:17,280 --> 00:05:19,039
你会看见一个自动完成对话框
you'll get an auto complete dialog
184
00:05:19,039 --> 00:05:21,039
它会向你展示你能用这个变量做的所有操作
which shows you all the possible things
185
00:05:21,039 --> 00:05:22,560
它会向你展示你能用这个变量做的所有操作
you can do with this
186
00:05:22,560 --> 00:05:24,960
句号可能是 Kotlin 中最重要的符号之一
variable the period is probably one of
187
00:05:24,960 --> 00:05:27,199
句号可能是 Kotlin 中最重要的符号之一
the most important symbols in Kotlin
188
00:05:27,199 --> 00:05:29,520
因为它能让我们使用这种语言的自带功能
since it allows us to use the built-in
189
00:05:29,520 --> 00:05:31,680
因为它能让我们使用这种语言的自带功能
functionality of the language
190
00:05:31,680 --> 00:05:33,600
值得指出的是,你能使用的方法与你的变量类型有关
one thing worth pointing out is that the
191
00:05:33,600 --> 00:05:35,039
值得指出的是,你能使用的方法与你的变量类型有关
options and methods that you get
192
00:05:35,039 --> 00:05:37,280
值得指出的是,你能使用的方法与你的变量类型有关
will depend on the type of the variable
193
00:05:37,280 --> 00:05:38,800
例如这个 int 类型的变量适用的方法就与 string 类型不同
for example the options here
194
00:05:38,800 --> 00:05:40,320
例如这个 int 类型的变量适用的方法就与 string 类型不同
with the integer will be different than
195
00:05:40,320 --> 00:05:42,000
例如这个 int 类型的变量适用的方法就与 string 类型不同
what we had with the string
196
00:05:42,000 --> 00:05:44,160
关于字符串的最后一个知识点
one last point on the topic of strings
197
00:05:44,160 --> 00:05:45,759
我们常常想要在字符串中输出一个变量的值
we'll frequently want to print out the
198
00:05:45,759 --> 00:05:46,960
我们常常想要在字符串中输出一个变量的值
value of a variable
199
00:05:46,960 --> 00:05:49,120
这种情况下我们可以用美元符号($)来进行字符串插值
inside a string for that we'll use the
200
00:05:49,120 --> 00:05:51,360
这种情况下我们可以用美元符号($)来进行字符串插值
dollar sign to do string interpolation