ÉîÈë½âÎöJavaµÄÐÂÌØÐÔassertion
ÎÒÃǾ³£ÏÂÔØ¿ªÔ´ÏîÄ¿µÄÔ´ÂëÔÚ±¾µØ±àÒë,ÊDz»ÊǾ³£±»Ò»¾ä"assert expression"¸ø¸ãµÃ±àÒë²»¹ýÈ¥£¬¾³£ÊÇÔÚÀÏ°æ±¾µÄjdk±àÒëÆ÷ÏÂû°ì·¨£¬ÏÂÃæ¾ÍÏò´ó¼ÒÍƼöһƪÉîÈë·ÖÎöjava assertionµÄÎÄÕ¡£
Ò»¡¢assertionµÄÓï·¨ºÍÓïÒå
J2SE 1.4ÔÚÓïÑÔÉÏÌṩÁËÒ»¸öÐÂÌØÐÔ£¬¾ÍÊÇassertion(¶ÏÑÔ)¹¦ÄÜ£¬ËüÊǸð汾ÔÚJavaÓïÑÔ·½Ãæ×î´óµÄ¸ïС£ÔÚÈí¼þ¿ª·¢ÖУ¬assertionÊÇÒ»ÖÖ¾µäµÄµ÷ÊÔ¡¢²âÊÔ·½Ê½£¬±¾ÎĽ«ÉîÈë½âÎöassertion¹¦ÄܵÄʹÓÃÒÔ¼°ÆäÉè¼ÆÀíÄ²¢¸ø³öÏà¹ØµÄÀý×Ó¡£
assertion(¶ÏÑÔ)ÔÚÈí¼þ¿ª·¢ÖÐÊÇÒ»ÖÖ³£Óõĵ÷ÊÔ·½Ê½£¬ºÜ¶à¿ª·¢ÓïÑÔÖж¼Ö§³ÖÕâÖÖ»úÖÆ£¬ÈçC£¬C++ºÍEiffelµÈ£¬µ«ÊÇÖ§³ÖµÄÐÎʽ²»¾¡Ïàͬ£¬ÓеÄÊÇͨ¹ýÓïÑÔ±¾Éí¡¢ÓеÄÊÇͨ¹ý¿âº¯ÊýµÈ¡£ÁíÍ⣬´ÓÀíÂÛÉÏÀ´Ëµ£¬Í¨¹ýassertion·½Ê½¿ÉÒÔÖ¤Ã÷³ÌÐòµÄÕýÈ·ÐÔ£¬µ«ÊÇÕâÊÇÒ»ÏîÏ൱¸´ÔӵŤ×÷£¬Ä¿Ç°»¹Ã»ÓÐÌ«¶àµÄʵ¼ùÒâÒå¡£
ÔÚʵÏÖÖУ¬assertion¾ÍÊÇÔÚ³ÌÐòÖеÄÒ»ÌõÓï¾ä£¬Ëü¶ÔÒ»¸öboolean±í´ïʽ½øÐмì²é£¬Ò»¸öÕýÈ·³ÌÐò±ØÐë±£Ö¤Õâ¸öboolean±í´ïʽµÄֵΪtrue£»Èç¹û¸ÃֵΪfalse£¬ËµÃ÷³ÌÐòÒѾ´¦ÓÚ²»ÕýÈ·µÄ״̬Ï£¬ÏµÍ³½«¸ø³ö¾¯¸æ»òÍ˳ö¡£
Ò»°ãÀ´Ëµ£¬assertionÓÃÓÚ±£Ö¤³ÌÐò×î»ù±¾¡¢¹Ø¼üµÄÕýÈ·ÐÔ¡£assertion¼ì²éͨ³£ÔÚ¿ª·¢ºÍ²âÊÔʱ¿ªÆô¡£ÎªÁËÌá¸ßÐÔÄÜ£¬ÔÚÈí¼þ·¢²¼ºó£¬assertion¼ì²éͨ³£Êǹرյġ£ÏÂÃæ¼òµ¥½éÉÜÒ»ÏÂJavaÖÐassertionµÄʵÏÖ¡£
1¡¢Óï·¨±íʾ
ÔÚÓï·¨ÉÏ£¬ÎªÁËÖ§³Öassertion£¬JavaÔö¼ÓÁËÒ»¸ö¹Ø¼ü×Öassert¡£Ëü°üÀ¨Á½ÖÖ±í´ïʽ£¬·Ö±ðÈçÏ£º
assert expression1;
assert expression1: expression2;
ÔÚÁ½ÖÖ±í´ïʽÖУ¬expression1±íʾһ¸öboolean±í´ïʽ£¬expression2±íʾһ¸ö»ù±¾ÀàÐÍ»òÕßÊÇÒ»¸ö¶ÔÏó(Object)£¬»ù±¾ÀàÐÍ°üÀ¨boolean,char,double,float,intºÍlong¡£ÓÉÓÚËùÓÐÀ඼ΪObjectµÄ×ÓÀ࣬Òò´ËÕâ¸ö²ÎÊý¿ÉÒÔÓÃÓÚËùÓжÔÏó¡£
2¡¢ÓïÒ庬Òå
ÔÚÔËÐÐʱ£¬Èç¹û¹Ø±ÕÁËassertion¹¦ÄÜ£¬ÕâЩÓï¾ä½«²»ÆðÈκÎ×÷Óá£Èç¹û´ò¿ªÁËassertion¹¦ÄÜ£¬ÄÇôexpression1µÄÖµ½«±»¼ÆË㣬Èç¹ûËüµÄֵΪfalse£¬¸ÃÓï¾äÇ¿Å׳öÒ»¸öAssertionError¶ÔÏó¡£
Èç¹ûassertionÓï¾ä°üÀ¨expression2²ÎÊý£¬³ÌÐò½«¼ÆËã³öexpression2µÄ½á¹û£¬È»ºó½«Õâ¸ö½á¹û×÷ΪAssertionErrorµÄ¹¹Ô캯ÊýµÄ²ÎÊý£¬À´´´½¨AssertionError¶ÔÏ󣬲¢Å׳ö¸Ã¶ÔÏó£»Èç¹ûexpression1ֵΪtrue£¬expression2½«²»±»¼ÆËã¡£
Ò»ÖÖÌØÊâÇé¿öÊÇ£¬Èç¹ûÔÚ¼ÆËã±í´ïʽʱ£¬±í´ïʽ±¾ÉíÅ׳öException£¬ÄÇôassert½«Í£Ö¹ÔËÐУ¬¶øÅ׳öÕâ¸öException¡£
3¡¢Ò»Ð©assertionÀý×Ó
ÏÂÃæÊ
Ïà¹ØÎĵµ£º
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Test {
/**
* @×÷ÕßQQ£º351828554 2009/12/01 ÐÇÆÚ¶þ
version:1.0
*/
public static void main(String[] args) {
new encry ......
Person p=new Person();
ÕâÊÇʲô?µ±È»ÊÇʵÀý»¯Ò»¸ö¶ÔÏóÁË.¿ÉÊÇÕâÖÖʵÀý»¯¶ÔÏóµÄ·½·¨´æÔÚÒ»¸öÎÊÌâ,ÄǾÍÊDZØÐëÒªÖªµÀÀàÃû²Å¿ÉÒÔʵÀý»¯ËüµÄ¶ÔÏó,ÕâÑùÎÒÃÇÔÚÓ¦Ó÷½Ãæ¾Í»áÊܵ½ÏÞÖÆ.ÄÇôÓÐûÓÐÕâÑùÒ»ÖÖ·½Ê½,ÈÃÎÒÃDz»ÖªµÀÕâ¸öÀàµÄÀàÃû¾Í¿ÉÒÔʵÀý»¯ËüµÄ¶ÔÏóÄØ?Thank Goodness!ÐÒ¿÷ÎÒÃÇÓõÄÊÇjava, java¾ÍÌṩÁËÕâÑùµÄ»úÖÆ.
1).ja ......
±êÌâ ÔÚJavaÖÐʵÏÖ¸¡µãÊýµÄ¾«È·¼ÆËã AYellow£¨Ô×÷£© ÐÞ¸Ä
¹Ø¼ü×Ö Java ¸¡µãÊý ¾«È·¼ÆËã
ÎÊÌâµÄÌá³ö£º
Èç¹ûÎÒÃDZàÒëÔËÐÐÏÂÃæÕâ¸ö³ÌÐò»á¿´µ½Ê²Ã´£¿
public class Test{
public static void mai ......
JDK1.4ÖÐ
Map map = new HashMap();
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
}
JDK1.5ÖÐ,Ó¦ÓÃÐÂÌØÐÔFor-EachÑ»·
Map m = new HashMap(); ......
2. Calendar ¶ÔÏóµÄÈÝ´íÐÔ£¬Lenient ÉèÖà ÎÒÃÇÖªµÀÌض¨µÄÔ·ÝÓв»Í¬µÄÈÕÆÚ£¬µ±Ò»¸öÓû§¸ø³ö´íÎóµÄÈÕÆÚʱ£¬Calendar ÈçºÎ´¦ÀíµÄÄØ£¿ import java.io.*;
import java.util.*; public class WhatIsCalendar
{
public static void main(String[] args) throws Exception{
......