有关系和正确的实践

发布于 2024-12-13 11:13:13 字数 3296 浏览 1 评论 0原文

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

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);
    }
}

Recently I am doing a coding exercises I need to make my project , and so far I am practicing it with the code below what I want to ask is that, is this a has a relationship? am I doing the right practice? look at my code, sorry for my bad english

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());
        }
}

Contact Class

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());
    }
}

Employee Class

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());
    }
}

And the Test class

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乜一 2024-12-20 11:13:13

好吧,由于 Employee 没有扩展 Personal,它一个 Personal 和一个 Contact >。

我想您更希望Employee成为一个Personal,因此它应该看起来像这样:

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

所以总结一下:

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

Well, since Employee doesn't extend Personal it has a Personal and a Contact.

I guess you'd rather like Employee to be a Personal and thus it should look like this:

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

So to summarize:

  • is-a means a class/object extends another class or implements an interface, i.e. A is-a B if A extends B or A implements B
  • has-a means that a class/object has a variable of that type, like Contact contact in your Employee class, which means Employee has-a contact.
林空鹿饮溪 2024-12-20 11:13:13

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

Yes, this is a "has-a" relationship (exactly as we discussed in your other question).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文