|
ÃæÏòJava¿ª·¢ÈËÔ±db4oÖ¸ÄÏ:Êý×éºÍ¼¯ºÏ |
|
![]() |
|
|
|
´¦Àí¶àÑùÐÔ¹Øϵ
ÊæÊʵļÒÍ¥Éú»î»áµ¼ÖÂÒ»¸ö»ò¸ü¶à ¡°Ð¡È˶ù¡± ½µÁÙµ½Õâ¸ö¼ÒÍ¥¡£µ«ÊÇ£¬ÔÚÔö¼ÓСº¢µ½¼ÒÍ¥ÖÐ֮ǰ£¬ÏÈÈ·±£ Person ÕæÕýÓеط½¿Éס¡£¸øËûÃÇÒ»¸ö¹¤×÷³¡Ëù£¬»òÕß»¹ÓÐÒ»¸öºÜºÃµÄÏÄÈնȼÙÎÝ¡£Ò»¸ö Address ÀàÐÍÓ¦¸Ã¿ÉÒÔ½â¾öËùÓÐÕâÈý¸öµØ·½¡£
Çåµ¥ 1. Ìí¼ÓÒ»¸ö Address ÀàÐ͵½ Person ÀàÖÐ
package com.tedneward.model;
public class Address { public Address() { }
public Address(String street, String city, String state, String zip) { this.street = street; this.city = city; this.state = state; this.zip = zip; }
public String toString() { return "[Address: " "street=" street " " "city=" city " " "state=" state " " "zip=" zip "]"; }
public int hashCode() { return street.hashCode() & city.hashCode() & state.hashCode() & zip.hashCode(); }
public boolean equals(Object obj) { if (obj == this) return this;
if (obj instanceof Address) { Address rhs = (Address)obj;
return && this.city.equals(rhs.city) && this.state.equals(rhs.state) && this.zip.equals(rhs.zip)); } else return false; }
public String getStreet() { return this.street; } public void setStreet(String value) { this.street = value; }
public String getCity() { return this.city; } public void setCity(String value) { this.city = value; }
public String getState() { return this.state; } public void setState(String value) { this.state = value; }
public String getZip() { return this.zip; } public void setZip(String value) { this.zip = value; }
private String street; private String city; private String state; private String zip; }
¿ÉÒÔ¿´µ½£¬Address Ö»ÊÇÒ»¸ö¼òµ¥µÄÊý¾Ý¶ÔÏó¡£½«ËüÌí¼Óµ½ Person ÀàÖÐÒâζ×Å Person ½«ÓÐÒ»¸öÃûΪ addresses µÄ Address Êý×é×÷Ϊ×ֶΡ£µÚÒ»¸öµØÖ·ÊǼÒͥסַ£¬µÚ¶þ¸öÊǹ¤×÷µØÖ·£¬µÚÈý¸ö£¨Èç¹û²»Îª null µÄ»°£©ÊǶȼÙÎݵØÖ·¡£µ±È»£¬ÕâЩ¶¼±»ÉèÖÃΪ protected£¬ÒԱ㽫À´Í¨¹ý·½·¨À´·â×°¡£
Íê³ÉÕâЩÉèÖúó£¬ÏÖÔÚ¿ÉÒÔÔöÇ¿ Person À࣬ʹ֧֮³ÖСº¢£¬ËùÒÔΪ Person ¶¨ÒåÒ»¸öÐÂ×ֶΣºÒ»¸ö Person ArrayList£¬ËüͬÑùÒ²ÓÐһЩÏà¹ØµÄ·½·¨£¬ÒÔ±ã½øÐÐÊʵ±µÄ·â×°¡£
½ÓÏÂÀ´£¬ÓÉÓÚ´ó¶àÊýСº¢¶¼Óи¸Ä¸£¬»¹½«Ìí¼ÓÁ½¸ö×Ö¶ÎÀ´±íʾĸÇ׺͸¸Ç×£¬²¢Ôö¼ÓÊʵ±µÄ accessor/mutator ·½·¨¡£½«Îª Person ÀàÔö¼ÓÒ»¸öеķ½·¨£¬Ê¹Ö®¿ÉÒÔ´´½¨Ò»¸öÐ嵀 Person£¬Õâ¸ö·½·¨ÓÐÒ»¸öÌùÇеÄÃû³Æ£¬¼´ haveBaby¡£´ËÍ⻹Ôö¼ÓһЩҵÎñ¹æÔò£¬ÒÔÖ§³ÖÉúСº¢µÄÉúÎïѧÐèÇ󣬲¢½«Õâ¸öеÄС Person Ìí¼Óµ½ÎªÄ¸Ç׺͸¸Ç××ֶδ´½¨µÄ children ArrayList ÖС£×öÍêÕâЩ֮ºó£¬ÔÙ½«Õâ¸öÓ¤¶ù·µ»Ø¸øµ÷ÓÃÕß¡£
Çåµ¥ 2 ÏÔʾ£¬Ð¶¨ÒåµÄ Person Àà¿ÉÒÔ´¦ÀíÕâÖÖ¶àÑùÐÔ¹Øϵ¡£
Çåµ¥ 2. ¶¨ÒåΪ¶àÑùÐÔ¹ØϵµÄ¼ÒÍ¥Éú»î
package com.tedneward.model;
import java.util.List; import java.util.ArrayList; import java.util.Iterator;
public class Person { public Person() { } public Person(String firstName, String lastName, Gender gender, int age, Mood mood) { this.firstName = firstName; this.lastName = lastName; this.gender = gender; this.age = age; this.mood = mood; }
public String getFirstName() { return firstName; } public void setFirstName(String value) { firstName = value; }
public String getLastName() { return lastName; } public void setLastName(String value) { lastName = value; }
public Gender getGender() { return gender; }
public int getAge() { return age; } public void setAge(int value) { age = value; }
public Mood getMood() { return mood; } public void setMood(Mood value) { mood = value; }
public Person getSpouse() { return spouse; } public void setSpouse(Person value) { // A few business rules if (spouse != null) throw new IllegalArgumentException("Already married!");
if (value.getSpouse() != null && value.getSpouse() != this) throw new IllegalArgumentException("Already married!");
spouse = value;
// Highly sexist business rule if (gender == Gender.FEMALE) this.setLastName(value.getLastName());
// Make marriage reflexive, if its not already set that way if (value.getSpouse() != this) value.setSpouse; }
public Address getHomeAddress() { return addresses[0]; } public void setHomeAddress(Address value) { addresses[0] = value; }
public Address getWorkAddress() { return addresses[1]; } public void setWorkAddress(Address value) { addresses[1] = value; }
public Address getVacationAddress() { return addresses[2]; } public void setVacationAddress(Address value) { addresses[2] = value; }
public Iterator£¼Person£¾ getChildren() { return children.iterator(); } public Person haveBaby(String name, Gender gender) { // Business rule if ) throw new UnsupportedOperationException("Biological impossibility!");
// Another highly objectionable business rule if (getSpouse() == null) throw new UnsupportedOperationException("Ethical impossibility!");
// Welcome to the world, little one! Person child = new Person(name, this.lastName, gender, 0, Mood.CRANKY);
¹²4Ò³: ÉÏÒ»Ò³ 1 [2] [3] [4] ÏÂÒ»Ò³ |
ÉÏһƪ£º¿ª·¢¿ò¼Ü--ÔÚTomcatÏÂÖÐÎĵij¹µ×½â¾ö ÏÂһƪ£ºJSPµ÷ÓÃSQL server 2000µÄ´æ´¢¹ý³Ì
![](/images/xg.gif) |
Ïà¹ØÎĵµ |
|
![](/images/xg.gif) |
·¢±íÆÀÂÛ |
|
|
|
|