有关系和正确的做法

时间:2011-11-03 10:52:45

标签: java relationship composition aggregation

最近我正在做一个我需要编写项目的编码练习,到目前为止,我正在用下面的代码练习它,我想问的是,这是一个有关系吗?我在做正确的练习吗?看看我的代码,抱歉我的英文不好

public class Personal {

        private String firstName;
        private String middleInitial;
        private String lastName;
        private int age;

        public Personal(String firstName,String middleInitial , String lastName , int age){
            setFirstName(firstName);
            setMiddleInitial(middleInitial);
            setLastName(lastName);
            setAge(age);
        }

        public void setFirstName(String firstName){
            this.firstName = firstName;
        }

        public String getFirstName(){
            return firstName;
        }

        public void setMiddleInitial(String middleInitial){
            this.middleInitial = middleInitial;
        }

        public String getMiddleInitial(){
            return middleInitial;
        }

        public void setLastName(String lastName){
            this.lastName = lastName;
        }

        public String getLastName(){
            return lastName;
        }

        public void setAge(int age){
            this.age = age;
        }

        public int getAge(){
            return age;
        }

        public String toString(){
            return String.format("First Name: "+getFirstName()+"\nMiddle Initial: "+getMiddleInitial()+
                                    "\nLast Name: "+getLastName()+"\nAge: "+getAge());
        }
}

联系班级

public class Contact {
    private String address;
    private String email;
    private String contactNumber;

    public Contact(String address,String contactNumber, String email){
        setAddress(address);
        setContactNumber(contactNumber);
        setEmail(email);
    }

    public void setAddress(String address){
        this.address = address;
    }

    public String getAddress(){
        return address;
    }

    public void setEmail(String email){
        this.email = email;
    }

    public String getEmail(){
        return email;
    }

    public void setContactNumber(String contactNumber){
        this.contactNumber = contactNumber;
    }

    public String getContactNumber(){
        return contactNumber;
    }

    public String toString(){
        return String.format("Address: "+getAddress()+"\nContact Number: "+getContactNumber()+
                                "\nEmail Address: "+getEmail());
    }
}

员工类

public class Employee {
    private Personal personal;
    private Contact contact;

    public Employee(Personal personal, Contact contact){
        this.personal = personal;
        this.contact = contact;
    }

    public void setFirstName(String firstName){
        this.personal.setFirstName(firstName);
    }

    public String toString(){
        return String.format(personal.toString()+contact.toString());
    }
}

和测试类

public class TestClass {

    public static void main(String[] args){
        Personal personalHerp = new Personal("John","M","Doe",18);
        Contact contactHerp = new Contact("88 Herp Derp St U mad New york","724-15-70","fido.com");
        Employee employeeHerp = new Employee(personalHerp,contactHerp);
        System.out.println(employeeHerp);
    }
}

2 个答案:

答案 0 :(得分:1)

好吧,由于Employee未延伸Personal,因此 PersonalContact

我想你更喜欢Employee 一个Personal因此它应该是这样的:

public class Employee extends Personal {
  private Contact contact;
  ...
}

总结一下:

  • is-a 表示类/对象扩展另一个类或实现一个接口,即如果A extends BA implements B
  • ,则A是-B
  • has -a 表示类/对象具有该类型的变量,例如Contact contact类中的Employee,这意味着Employee具有 - 联系方式。

答案 1 :(得分:0)

是的,这是一种“有一种”关系(正如我们在你的另一个问题中所讨论的那样)。