java 板


Assertion 是新版 SCJP 考题增加的项目,这东西其实跟 Exception 有点相似 然而它是属於 java.lang.Error 的东西 我只是介绍他在程式码中的使用方法 而编译的使用方法可以先参照下面网址的简介 http://www.oreilly.com.tw/sleepless/shithappen2.htm 原始来源是来自於下面 sun 的文件 Programming With Assertions http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html 一、 assert的两种语法: assert Expression1; Expression1 必须是个 boolean Expression 当 Expression1 为 false 时,会丢出个 AssertionError assert Expression1 : Expression2; Expression1 是 boolean Expression Expression2 则是一个值 当 Expression1 为 false 时,会把 Expression2 的值传给 AssertionError 的建构子 二、 把 Assertion 放入程式码 这里分成三个 section ˙Internal Invariants ˙Control-Flow Invariants ˙Preconditions, Postconditions, and Class Invariants 但是先有两个 Assertion 不能使用的重点: 1.Do not use assertions for argument checking in public methods. 意思是请不要把 Assertion 跟 Exception 弄混了,两者做的事是不同的 2.Do not use assertions to do any work that your application requires for correct operation. ex: 这是错的: // Broken! - action is contained in assertion assert names.remove(null); 要改成: // Fixed - action precedes assertion boolean nullsRemoved = names.remove(null); assert nullsRemoved; // Runs whether or not asserts are enabled 原因似乎是 assertion 预设是 disable,要写的明确点他才能用... ˙Internal Invariants ☆use an assertion whenever you would have written a comment that asserts an invariant. ex1: 原式: if (i % 3 == 0) { ... } else if (i % 3 == 1) { ... } else { // We know (i % 3 == 2) ... } 後式: if (i % 3 == 0) { ... } else if (i % 3 == 1) { ... } else { assert i % 3 == 2 : i; ... } 意思是你原本注解为你已知的事实,可以用 assert 来确保不会错 ex2: 原式: switch(suit) { case Suit.CLUBS: ... break; case Suit.DIAMONDS: ... break; case Suit.HEARTS: ... break; case Suit.SPADES: ... } 你通常会在 switch 里加个 default,这里就可以用到 assertion default: assert false : suit; 或是另一种像是丢出 Exception 的语法 default: throw new AssertionError(suit); 这会强制丢出个 AssertionError ˙Control-Flow Invariants ☆place an assertion at any location you assume will not be reached. 语法: assert false; ex: 原式: void foo() { for (...) { if (...) return; } // Execution should never reach this point!!! } 後式: void foo() { for (...) { if (...) return; } assert false; // Execution should never reach this point! } 就是在你程式执行不到的地方加 assertion ˙Preconditions, Postconditions, and Class Invariants 这里分成: ˙Preconditions └˙Lock-Status Preconditions ˙Postconditions ˙Class invariants Preconditions — what must be true when a method is invoked. ☆Do not use assertions to check the parameters of a public method. ex: 原式: public void setRefreshRate(int rate) { // Enforce specified precondition in public method if (rate <= 0 || rate > MAX_REFRESH_RATE) throw new IllegalArgumentException("Illegal rate: " + rate); setRefreshInterval(1000/rate); } 後式: private void setRefreshInterval(int interval) { // Confirm adherence to precondition in nonpublic method assert interval > 0 && interval <= 1000/MAX_REFRESH_RATE : interval; ... // Set the refresh interval } 意思是这状况下 assertion 只能出现在 nonpublic 里 请记得这是个 Error,而不是 Exception Lock-Status Preconditions — preconditions concerning whether or not a given lock is held. ex: 原式: private Object[] a; public synchronized int find(Object key) { return find(key, a, 0, a.length); } // Recursive helper method - always called with a lock on this object private int find(Object key, Object[] arr, int start, int len) { ... } 後式: // Recursive helper method - always called with a lock on this. private int find(Object key, Object[] arr, int start, int len) { assert Thread.holdsLock(this); // lock-status assertion ... } 意思是可以用 assertion 来检查有没有 lock Postconditions — what must be true after a method completes successfully. 这种状况可以分别用在 public 与 nonpublic 里 public: ex: public BigInteger modInverse(BigInteger m) { if (m.signum <= 0) throw new ArithmeticException("Modulus not positive: " + m); ... // Do the computation assert this.multiply(result).mod(m).equals(ONE) : this; return result; } nonpublic: ex: 原式: void foo(int[] array) { // Manipulate array ... // At this point, array will contain exactly the ints that it did // prior to manipulation, in the same order. } 後式: void foo(final int[] array) { // Inner class that saves state and performs final consistency check class DataCopy { private int[] arrayCopy; DataCopy() { arrayCopy = (int[]) array.clone(); } boolean isConsistent() { return Arrays.equals(array, arrayCopy); } } DataCopy copy = null; // Always succeeds; has side effect of saving a copy of array assert ((copy = new DataCopy()) != null); ... // Manipulate array // Ensure array has same ints in same order as before manipulation. assert copy.isConsistent(); } 使用 inner class 跟这两个 assertion 可以用来确认你的资料 Class invariants — what must be true about each instance of a class. ex: // Returns true if this tree is properly balanced private boolean balanced() { ... } 在这 ex 中,加入 assert balanced(); 是为了确认这个 class 里的 instance 後记: 为了参加 SCJP,这个 Assertion 让我去找了些资料 但是市面上有许多标示 1.4 版的书,根本就没有提到 所以资料几乎都是英文的 像是 O'REILLY 原文新出的 Learning Java, 2nd Edition http://www.oreilly.com/catalog/learnjava2/ 里面就有介绍了,可惜天珑好像还没进货 @.@" 下面是几个我觉得不错的 assertion 介绍 http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html http://www.jcp.org/content/main/jsr/detail/materials/asrt_prop.html 第一个就是我这篇的来源 我只作了非常精简的介绍,想要了解更详细的话,就去看看原本的文章跟API吧 其实我之前对这 assertion 根本没听过... 想说没有 Exception 跟 Assertion 还是一样可以活的很快乐... 也不知道怎麽去测他就是... 希望能有前辈给我们更清楚的说明 也希望想参加新版 SCJP 考试的人能够来一起研究罗~ ^o^" -- 欢迎加入◢██◣◢██◣█◣ ◣◢██◣☆swanky ██ ██◤███ ███◤█ ██ ████◥█◢◤████ ◥█◤ ◥█◥◤ ◥◤ ◥█◥◤版的讨论 -- ※Post by swanky from u138-246.u61-70.giga.net ▁▃▅▆▄▃▁ ▃▅▂ ▁ ▁▃▄▁ ▃▅欢迎大家到市立师院 凯达格兰▆▅▄▇ _▁▃▄▅█▅▄▂▁_▄▃ ▇▅▄ ▄▅ ccbbs.tmtc.edu.tw ▂▃▆▇▆▁▂▄▆▆▇████▆▃







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草
伺服器连线错误,造成您的不便还请多多包涵!
「赞助商连结」






like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:BuyTogether站内搜寻

TOP