根据Java的范围规则,在外部作用域中声明的对象对内部作用域内的代码是可见的,反之亦然。
现在,考虑一下这个程序:
public class Scope {
public static void main (String args[]) {
int x = 10; //known to all code within main
if (x == 10) {
int y = 20;
System.out.println("x and y: " + x + " " + y);
x = y * 2;
}
//y = 100;
System.out.println("x is: " + x);
}
}
它的输出是:
x and y: 10 20
{
{1}}
为什么x is: 40
x
在这里? 40
位于x = y * 2
内,因此,if
已定位到那里,所以第二次不应再y
为x
?为什么20
从x
中获取40
,因为在内部范围内声明的对象在外部不可见?
答案 0 :(得分:1)
identify -format %# someImage.png
e74164f4bab2dd8f7f612f8d2d77df17106bac77b9566aa888d31499e9cf8564
的范围有限,但是y
不是。 x
是20 * 2
。 40
是作业,x = 40
在块之后仍然可见。 x
没有" fetch" fetch"四十,四十被分配到x
(然后x
超出范围)。你可以把它想象成,
y
通知if (x == 10) {
System.out.println("x and y: " + x + " 20");
x = 40;
}
可以完全从讨论中删除。
答案 1 :(得分:1)
x
在if
语句之外声明,因此它的范围是整个 main
方法(无论它是否在{{内部的任何循环内部更改) 1}}方法,所有更改都会影响它,因为它在外部声明)。在main
语句中声明了y
,因此您无法在其外部使用if
,但x
对if
内x
所做的所有更改均可见/* HackerOne/Callout 1 */
.callout1 {
display: table;
width: 100%;
height: 420px;
color: #fff;
background: url(../img/hacker-home-res2.png) no-repeat center center scroll;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
}
/* Icons/Callout 2 */
.callout2 {
display: table;
width: calc(50%);
height: 390px;
color: #fff;
float: left;
background: url(../img/icons-new.png) no-repeat center center scroll;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
}
/* CodeFu/Callout 3 */
.callout3 {
display: table;
width: calc(50%);
height: 390px;
float: right;
color: #fff;
background: url(../img/codefu-new-header.png) no-repeat center center scroll;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
}
/* Flourgirl/Callout 6 */
.callout6 {
display: table;
width: 100%;
height: 420px;
color: #fff;
background: url(../img/flourgirl-logo2.png) no-repeat center center scroll;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
}
/* Seek/Callout 4 */
.smaller {
width: 102.7%;
}
.callout4 {
display: table;
width: calc(50%);
height: 390px;
color: #fff;
float: left;
background: url(../img/seek-logo.png) no-repeat center center scroll;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
}
/* Addaptive/Callout 5 */
.callout5 {
display: table;
width: calc(50%);
height: 390px;
float: right;
color: #fff;
background: url(../img/addaptive-logo-res.png) no-repeat center center scroll;
-webkit-background-size: cover;
-moz-background-size: cover;
background-size: cover;
-o-background-size: cover;
}
.service-icon {
background-color: #fff;
color: #1d809f;
height: 7rem;
width: 7rem;
display: block;
line-height: 7.5rem;
font-size: 2.25rem;
box-shadow: 0 3px 3px 0 rgba(0, 0, 0, .1)
}
.callout {
padding: 15rem 0;
background: linear-gradient(90deg, rgba(255, 255, 255, .1) 0, rgba(255, 255, 255, .1) 100%), url(../img/bg-callout.jpg);
background-position: center center;
background-repeat: no-repeat;
background-size: cover
}
.callout h2 {
font-size: 3.5rem;
font-weight: 700;
display: block;
max-width: 30rem
}
.portfolio-item {
display: block;
position: relative;
overflow: hidden;
max-width: 530px;
margin: auto auto 1rem
}
.portfolio-item .caption {
display: flex;
height: 100%;
width: 100%;
background-color: rgba(33, 37, 41, .2);
position: absolute;
top: 0;
bottom: 0;
z-index: 1
}
.portfolio-item .caption .caption-content {
color: #fff;
margin: auto 2rem 2rem
}
.portfolio-item .caption .caption-content h2 {
font-size: .8rem;
text-transform: uppercase
}
.portfolio-item .caption .caption-content p {
font-weight: 300;
font-size: 1.2rem
}
@media (min-width:992px) {
.portfolio-item {
max-width: none;
margin: 0;
}
.portfolio-item .caption {
-webkit-transition: -webkit-clip-path .25s ease-out, background-color .7s;
-webkit-clip-path: inset(0);
clip-path: inset(0)
}
.portfolio-item .caption .caption-content {
transition: opacity .25s;
margin-left: 5rem;
margin-right: 5rem;
margin-bottom: 5rem
}
.portfolio-item img {
-webkit-transition: -webkit-clip-path .25s ease-out;
-webkit-clip-path: inset(-1px);
clip-path: inset(-1px)
}
.portfolio-item:hover img {
-webkit-clip-path: inset(2rem);
clip-path: inset(2rem)
}
.portfolio-item:hover .caption {
background-color: rgba(29, 128, 159, .9);
-webkit-clip-path: inset(2rem);
clip-path: inset(2rem)
}
}
在外面,因为它在外面宣布。
答案 2 :(得分:0)
public class Scope {
public static void main(String args[]) {
// visible to whole main()
int x = 10; //known to all code within main
if (x == 10) {
// visible to if statement only
int y = 20;
System.out.println("x and y: " + x + " " + y);
// this is not local x, this is int x = 10 from main function
x = y * 2;
}
// y is not visible here (this is not if block)
// x is visible and modified
System.out.println("x is: " + x);
}
}
答案 3 :(得分:0)
新功能定义了一个新的有限范围:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ServerNew 1731 root 4u IPv4 24114 0t0 TCP *:8090 (LISTEN)