• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

面向Java开发人员db4o指南:数组和集合

发布: 2008-4-28 10:27 | 作者: 不详 | 来源: 无 | 查看: 23次 | 进入软件测试论坛讨论

领测软件测试网

清单 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 it's not already set that way
if (value.getSpouse() != this)
value.setSpouse(this); 
}

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 (this.gender.equals(Gender.MALE))
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); 
// Well, wouldn't YOU be cranky if you'd just been pushed out of
// a nice warm place?!?

// These are your parents... 
child.father = this.getSpouse(); 
child.mother = this; 

// ... and you're their new baby.
// (Everybody say "Awwww....")
children.add(child); 
this.getSpouse().children.add(child); 

return child; 
}

public String toString()
{
return 
"[Person: " +
"firstName = " + firstName + " " +
"lastName = " + lastName + " " +
"gender = " + gender + " " +
"age = " + age + " " + 
"mood = " + mood + " " +
(spouse != null ? "spouse = " + spouse.getFirstName() + " " : "") +
"]"; 
}

public boolean equals(Object rhs)
{
if (rhs == this)
return true; 

if (!(rhs instanceof Person))
return false; 

Person other = (Person)rhs; 
return (this.firstName.equals(other.firstName) &&
this.lastName.equals(other.lastName) &&
this.gender.equals(other.gender) &&
this.age == other.age); 
}

private String firstName; 
private String lastName; 
private Gender gender; 
private int age; 
private Mood mood; 
private Person spouse; 
private Address[] addresses = new Address[3]; 
private List<Person> children = new ArrayList<Person>(); 
private Person mother; 
private Person father; 
}

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网