ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

《迪哥Java教程》#16 Java里的代码块 – 译学馆
未登录,请登录后再发表信息
最新评论 (0)
播放视频

《迪哥Java教程》#16 Java里的代码块

How to use Java blocks! - 016

大家好 我们需要把一组java语句放到块中
Hi there! We need a group Java statements together in to blocks.
我说的块是指语句块 有许多关于java
By blocks I mean a block of statements. There are rules though
如何执行这些语句块的规则
on how Java works between the blocks.
接下来我们会看到如何创建Java语句块
We’re going to look at how we create Java blocks,
如何把它们放到一起
how we put them together,
以及如何把这些语句块整合到一个Java应用中
and how we start to build these blocks into a full Java application.
本期课程中我们会讲解
In this lesson we’re going to answer what
什么是Java语句
is a Java statement.
我们会看到怎样把语句组合成Java语句块
We’ll look at how to group these statements into a Java block.
关于Java语句块 我们需要理解变量作用域
With Java blocks we’ll need to understand variable scope
因为我们会嵌套Java语句块
as we nest the Java blocks.
我们也可以直观地看到 为什么在非必要的情况下
We’ll also see firsthand why we need to use braces to define blocks
也要用大括号来定义语句块
even when it’s not a hundred percent necessary.
我们就从什么是Java语句开始吧
Let’s start with what is a Java statement.
你可以把Java中的语句
You can think of a statement
想象成一个简单的句子
in Java similar to a simple sentence.
我之所以说简单
I say simple because
是因为每一条Java语句都应该只做一件事
each Java statement should do just one thing.
在Java中 你可以把语句组合起来 把这些语句的集合
In Java you can also group statements and represent the collection of statements
看作一条语句
as one statement.
在Java中 语句是从上到下 从左到右读的
In Java, statements are read in your program top-down and then left to right.
每条Java语句都表示一条可执行命令
Each Java statement represents a command to execute.
基本上每条语句就是告诉Java“这样做”
With each statement you’re basically telling Java “go do this”.
Java中有两类语句
Java has two categories of statements available.
声明语句和控制语句
Declarative statements and control statements.
顾名思义 声明语句就是声明一个事实
Declarative statements do exactly what it sounds like, declare a fact.
我声明了一个整型变量“i”
I declare a variable of type int named “i”
等于42 声明语句可以用来给变量赋值
to be equal to 42. We use declarative statements to assign values to variables.
也可以用声明语句修改变量的值
We also use declarative statements to change the value of variables.
待会 我们可能就会说“i”等于100了
So later on we might say “i” is now 100.
这也是一个声明语句
That is also a declarative statement.
控制语句可以控制Java应用的运行流程
Control statements help you control the flow the Java application.
Java中的控制语句类型包括做决策 执行循环
Java has control statements for making decisions, performing loops,
以及分支到其他代码块
and branching to other sections of code.
决策控制语句的意思是
A decision control statement would state,
如果条件为真 就运行这个代码块
if something’s “true” run this block of code.
循环控制语句是说
A looping control statement says,
如果我不说停止 就一遍遍地运行这段代码
run this code over and over and over until I say stop.
分支控制语句会告诉Java跳到这里 并执行这个代码块
Branching control statements tell Java to jump over here and run this block of code.
一个运行代码块的例子是
An example of running a block of statements
向屏幕打印东西
is when we print things to the screen.
这么做时 它背后会执行许多操作 但是我们只用这一句来表示
Lots of things happen behind the scenes, but we represent it with this one statement.
我们把Java语句的集合叫做什么呢
So what would we call a group of Java statements?
我们把Java语句的集合叫语句块
We call a collection of Java statements a Java block.
就像这些玩具一样
Now just like the toys,
我们用控制语句把这些块连接起来构成一个Java程序
we connect these blocks with control statements to form a Java program.
多个语句块可以组合成更大的语句块
Blocks can then be assembled into larger blocks.
语句块通常用开始和结尾的大括号定义
Blocks are usually defined by starting brace and an ending brace.
我们来看一下语句块
Let’s take a look at the blocks.
Java程序就是一个由许多小语句块构成的大语句块
A Java program is really a big block made of many little blocks.
这里的关键点是
The important takeaway here is your Java
Java应用是由多个小语句块组成的大语句块
application is one big block composed of smaller blocks.
在Java程序中
Inside your Java program
你需要写一些小代码块 通常是方法或类
you’ll have smaller blocks of code. Often these are methods or classes.
前面我们已经见过了
We’ve seen this before.
这是main方法 它是所有Java程序的开始
This is the main method which is the start of every Java application.
main方法就是一个Java语句块
The main method is a Java block.
它包含在一个表示整个Java程序的更大的语句块中
Its enclosed in the larger block up the Java application.
这个更大的语句块是由小语句块组成的
The larger block is made from and encloses smaller blocks.
我们可以创建无数层的嵌套语句块
We can keep creating blocks inside blocks inside blocks to infinity…
都是像这样的组织方式
It’s blocks all the way down.
这个例子中有一个if-then语句 下次课我们会介绍
In this case an if-then control statement, something we’re covering in the next lesson,
它也包含一个语句块
contains another Java block.
在每一层中 都可以看到语句块用大括号括起来
At each level you can see the braces that enclose the Java block.
当然了并不是每个语句块都需要大括号
Of course you don’t need braces for every block.
否则每条语句就都需要大括号了
otherwise every statement would need braces around it.
Java已经因为大括号太多而名声很差了
Java already has a bad reputation for having too many braces,
那样只会使它名声更差
and that would just make it worse.
就像这里的一样 你可以使语句块不包含大括号
You can have a block that does not have braces as this case shows.
if-then控制语句会把单独一条语句当做一个语句块
The if-then control statement can branch to block that is a single statement.
这种代码形式很糟糕 你不会想这样写的
This is bad form though, and you really don’t want to do it.
关于下面这一点 人们可能有争议
Now some might argue this point,
即缩进有无均可 开发者之间并不能达成一致
but spacing is optional and is inconsistent between developers.
如果你在橙色语句块中添加一条语句 别人会怎么想呢
Look at what happens if someone thinks you’re adding a statement to the orange block.
因为缩进 他们可能会忽略橙色语句块缺少大括号
Because of the spacing, they might miss that the orange block is missing braces.
因为缩进的存在 看起来这两条语句属于同一语句块
From the indenting, it looks like the two statements are part of the same block.
它实际的形式是这样的
Here’s what this code really looks like.
我们的预期行为是
The expected behavior is
第二条打印语句也不会执行
the second print statement is also not run,
但因为它属于蓝色语句块 而不是橙色语句块
but since is really part of the blue block, and not the orange block,
所以第二条打印语句总会执行
the second print statement will always be run.
我们运行一下代码 看看会发生什么
Let’s run this in code and see what happens.
这里就是我们在幻灯片上看到的代码
Here we have the code exactly how we had it in the slide.
我们会用NetBeans调试器单步调试这个程序
We’re going step through this program using the NetBeans debugger.
我会演示如何做
I’ll show you how to do that.
首先在第12行设置一个断点
First we’re going to set a breakpoint on line 12.
断点就是
What a breakpoint is,
在我们运行代码时 告诉调试器在这里停下
is it tells our debugger stop here when we start running our code.
然后我们就可以一次执行一条语句了
And then we can implement our code by one step at a time.
这样我们就可以真正看到程序运行过程了
So we can actually see the flow of our application.
现在我们已经设置了断点
Now that we’ve set our breakpoint,
对于缩进的这些代码 我们希望
what we’re expecting here from the indentation is
程序能够运行
this program will run.
并且打印出“byte b = 42”
It will print out byte b = 42,
因为这个代码块的条件为假
and then since the block is false,
它不应该打印出“inside block”以及“not inside block”
it shouldn’t print out inside block and not inside block.
选择“Debug” “Debug project”
So we go to “Debug | Debug project”.
程序停在了断点处
And then it stops on our breakpoint.
接下来点击这个按钮
So next we’re going to hit this button,
“跳过表达式”
“Step over expression”.
继续 系统输出了“b = 42”
We go, and it’s the system print out “b = 42”, and prints it out.
然后一旦遇到“if(false)”语句 它应该就结束了
Then it should be done once it hits the “if (false)”,
但它会执行不在语句块里的这条语句
but it runs the not inside block statement.
从代码格式来看 这不是预期的结果
That isn’t the expected behavior from way it looks.
它输出了“byte b = 42”
So our code had “byte b = 42”,
以及“not inside block”
and “not inside block”.
这个故事告诉我们不要这样写
The moral of the story is don’t do this!
它会使程序流变得很糟糕
It’s crossing the streams bad.
你无法知道会发生什么 现在我们看一下作用域
You never know what’s going to happen. Now let’s take a look at scope.
在蓝色语句块中 我们声明了一个变量b 并把它打印到了屏幕上
In the blue block, we’re declaring a variable b and printing it to the screen.
它能正常运行是因为声明和打印在同一语句块
This works because both the declaration and the print are in the same block.
如果我们在橙色语句块中声明一个变量会怎样呢
Now look what happens when we declare a variable inside the orange block.
我们在里面声明了一个变量k 并使它的值为24
Here we’re declaring a variable “k” and setting it equal to 24.
声明语句是在橙色语句块内
The declaration happens inside the orange block.
如果在蓝色语句块中打印k 你觉得会发生什么呢
So what do you think happens when we try to print “k” in the blue scope?
我们来试一下 这就是幻灯片中的代码
Well let’s go try. Okay we have the code exactly as we have it in slide.
首先我们尝试运行它
First thing we’re going to do is try to run it.
点击“Run | Bulid project”
And we hit “Run | Build project”
它出现了一个编译错误
and we get a compilation error.
这段代码甚至无法编译
This code won’t even compile.
错误是
The error
它无法找到18行的符号
is it cannot find the symbol on line 18.
如果我们仔细看代码 就可以发现
Now if we look closely in our code, we would have seen the little
k下面有一个红色的小标记
red mark underneath our “k”,
它告诉我们“无法找到符号k”
which would have told us “Cannot find symbol, Symbol variable k”.
这是因为k不在这个作用域内
The reason is k is not in scope here.
k只存在于这个if语句块内
The only place k exists is inside the block of this if statement.
我们已经看到 k在这个封闭的作用域内是不可见的
So what we’ve seen is, k is not visible in the enclosing scope.
你无法在蓝色语句块内访问到k 它只存在于橙色语句块内
You cannot see k in the blue block. It only exists in the orange block.
如果我们在橙色语句块内打印b会怎样呢
So what do you think will happen if we try to print b in the orange block?
我们来试一下吧
Let’s go try it!
我们已经知道不能在这打印b
Okay we know that we can’t print k here,
我们继续写 把这些删除
so let’s go ahead and delete that out.
看看我们能否在if语句块内打印b
Let’s see if we can print b inside the if block.
b是在上面声明的 不在if语句块内
So b is declared above, outside of the if block.
目前为止 看起来还不错
Okay, looks good so far.
我们能打印b
And we were able to print b.
在这种情况下 b是可见的
So in this case, b is visible.
变量只能在它定义的语句块
Variables can be seen only in the block it’s defined
以及该语句块包含的语句块内访问到
and in any in block it encloses.
这种情况下 b可以被访问到
So in this case b can be seen, or
也可以说“b在作用域内” 在蓝色和橙色的的语句块内
is “in scope”, in the blue and orange blocks
变量k无法在蓝色语句块内访问到
The k variable cannot be seen from the blue scope.
从蓝色语句块的角度看 它“不在作用域内”
It’s said to be “out of scope” from the view of the blue block.
有时候 变量的作用域会和生命周期混淆
Scope sometimes confused with the lifetime with the variable.
有时它们相同 我们会在其他课程中讲解
Sometimes this is true, but we’re going to see in other lessons,
变量的生命周期可能会超出程序目前执行的作用域
the lifetime of the variable can extend past the scope where the program is currently running.
现在只需要理解作用域的原理就可以了
Right now, just understand how scope works.
最后一件需要注意的事是空语句块
The last thing to note is empty blocks.
你已经知道了语句块可以没有大括号
You’ve seen your statement can be a complete block without braces.
同样 大括号内也可以没有语句
You can also have braces with no statements inside.
这样就是一个空语句块
This defines an empty block.
如果你需要一个空语句块
If you ever find yourself needing an empty block,
要在空语句块内写上注释 说明你这么做的目的和原因
leave a comment inside the empty block noting you did that on purpose and why.
通常我们不需要空语句块
Usually there’s no reason to have an empty block.
但是你可以这么做 有的地方它也有意义 比如说接口
But you can do it and there’s few instances where it might make sense like in interfaces.
后面我们会讲到
We’ll cover that much later.
本节课程中 我们学习了Java语句可以是
Okay In this lesson we’ve learned that a Java statement can either be
声明语句或控制语句
a declarative statement or control statement.
声明语句声明一个事实
Declarative statements declare facts.
控制语句控制程序的运行流向
And control statements control the flow of your application.
我们已经讲过许多声明语句了
We’ve covered a ton of declarative statements,
下节课我们开始讲述控制语句
so let’s start looking at control statements in the next lesson!
喂 感谢观看本视频
Hey! Thanks for watching the video.
如果你想测试自己学到多少的话
There is a quick quiz for this on DeegeU.com to if you’d
在DeegeU.com上有本节的快速测验
like to gauge how much you learned.
如果你喜欢本视频
If you like the videos are seeing,
一定要点击“喜欢”来告诉我 并且订阅youtube上的DeegeU频道
please let me know by liking the video and hitting the subscribe button for the DeegeU channel on YouTube.
非常感谢
I’d really appreciate that!
如果你有困惑或问题 请在评论区或DeegeU.com留言
If you have concerns or questions please leave them in the comments below or on DeegeU.com.
DeegeU.com主页上有一个投票
There’s a poll on the front page of DeegeU.com
这样你就可以告诉我下次要讲什么了
so you can also let me know the topics covered next.
感谢观看 下次见
Thanks for watching, and see you next video!

发表评论

译制信息
视频概述

本节讲述了控制语句和声明语句,变量的生命周期和作用域以及语句块。

听录译者

收集自网络

翻译译者

[B]hugue

审核员

审核团1024

视频来源

https://www.youtube.com/watch?v=3vZ-JbTTa6s

相关推荐