Assert 在Java和Groovy中的区别
对于防御性编程而言,assert一直都是非常重要的工具。但对于熟悉-ea参数的Java程序员而言,当他们看到所有Groovy断言无论是否有-ea都照样执行时一定会非常困惑。
事实上,Groovy的断言是无条件开启的!
对此,开发人员Roshan Dawrani做了如下解释:(原文)
The JDK documentation has a long chapter on assertions that talks about the disabling feature for assertions and its impact on compiling, starting the VM, and resulting design issues. Although this is fine and the design rationale behind Java assertions is clear, we feel the disabling feature is the
biggest stumbling block for using assertions in Java. You can never be sure that your assertions are really executed.
在Core Java(或者是TIJ?记不清了)里,作者提到assertion类似“在海边开赛艇要带好你的卡通救生圈;如果船已经在大海中央,就把救生圈扔掉好了”。不过,在Groovy中,显然这种防御措施会一直跟随你到大海的任意角落(希望不是海底)。
PS:如果仔细看看Groovy断言的输出,你一定会意识到它对于debug是多么有用:
1: def xml = new XmlParser().parseText("x")
2: assert "y" == xml.text()
3: -----
4: output:
5:
6: assert "y" == xml.text()
7: | | |
8: | | x
9: | test[attributes={}; value=[x]]
10: false
11:
******
Quote of the day:
I didn't really say everything I said. - Yogi Berra
相关文档:
先来了解一下链表模式的原理:
首先写一个JavaBean,内容是要添加的元素和该元素的节点。
public class NodeBean implements Serializable
{
private Object data; //元素本身
private NodeBean next; //下一个节点
&n ......
一、抽象类注意事项:
1,抽象类中可以用0+个抽象方法。
2,有抽象方法的的类必须是抽象类。
3,抽象类派生的非抽象子类必须实现抽象类中定义的所有抽象方法。
4,抽象类不能创建对象。
5,abstract 不能与final并列修饰同一个类
6,abstract不能与private,static ,final,native并列修饰同一方法。
package com.wens ......
将某 class 产生出一个 instance 之后,此 class 所有的 instance field 都会新增一份,那么所有的 instance method 是否也会新增一份?答案是不会,我们用field表示字段,用method表示方法,那么加上static区分后就 有四种:
class field:有用static修饰的field
class method:有用static修饰的method
instance fi ......
如果你用的是myeclipse 6.0 而且 在使用
@Override 时出现以下错误:
The method XXXXXX of type XXXXXXXXX must
override a superclass method
那是因为你的Compiler 是jdk1.5,只要把它改为 1.6就可以了
方法:
1. window ->preferences... -> java -> Compiler
2. Compiler compliance lev ......