µÚ1Ìõ How to Write an Equality Method in Java
http://www.artima.com/lejava/articles/equality.html
How to Write an Equality Method in Java
by Martin Odersky, Lex Spoon, and Bill Venners
June 1, 2009
Summary
This article describes a technique for overriding the equals method that preserves the contract of equals even when subclassses of concrete classes add new fields.
In Item 8 of Effective Java1, Josh Bloch describes the difficulty of preserving the equals contract when subclassing as a “fundamental problem of equivalence relations in object-oriented languages.” Bloch writes:
There is no way to extend an instantiable class and add a value component while preserving the equals contract, unless you are willing to forgo the benefits of object-oriented abstraction.
Chapter 28 of Programming in Scala shows an approach that allows subclasses to extend an instantiable class, add a value component, and nevertheless preserve the equals contract. Althou ......
µÚ1Ìõ How to Write an Equality Method in Java
http://www.artima.com/lejava/articles/equality.html
How to Write an Equality Method in Java
by Martin Odersky, Lex Spoon, and Bill Venners
June 1, 2009
Summary
This article describes a technique for overriding the equals method that preserves the contract of equals even when subclassses of concrete classes add new fields.
In Item 8 of Effective Java1, Josh Bloch describes the difficulty of preserving the equals contract when subclassing as a “fundamental problem of equivalence relations in object-oriented languages.” Bloch writes:
There is no way to extend an instantiable class and add a value component while preserving the equals contract, unless you are willing to forgo the benefits of object-oriented abstraction.
Chapter 28 of Programming in Scala shows an approach that allows subclasses to extend an instantiable class, add a value component, and nevertheless preserve the equals contract. Althou ......
ÕâÁ½¸öÐÇÆÚ³ýÁËרҵ¿Î·½ÃæµÄѧϰ£¬Ò»Ö±ÔÚѧϰJSP£¬ÓõÄÊÇ¡¶21ÌìѧͨJAVA WEB¿ª·¢¡·Õâ±¾»ù´¡ÈëÃŽ̲ģ¬Ê鱾ɿ¼°µ½µÄ¶«Î÷²¢²»¶à£¬Ñ§µÄ»¹ÊÇһЩ±È½ÏdzÏԵĶ«Î÷°É¡£Ò²ÐíºÜ¶à¶«Î÷ÕæµÄÒª½Ó´¥ÁËÖ®ºó²Å»áÓиоõ£¬×÷Ϊ¸Õ¸Õ½Ó´¥web¿ª·¢µÄ²ËÄñ¶øÑÔ£¬Äܹ»½«Ð´µÄ¶«Î÷ÔËÐÐÆðÀ´ÕæµÄºÜÓгɾ͸У¬(*^__^*) ÎûÎû……
»ØÏëÕâÁ½¸öÐÇÆÚµÄѧϰ£¬´ÓÅäÖû·¾³£¬Óüòµ¥µÄÈý²ãÉè¼ÆÐ´µÇ¼£¬Ñ§Ï°struts2£¬µ½ÏÖÔÚ³õ²½Á˽âhibernate£¬×î´óµÄ¸Ð¾õÊÇ×Ô¼ºÒªÑ§µÄ¶«Î÷»¹ºÜ¶à£¬Ö»ÒªÕæÕýÓÃÐÄÈ¥×öÁ˿϶¨Äܹ»¶à¶àÉÙÉÙÈÃ×Ô¼º³É³¤ÆðÀ´¡£ÎÒÐèҪһЩѹÁ¦À´Ñ¸ËÙµÄÌá¸ß×Ô¼º¡£ ......
Õ⼸Ìì²Î¿¼ÓйØ×ÊÁÏ£¬½èÖúinstall4jÈí¼þ×ÜËã³õ²½Ñ§»áÁËJava³ÌÐò£¨jar£©´ò°ü³É¿É°²×°µÄWindowsÓ¦ÓóÌÐòµÄ·½·¨¡£
Ïà¹ØÈí¼þÔÚ£ºhttp://download.csdn.net/¿ÉÒÔËÑË÷µ½
ÒÔÏÂÊǹý³Ì¼Ç¼£º
1.
ͨ¹ý¸÷ÖÖ·½Ê½Éú³ÉÒ»¸ö¿ÉÖ´ÐеÄjarÎļþ£¨Çë²Î¿¼£ºhttp://blog.sina.com.cn/s/blog_65bb33240100ii4r.html£©
2.
Éú³ÉÒ»¸ö×Ô¼ºµÄjreÀ¦°óÎļþºó׺ÃûΪtar.gz
3.
ͨ¹ýinstall4j½øÐÐJava³ÌÐòµÄ´ò°ü´¦Àí
½ÓÏÂÀ´Öصã¼Ç¼µÚ¶þ²½ºÍµÚÈý²½
¸½Í¼¹©²Î¿¼
http://download.csdn.net/source/2304884
http://download.csdn.net/source/2303298
µÚ¶þ²½£º
ÔËÐÐinstall4j£¬Ñ¡ÔñÏîÄ¿²Ëµ¥£¨Project£©ÏµÄÉú³ÉÒ»¸öJRE°ó¶¨Îļþ£¨Create a JRE Bundle£©£¬È»ºóµ¯³öÉú³ÉÏòµ¼¶Ô»°¿òµã»÷Next½øÈëÑ¡ÔñJREµÄ¶Ô»°¿ò£¬Ñ¡Ôñä¯ÀÀ°´Å¥£¨µÚÒ»ÅÅ×îÓұߣ©µ¯³ö
Ñ¡ÔñÎļþ¼Ð¶Ô»°¿ò£¬Ñ¡ÔñÄúÒªÉú³ÉÀ¦°óÎļþ£¨¼´tar.gzÎļþ£©µÄĿ¼£¬µã»÷Open°´Å¥
ÊäÈëÄúµÄjre°æ±¾Èç1.5
ÊäÈëÀ¦°óÎļþÃû£¬Ëæ±ãÊäÈëÒ»¸ö£¬×îºÃÊÇÓ¢ÎÄ×Ö·ûµÄ£¨ÖÐÎĵϰ»ØÏÔΪ¡õ²»ºÃÇø·Ö£¬àË¡£Ã»°ì·¨Ã»ÕÒµ½Ö§³ÖÖÐÎĵģ©
µã»÷next¿´µ½Ã»ÓУ¬ÕýÔÚ°ïÄúÉú³ÉÀ¦°óÎļþÄØ£¬Èç¹ûÄúµÄJRE²»ÊǾ«¼ ......
Ò»¡¢ÏòÉÏתÐÍ¡£
ͨË׵ؽ²¼´Êǽ«×ÓÀà¶ÔÏóתΪ¸¸Àà¶ÔÏó¡£´Ë´¦¸¸Àà¶ÔÏó¿ÉÒÔÊǽӿڡ£
1£¬ÏòÉÏתÐÍÖеķ½·¨µ÷Óá£
¿´ÏÂÃæ´úÂ룺
package com.wensefu.others;
public class Animal {
public void eat(){
System.out.println("animal eatting...");
}
}
class Bird extends Animal{
public void eat(){
System.out.println("bird eatting...");
}
public void fly(){
System.out.println("bird flying...");
}
}
class Main{
public static void main(String[] args) {
Animal b=new Bird(); //ÏòÉÏתÐÍ
b.eat();
//! error: b.fly(); bËäÖ¸Ïò×ÓÀà¶ÔÏ󣬵«´Ëʱ¶ªÊ§fly()·½·¨
dosleep(new Male());
dosleep(new Female());
}
public static void dosleep(Human h) {
h.sleep();
}
}
package com.wensefu.others;
public class Human {
public void sleep() {
System.out.println("Human sleep..");
}
}
class Male extends Human {
@Override
public void sleep() {
System.out.println("Male sleep..");
}
}
class Fem ......
±äÁ¿£º´æ´¢Êý¾ÝµÄ»ù±¾µ¥Ôª£»Ò»¸öÊý¾Ý´æ´¢¿Õ¼äµÄ±íʾ.
»ù±¾Êý¾ÝÀàÐÍ(8ÖÖ)£º
byte(1)-->short(2)-->int(4)-->long(8) float(4)-->double(8) boolean(1) char(2)
ÒýÓÃÊý¾ÝÀàÐÍ£º
String¡¢Êý×é¡¢Àà¡¢½Ó¿Ú¡¢¶ÔÏó ......
ϵͳ×Ô¶¯Å׳öµÄÒì³£
ËùÓÐϵͳ¶¨ÒåµÄ±àÒëºÍÔËÐÐÒì³£¶¼¿ÉÒÔÓÉϵͳ×Ô¶¯Å׳ö£¬³ÆÎª±ê×¼Òì³££¬²¢ÇÒ Java Ç¿ÁÒµØÒªÇóÓ¦ÓóÌÐò½øÐÐÍêÕûµÄÒì³£´¦Àí£¬¸øÓû§ÓѺõÄÌáʾ£¬»òÕßÐÞÕýºóʹ³ÌÐò¼ÌÐøÖ´ÐС£
Óï¾äÅ׳öµÄÒì³£
Óû§³ÌÐò×Ô¶¨ÒåµÄÒì³£ºÍÓ¦ÓóÌÐòÌØ¶¨µÄÒì³£,±ØÐë½èÖúÓÚ throws ºÍ throw Óï¾äÀ´¶¨ÒåÅ׳öÒì³£¡£
throwÊÇÓï¾äÅ׳öÒ»¸öÒì³£¡£
Óï·¨£ºthrow (Òì³£¶ÔÏó);
throw e;
throwsÊÇ·½·¨¿ÉÄÜÅ׳öÒì³£µÄÉùÃ÷¡£(ÓÃÔÚÉùÃ÷·½·¨Ê±£¬±íʾ¸Ã·½·¨¿ÉÄÜÒªÅ׳öÒì³£)
Óï·¨£º[(ÐÞÊηû)](·µ»ØÖµÀàÐÍ)(·½·¨Ãû)([²ÎÊýÁбí])[throws(Òì³£Àà)]{......}
public void doA(int a) throws Exception1,Exception3{......}
¾ÙÀý£º
throws E1,E2,E3Ö»ÊǸæËß³ÌÐòÕâ¸ö·½·¨¿ÉÄÜ»áÅ׳öÕâЩÒì³££¬·½·¨µÄµ÷ÓÃÕß¿ÉÄÜÒª´¦ÀíÕâЩÒì³££¬¶øÕâЩÒì³£E1£¬E2£¬E3¿ÉÄÜÊǸú¯ÊýÌå²úÉúµÄ¡£
throwÔòÊÇÃ÷È·ÁËÕâ¸öµØ·½ÒªÅ׳öÕâ¸öÒì³£¡£
È磺 void doA(int a) throws Exception1,Exception3{
try{
&nb ......