我想使对象的结构不可变,防止其属性随后被替换.但是,属性需要是可读的.这可能吗?
我确信没有语言功能(final在Java和readonlyC#中都有)来支持这一点,但是想知道是否可能有另一种机制来实现相同的结果?
我正在寻找这些方面的东西:
var o = {
a: "a",
f: function () {
return "b";
}
};
var p = o.a; // OK
o.a = "b"; // Error
var q = o.f(); // OK
o.f = function () { // Error
return "c";
};
Run Code Online (Sandbox Code Playgroud) 我们的应用程序使用初始化代码,这取决于执行静态代码的顺序,我想知道这个顺序是否在所有JVM中都是一致的.
这是我的意思的样本:
public class Main {
static String staticVar = "init_value";
public static void main(String[] args) {
System.out.println(A.staticVar);
staticVar = "mainValue";
System.out.println(A.staticVar);
}
}
public class A {
static String staticVar = Main.staticVar;
}
Run Code Online (Sandbox Code Playgroud)
会给:
init_value init_value
和
public class Main {
static String staticVar = "init_value";
public static void main(String[] args) {
// System.out.println(A.staticVar);
staticVar = "mainValue";
System.out.println(A.staticVar);
}
}
public class A {
static String staticVar = Main.staticVar;
}
Run Code Online (Sandbox Code Playgroud)
会给(在我的环境):
mainValue
总而言之,在所有JVM中,当我们第一次使用类时,是否总是执行静态代码?
我在样式指南中没有看到任何关于此的内容。此外,浏览 Dart SDK 代码和一些 pub 包,我没有看到任何参数被声明为 final。事实上,为了深入了解,我还没有在任何地方看到这样做过。(这可能只是反映了我缺乏经验!)
做出这个声明显然是可以的,Dart 编辑器确实使用该注释将重新分配标记为错误。
SO .. 将 final 与 params 一起使用是不是一件好事,它不是惯用的 Dart,它实际上是一件好事,还是没有开发的习惯?
我试图通过执行程序和runnable并行执行100个任务,任务需要使用循环变量:
for (int i = 0; i < 100; i++) {
executor.execute(() -> {
doSomething(String.format("Need task number %d done", i));
}
});
}
Run Code Online (Sandbox Code Playgroud)
在'我'说的时候,我得到一个波浪形的 - Variable used in lambda expression should be effectively final.
据我所知,循环变量不能是最终的或有效的最终变量,因为它随着每次迭代而被更改.我发现了一个简单的解决方法,
for (int i = 0; i < 100; i++) {
int index = i;
executor.execute(() -> {
doSomething(String.format("Need task number %d done", index));
}
});
}
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎不是最有效的解决方案,在每次迭代时声明一个新变量.有没有更好的方法呢?
考虑这个类层次结构:
struct Animal { virtual ~Animal(); };
struct Cat : virtual Animal {};
struct Dog final : virtual Animal {};
Run Code Online (Sandbox Code Playgroud)
我的理解是,将final在class Dog确保没有人能够创建一个类继承Dog,其中,推论,意味着没有人能够创建IS-A类Dog和IS-A Cat同时进行.
考虑这两个dynamic_cast:
Dog *to_final(Cat *c) {
return dynamic_cast<Dog*>(c);
}
Cat *from_final(Dog *d) {
return dynamic_cast<Cat*>(d);
}
Run Code Online (Sandbox Code Playgroud)
GCC,ICC和MSVC忽略final限定符并生成调用__dynamic_cast; 这是不幸的,但并不令人惊讶.
让我感到惊讶的是,Clang和Zapcc都为("总是返回nullptr")生成了最佳代码from_final,但是生成了对__dynamic_castfor 的调用to_final.
这真的是一个错过的优化机会(在一个编译器中显然有人投入一些努力来尊重final演员阵容中的限定符),或者在这种情况下,由于某些微妙的原因,我仍然没有看到优化是不可能的?
可能重复:
Javascript中是否有常量?
有没有办法在javascript中声明最终值的静态,以便它不能被第三方更改?
我所拥有的是文章共享应用程序,免费用户受到广告支持.我想做的是通过改变内容的存储变量来防止免费用户改变innerHTML内容.
我现在拥有的是一个计时器,它每5秒在用户网站上重新加载文章的innerHTML,并且我将重载的值存储在变量中.
但是,如果使用jsbeatify的天才探索哪个变量是删除广告的关键并改变了这一点,那么我们就会失去收入和产品的曝光率.
如何防止内部变量的改变?
这是我想出的最终结果:
<div id="specialdiv"></div>
<input type="button" value="try to change the function i to do something different" onclick="t.i = function(){alert(data.secret);}"><BR>
<input type="button" value="set function to null out of spite" onclick="t=null;">
Run Code Online (Sandbox Code Playgroud)
<script type="text/javascript">
e = function(){var data = { };
Object.defineProperty(data, 'secret', {
value: "Hello world!",
writable : false,
enumerable : true,
configurable : false
});this.z=function(){window.setTimeout("try{document.getElementById('specialdiv').innerHTML = '"+data.secret+"';t.i.z();}catch(err) {document.body.innerHTML=err;}",5000);}}
g = function(){};
g.prototype = new e();e=null;window.t = {}
Object.defineProperty(window.t, 'i', {
value: new g(), …Run Code Online (Sandbox Code Playgroud) 这段代码:
public class Sandbox {
public enum E {
VALUE {
@Override
public String toString() {
return "I'm the value";
}
};
@Override
public String toString() {
return "I'm the enum";
}
}
public static void main(String[] args) {
System.out.println(E.VALUE);
}
}
Run Code Online (Sandbox Code Playgroud)
打印:
我很有价值
但是,这段代码:
public class Sandbox {
public static final class C {
@Override
public String toString() {
return "I'm a C";
}
}
public static void main(String[] args) {
System.out.println(new C() {
@Override
public String toString() { …Run Code Online (Sandbox Code Playgroud) 我知道final类(你不能继承类)和私有构造函数(你不能创建类的实例)之间的区别.但是为什么Arrays和MathJava类都有私有构造函数但是Math是最终类而Arrays不是?
有什么不同?不是两个实用类?
谢谢
final ×10
java ×6
javascript ×2
static ×2
c++ ×1
constructor ×1
dart ×1
dynamic-cast ×1
enums ×1
immutability ×1
jvm ×1
lambda ×1
parameters ×1
polymorphism ×1
private ×1