明智地使用重载(Use overloading judiciously)
除了标准的TreeSet之外,TreeSet的构造函数还包括一个允许您提供Comparator的构造函数,以及一个允许您从另一个SortedSet创建一个的构造函数:
TreeSet(Comparator<? super E> c) // construct an empty set which will be sorted using the // specified comparator TreeSet(SortedSet<E> s) // construct a new set containing the elements of the // supplied set, sorted according to the same ordering
其中第二个与标准“转换构造函数”的声明相当接近:
TreeSet(Collection<? extends E> c)
正如Joshua Bloch在Effective Java中解释的那样(在“方法”一章中“明智地使用重载”项),调用两个带有相关类型参数的构造函数或方法重载之一会产生令人困惑的结果。 这是因为,在Java中,基于参数的静态类型在编译时解析对重载的构造函数和方法的调用,因此对参数应用强制转换可以对调用的结果产生很大的影响,因为以下代码显示:
// construct and populate a NavigableSet whose iterator returns its // elements in the reverse of natural order: NavigableSet<String> base = new TreeSet<String>(Collections.reverseOrder()); Collections.addAll(base, "b", "a", "c"); // call the two different constructors for TreeSet, supplying the // set just constructed, but with different static types: NavigableSet<String> sortedSet1 = new TreeSet<String>((Set<String>)base); NavigableSet<String> sortedSet2 = new TreeSet<String>(base); // and the two sets have different iteration orders: List<String> forward = new ArrayList<String>(); forward.addAll(sortedSet1); List<String> backward = new ArrayList<String>(); backward.addAll(sortedSet2); assert !forward.equals(backward); Collections.reverse(forward); assert forward.equals(backward);
此问题折磨了Framework中所有已排序集合的构造函数(TreeSet,TreeMap,ConcurrentSkipListSet和ConcurrentSkipListMap)。 要在您自己的类设计中避免它,请为不同的重载选择参数类型,以便不能将适合于一个重载的类型的参数强制转换为适合不同类型的类型。 如果这是不可能的,那么两个重载应设计成与相同的参数相同,无论其静态类型如何。 例如,从集合构造的PriorityQueue使用原始的排序,无论提供构造函数的静态类型是包含Comparator的类型PriorityQueue还是SortedSet之一,或者只是普通的Collection。 为实现此目的,转换构造函数使用所提供集合的Comparator,如果没有,则只返回自然顺序。
目前,我正在阅读一本名为Java Generics and Collections的书,以上是我不理解的内容[page185~186]。
首先,我不太明白为什么它使用这个例子以及它想要说明的内容。
其次,我不太了解“转换构造函数”的概念。 是因为转换构造函数的存在,我们应该明智地使用重载吗?
The constructors for TreeSet include, besides the standard ones, one which allows you to supply a Comparator and one which allows you to create one from another SortedSet:
TreeSet(Comparator<? super E> c) // construct an empty set which will be sorted using the // specified comparator TreeSet(SortedSet<E> s) // construct a new set containing the elements of the // supplied set, sorted according to the same ordering
The second of these is rather too close in its declaration to the standard "conversion constructor” :
TreeSet(Collection<? extends E> c)
As Joshua Bloch explains in Effective Java (item “Use overloading judiciously” in the chapter on Methods), calling one of two constructor or method overloads which take parameters of related type can give confusing results. This is because, in Java, calls to overloaded constructors and methods are resolved at compile time on the basis of the static type of the argument, so applying a cast to an argument can make a big difference to the result of the call, as the following code shows:
// construct and populate a NavigableSet whose iterator returns its // elements in the reverse of natural order: NavigableSet<String> base = new TreeSet<String>(Collections.reverseOrder()); Collections.addAll(base, "b", "a", "c"); // call the two different constructors for TreeSet, supplying the // set just constructed, but with different static types: NavigableSet<String> sortedSet1 = new TreeSet<String>((Set<String>)base); NavigableSet<String> sortedSet2 = new TreeSet<String>(base); // and the two sets have different iteration orders: List<String> forward = new ArrayList<String>(); forward.addAll(sortedSet1); List<String> backward = new ArrayList<String>(); backward.addAll(sortedSet2); assert !forward.equals(backward); Collections.reverse(forward); assert forward.equals(backward);
This problem afflicts the constructors for all the sorted collections in the Framework (TreeSet, TreeMap, ConcurrentSkipListSet, and ConcurrentSkipListMap). To avoid it in your own class designs, choose parameter types for different overloads so that an argument of a type appropriate to one overload cannot be cast to the type appropriate to a different one. If that is not possible, the two overloads should be designed to behave identically with the same argument, regardless of its static type. For example, a PriorityQueue constructed from a collection uses the ordering of the original, whether the static type with which the constructor is supplied is one of the Comparator-containing types PriorityQueue or SortedSet, or just a plain Collection. To achieve this, the conversion constructor uses the Comparator of the supplied collection, only falling back on natural ordering if it does not have one.
Currently, I am reading the book called Java Generics and Collections, and above is something I don't understand [page185~186].
First, I don't quite understand why it uses this example and what things it want to illustrate.
Second, I don't quite understand the concept of "conversion constructor". Is it because of the existence of conversion constructor, that we should use overloading judiciously?
原文:https://stackoverflow.com/questions/31712397
最满意答案
我建议在输入标记中添加
id
,如下所示<input size="15" id="user_name_135" name="user[135][name]" value="" disabled=disabled>
使用简单的Javascript
if(some_condition) { document.getElementById("user_name_135").disabled=false; }
使用jQuery
if(some_condition){ $("#user_name_135").prop('disabled', false);}
如果只有一个语句,
if
你可以省略{}
if(some_condition) document.getElementById("user_name_135").disabled=false;
编辑请将以下代码保存为html文件,看它应该适合你
<!doctype html> <html> <head> <script> function some(){ document.getElementById("user_name_135").disabled=false; } </script> </head> <body> <input size="15" id="user_name_135" name="user[135][name]" value="" disabled=disabled> <a href ="javascript:void(0)" onclick="some()">disbale input</a> </body> </html>
I'll suggest add
id
to your input tag as follows<input size="15" id="user_name_135" name="user[135][name]" value="" disabled=disabled>
Using simple Javascript
if(some_condition) { document.getElementById("user_name_135").disabled=false; }
Using jQuery
if(some_condition){ $("#user_name_135").prop('disabled', false);}
Also if there is only one statement followed by
if
you can omit{}
if(some_condition) document.getElementById("user_name_135").disabled=false;
Edited Please save following code as a html file and see it should work for you
<!doctype html> <html> <head> <script> function some(){ document.getElementById("user_name_135").disabled=false; } </script> </head> <body> <input size="15" id="user_name_135" name="user[135][name]" value="" disabled=disabled> <a href ="javascript:void(0)" onclick="some()">disbale input</a> </body> </html>
相关问答
更多-
方括号表示新阵列。 var ar=new Array("a","b"); var ar=["a","b"]; //Equal to the syntax above 在这种情况下,如果使用方括号,则不会有区别,因为如果它是数组,它将转换为字符串,但如果删除括号,则花费的时间会更少,因为它不必构建新数组并将其转换,但它使用简单的字符串。 Square brackets means new Array. var ar=new Array("a","b"); var ar=["a","b"]; //Equal ...
-
如果vendorcode为false则此代码将{}分配给vendorcode 。 意思是它是undefined , false , 0 , null等。 如果供应商vendorcode 不是 false它将保持其价值。 你可以大声朗读:“ vendorcode equals vendorcode OR {} ” This code assigns {} to vendorcode if vendorcode is false-y. Meaning it's undefined, false, 0, null ...
-
获取括号内的文字(Get text between brackets [duplicate])[2022-02-17]
尝试 var myString= "Charles de Gaulle, (Paris) [CDG]"; var result = myString.match(/\((.*)\)/); alert(result[1]); DEMO Try var myString= "Charles de Gaulle, (Paris) [CDG]"; var result = myString.match(/\((.*)\)/); alert(result[1]); DEMO -
我建议在输入标记中添加id ,如下所示 使用简单的Javascript if(some_condition) { document.getElementById("user_name_135").disabled=false; } 使用jQuery if(some_condition){ $("#user_name_135").pr ...
-
语法 - 变量声明周围的方括号是什么意思[重复](Syntax - what does square brackets around a variable declaration mean [duplicate])[2023-06-23]
它被称为解构赋值 ,它用于解包array的值并将它们分配给新变量。 所以在你的代码中: const [component] = router.getMatchedComponents({ ...to }) 您正在声明一个component变量,该变量将保存将从router.getMatchedComponents({...to})返回的数组中的对象hold,其中to是使用spread操作转换为array的数组结构。 It's called Destructuring assignment, and it' ... -
您需要在js文件的顶部提供jslint选项,例如: /*global window, jQuery, require, charts */ 可能还有一种方法可以在全球范围内进行此操作,我还没有研究过它(实际上并不需要它) You need to supply jslint options at the top of the js file, for example: /*global window, jQuery, require, charts */ There might also be a way ...
-
有了views: model['views'], 您声明视图等于model元素的views属性。 与其他人一样 With views: model['views'], You declare views equal to the views attribute of the model element. And so on with the others
-
如何使用Javascript RegExp来转义括号?(How can I use Javascript RegExp to escape brackets? [duplicate])[2023-01-25]
使用非贪婪量词: var str = "hjdsoghbesi (saeogbesor) serogberso ersberawsewf(edsrgb) sfdrobersb ouersber"; console.log(str.replace(/\(.*?\)/g, "")); *贪婪,它将匹配尽可能多的字符。 *? 很懒,它会尽可能少地匹配。 Use non-greedy quantifiers: var str = "hjdsoghbesi (saeogbesor) serogberso ersbe ... -
c被用作对象的属性访问器。 该函数用作静态映射器,以检索颜色字的相应值 function segColor(c) { return { red: "#FF0000", green: "#00ff00", blue: "#0000ff" }[c]; } console.log(segColor('green')); c is being used as a property accessor to the object. The ...
-
从javascript中的括号之间的多个数组循环[重复](Loop from multiple arrays between brackets in javascript [duplicate])[2023-01-11]
尝试这个 var secondvar = '403' var thirdvar = '3' var fourthvar = 'name' myvar[secondvar][thirdvar][fourthvar] Try this var secondvar = '403' var thirdvar = '3' var fourthvar = 'name' myvar[secondvar][thirdvar][fourthvar]