首页 \ 问答 \ 在java中,为什么需要将闭包变量声明为final?(in java, why do closured variables need to be declared final?)

在java中,为什么需要将闭包变量声明为final?(in java, why do closured variables need to be declared final?)

final Object o;

List l = new ArrayList(){{
    // closure over o, in lexical scope
    this.add(o);
}};

为什么必须宣布最终? 为什么其他具有可变变量的JVM语言不具备此要求?


final Object o;

List l = new ArrayList(){{
    // closure over o, in lexical scope
    this.add(o);
}};

why must o be declared final? why don't other JVM languages with mutable vars have this requirement?


原文:https://stackoverflow.com/questions/10626132
更新时间:2021-03-24 06:03

最满意答案

我会认为圆是主要元素,我会使用一个伪元素,我用旋转的SVG背景填充所有这些线。

body {
 margin::0;
 overflow:hidden;
}

.circle {
  height:100px;
  width:100px;
  margin:50px;
  background:red;
  border-radius:50%;
  position:relative;
}
.circle:before {
    content:"";
    position:absolute;
    top:-5000%;
    left:-5000%;
    right:-5000%;
    bottom:-5000%;
    background-image:
    url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="red" stroke-width="0.05" /></svg>'),
    url('data:image/svg+xml,<svg style="transform:rotate(20deg)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="red" stroke-width="0.05" /></svg>'),
    url('data:image/svg+xml,<svg style="transform:rotate(40deg)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="red" stroke-width="0.05" /></svg>'),
    url('data:image/svg+xml,<svg style="transform:rotate(60deg)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="red" stroke-width="0.05" /></svg>'),
    url('data:image/svg+xml,<svg style="transform:rotate(80deg)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="red" stroke-width="0.05" /></svg>'),
    url('data:image/svg+xml,<svg style="transform:rotate(100deg)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="red" stroke-width="0.05" /></svg>'),
    url('data:image/svg+xml,<svg style="transform:rotate(120deg)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="red" stroke-width="0.05" /></svg>'),
    url('data:image/svg+xml,<svg style="transform:rotate(140deg)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="red" stroke-width="0.05" /></svg>'),
    url('data:image/svg+xml,<svg style="transform:rotate(160deg)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="red" stroke-width="0.05" /></svg>');
    background-size:100% 100%;
  animation:animate 6s infinite linear;
}

@keyframes animate {
  from {
    transform:rotate(0);
  }
  to {
    transform:rotate(360deg);
  }
}
<div class="circle">

</div>

为了使事情变得更容易,您可以依赖JS来生成背景代码:

var all = document.querySelectorAll('.circle');

for (var i = 0; i < all.length; i++) {
  var c = all[i].getAttribute("data-color");
  var s = parseInt(all[i].getAttribute("data-step"));
  var b = 'url(\'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="' + c + '" stroke-width="0.05" /></svg>\')';
  var end = 180 / s;
  for (var j = 1; j < end; j++) {
    b += ',url(\'data:image/svg+xml,<svg style="transform:rotate(' + s * j + 'deg)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="' + c + '" stroke-width="0.05" /></svg>\')';
  }
  all[i].style.setProperty("--b", b);
  all[i].querySelector('span').style.setProperty("background", c);

}
body {
  overflow: hidden;
}

.circle {
  height: 100px;
  width: 100px;
  position:absolute;
}
.circle span {
  position:relative;
  height:100%;
  width:100%;
  display:flex;
  justify-content:center;
  align-items:center;
  z-index:3;
  border-radius: 50%;
  color:#fff;
}

.circle:after {
  content: "";
  z-index: -1;
  position: absolute;
  top: -5000%;
  left: -5000%;
  right: -5000%;
  bottom: -5000%;
  background-image: var(--b);
  background-size: 100% 100%;
  animation: animate 10s infinite linear;
}

@keyframes animate {
  from {
    transform: rotate(0);
  }
  to {
    transform: rotate(360deg);
  }
}
<!-- step will define the degree between each line -->

<div class="circle" data-color="red" data-step="10">
<span>some text</span>
</div>
<div class="circle" style="top:150px;left:150px;" data-color="green" data-step="20">
<span>text</span>
</div>

<div class="circle" style="left:250px;" data-color="orange" data-step="30">
<span>more here</span>
</div>

<div class="circle" style="right:50px;bottom:50px" data-color="cyan" data-step="10">
<span>more here</span>
</div>


I would consider the circle as the main element and I would use a pseudo element that I fill with all these lines using rotated SVG background.

body {
 margin::0;
 overflow:hidden;
}

.circle {
  height:100px;
  width:100px;
  margin:50px;
  background:red;
  border-radius:50%;
  position:relative;
}
.circle:before {
    content:"";
    position:absolute;
    top:-5000%;
    left:-5000%;
    right:-5000%;
    bottom:-5000%;
    background-image:
    url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="red" stroke-width="0.05" /></svg>'),
    url('data:image/svg+xml,<svg style="transform:rotate(20deg)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="red" stroke-width="0.05" /></svg>'),
    url('data:image/svg+xml,<svg style="transform:rotate(40deg)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="red" stroke-width="0.05" /></svg>'),
    url('data:image/svg+xml,<svg style="transform:rotate(60deg)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="red" stroke-width="0.05" /></svg>'),
    url('data:image/svg+xml,<svg style="transform:rotate(80deg)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="red" stroke-width="0.05" /></svg>'),
    url('data:image/svg+xml,<svg style="transform:rotate(100deg)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="red" stroke-width="0.05" /></svg>'),
    url('data:image/svg+xml,<svg style="transform:rotate(120deg)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="red" stroke-width="0.05" /></svg>'),
    url('data:image/svg+xml,<svg style="transform:rotate(140deg)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="red" stroke-width="0.05" /></svg>'),
    url('data:image/svg+xml,<svg style="transform:rotate(160deg)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="red" stroke-width="0.05" /></svg>');
    background-size:100% 100%;
  animation:animate 6s infinite linear;
}

@keyframes animate {
  from {
    transform:rotate(0);
  }
  to {
    transform:rotate(360deg);
  }
}
<div class="circle">

</div>

And to make thing easier you may rely on JS to generate the code of the background:

var all = document.querySelectorAll('.circle');

for (var i = 0; i < all.length; i++) {
  var c = all[i].getAttribute("data-color");
  var s = parseInt(all[i].getAttribute("data-step"));
  var b = 'url(\'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="' + c + '" stroke-width="0.05" /></svg>\')';
  var end = 180 / s;
  for (var j = 1; j < end; j++) {
    b += ',url(\'data:image/svg+xml,<svg style="transform:rotate(' + s * j + 'deg)" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200" height="5000" width="5000"><line x1="100" y1="0" x2="100" y2="200" stroke="' + c + '" stroke-width="0.05" /></svg>\')';
  }
  all[i].style.setProperty("--b", b);
  all[i].querySelector('span').style.setProperty("background", c);

}
body {
  overflow: hidden;
}

.circle {
  height: 100px;
  width: 100px;
  position:absolute;
}
.circle span {
  position:relative;
  height:100%;
  width:100%;
  display:flex;
  justify-content:center;
  align-items:center;
  z-index:3;
  border-radius: 50%;
  color:#fff;
}

.circle:after {
  content: "";
  z-index: -1;
  position: absolute;
  top: -5000%;
  left: -5000%;
  right: -5000%;
  bottom: -5000%;
  background-image: var(--b);
  background-size: 100% 100%;
  animation: animate 10s infinite linear;
}

@keyframes animate {
  from {
    transform: rotate(0);
  }
  to {
    transform: rotate(360deg);
  }
}
<!-- step will define the degree between each line -->

<div class="circle" data-color="red" data-step="10">
<span>some text</span>
</div>
<div class="circle" style="top:150px;left:150px;" data-color="green" data-step="20">
<span>text</span>
</div>

<div class="circle" style="left:250px;" data-color="orange" data-step="30">
<span>more here</span>
</div>

<div class="circle" style="right:50px;bottom:50px" data-color="cyan" data-step="10">
<span>more here</span>
</div>

相关问答

更多
  • 理想情况下,人们可以将起点从中心移到左上方,但在这里如何从中心开始。 与您的代码唯一真正的区别仅仅是我要求的开始长度,然后只问问(while循环)是否太长。 忽略我正在使用用于Python 2.x的raw_input ,这同样适用于不带圆括号的打印语句。 import turtle bob = turtle.Turtle() xx,yy= turtle.window_width(), turtle.window_height() print 'window size: ',xx,yy bob.spe ...
  • 那么,你可以轻松解决这个问题。 你要么做Bastian Rang在他的回答中所说的话,要么你可以继续将这张图片分成两张图片。 所以,登录信息将是一个单独的图像。 通过这种方式,您可以通过css或javascript来调整和调整大小。 并将该登录面板设置为相对位置,您可以轻松地将输入放置在内部并轻松移动。 如有必要,我可以解释更多。 您面临的问题是因为您已将输入区域和背景组合在一起的图像。 将它们分开,它会容易得多。 圣诞快乐,新年快乐! 本着奉献的精神,你去了: [更新] OP的登录设计。 你需要在谷歌浏览 ...
  • 你必须提供你自己的按钮。 对于play和pause ,AVPlayer中有直接的API。 对于全屏幕...呃...你知道你在哪里绘制:你只需要将你的UIWindow和相应的UIView&图层设置为全尺寸。 对于搜索,你有像seekToTime:这样的方法seekToTime: You'll have to provide your own buttons. For play and pause there are direct API's in AVPlayer. For full screen… well ...
  • 将其添加到页面顶部的样式标记中 body, div, svg { margin: 0; border: 0 none; padding: 0; } html, body, div, svg { height: 99%; max-width: 99%; min-height: 90%; } add this in your style tags at the top of the page body, div, svg { margin: 0; b ...
  • 如果我使用指向SVG的图像标签,并且使用以下CSS,它似乎对我(至少在Firefox中)有效: - #container img { width: 100%; max-height: 100%; } 容器是一个灵活的div,它限制了SVG的大小(这可能是你的表格单元格)。 SVG正在填充容器的宽度,比例保持在pro中,但是如果容器变得太宽,则最大高度会踢入并限制SVG的高度,而不会拉伸。 It seems to work for me (at least in Firefox) if I ...
  • 你必须反转(水平翻转)两个路径定义。 可能不明显的是它们使用小写m ,因此每个坐标对都相对于最后一个坐标对。 例如,原始的起始路径是: m 0,0 1440,0 0,806 0,-806 z 这些点是左上角,右上角,右下角,右上角(按此顺序)。 它动画为: m 0,0 1440,0 0,806 -1440,0 z 或者:左上角,右上角,右下角,左下角。 换句话说,最后一点从右上角移动到左下角。 要改为从左上角到右下角,我们必须颠倒该形状中坐标的顺序。 所以来自: m 1440,0 -1440,0 0,8 ...
  • 根据Apple的文档,您只需将`MPMoviePlayerController设置为全屏: [moviePlayer setFullscreen:YES animated:YES] 你试过这个吗? As per Apple's documentation, you just have to set the `MPMoviePlayerController to go full screen: [moviePlayer setFullscreen:YES animated:YES] Have you tr ...
  • 在AndroidManifest.xml中设置主题,让您的应用全屏显示。 您的ImageView将填充父级或匹配父级和根视图以匹配整个屏幕。 希望这有助于并享受您的工作 Set your theme in AndroidManifest.xml for your app to be full screen. Your ImageView to be fill parent or match parent and your root view to match entire screen. Hope this ...
  • 您应该将.second-content设置为相对的。然后将{position: absolute}添加到其他两个元素。 然后到第一个添加{top: 0}并添加到另一个{bottom: 0} body,html,.second-content {height: 100%; margin: 0} .second-content { background-color: #cccccc; position: relative } .second-content svg:first-child, ...
  • 我会认为圆是主要元素,我会使用一个伪元素,我用旋转的SVG背景填充所有这些线。 body { margin::0; overflow:hidden; } .circle { height:100px; width:100px; margin:50px; background:red; border-radius:50%; position:relative; } .circle:before { content:""; positi ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)