类是否必须位于同一继承树上才能具有 Has-A 关系

发布于 2024-12-23 13:49:08 字数 964 浏览 1 评论 0原文

class Employee {
    private String name;
    void setName(String n) { name = n; }
    String getName() { return name; }
}
interface Mungeable {
    void doMunging();
}
public class MyApp implements Mungeable {
    public void doMunging() { ; }
    public static void main(String[] args) {
        Employee e = new Employee();
        e.setName("bob");
        System.out.print(e.getName());
    } 
}

以及可能的答案:

Which are true? (Choose all that apply.)
A. MyApp is-a Employee.
B. MyApp is-a Mungeable.
C. MyApp has-a Employee.
D. MyApp has-a Mungeable.
E. The code is loosely coupled.
F. The Employee class is well encapsulated.

在回答上述问题时,我选择了选项 BCEF

显然正确答案只有 BEF。为了使 MyApp 与 Employee 具有 Has-A 关系,两者必须位于同一继承树层次结构中。这是正确的吗?我认为如果一个类将对象作为成员,它会自动具有 Has-A 关系。

class Employee {
    private String name;
    void setName(String n) { name = n; }
    String getName() { return name; }
}
interface Mungeable {
    void doMunging();
}
public class MyApp implements Mungeable {
    public void doMunging() { ; }
    public static void main(String[] args) {
        Employee e = new Employee();
        e.setName("bob");
        System.out.print(e.getName());
    } 
}

And the possible answers:

Which are true? (Choose all that apply.)
A. MyApp is-a Employee.
B. MyApp is-a Mungeable.
C. MyApp has-a Employee.
D. MyApp has-a Mungeable.
E. The code is loosely coupled.
F. The Employee class is well encapsulated.

While answering the above question i selected options B,C,E and F

Apparently the only correct answers are B,E and F. For MyApp to have a Has-A relationship with Employee both have to be in the same inheritance tree hierarchy. Is this correct? I thought that if a class has the object as a member it automatically has a Has-A relationship.

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

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

发布评论

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

评论(5

孤凫 2024-12-30 13:49:08

MyApp 没有 Employee,没有定义任何成员。 MyApp 有 main 方法,就是这样。根据下面的代码,MyApp has-a Employee

public class MyApp implements Mungeable {
    public void doMunging() { ; }
    private Employee;
    public static void main(String[] args) {
        Employee e = new Employee();
        e.setName("bob");
        System.out.print(e.getName());
    } 
}

MyApp doesn't have Employee, no member is defined. MyApp has main method, thats it. As per below code, MyApp has-a Employee.

public class MyApp implements Mungeable {
    public void doMunging() { ; }
    private Employee;
    public static void main(String[] args) {
        Employee e = new Employee();
        e.setName("bob");
        System.out.print(e.getName());
    } 
}
天邊彩虹 2024-12-30 13:49:08

要使 MyApp 与 Employee 建立 Has-A 关系,两者都必须是
在同一继承树层次结构中。这是正确的吗?

这是不正确的。

我认为如果一个类将对象作为成员,它会自动
具有 Has-A 关系。

你是对的。要点是,MyApp 没有 Employee 作为成员。

For MyApp to have a Has-A relationship with Employee both have to be
in the same inheritance tree hierarchy. Is this correct?

It is not correct.

I thought that if a class has the object as a member it automatically
has a Has-A relationship.

You were right. Point is, MyApp does not have Employee as a member.

红衣飘飘貌似仙 2024-12-30 13:49:08

为了使 MyAppEmployee 建立关系,Employee e 应该是一个成员变量,而不仅仅是在静态方法中本地实例化。

For MyApp to have a relationship to Employee, Employee e should be a member variable and not just be instantiated locally in a static method.

寂寞美少年 2024-12-30 13:49:08

类是否必须位于同一继承树上才能具有 Has-A 关系

否, 例如:

class Person{
    // Person has-a name, 
    // where Person is not a String,
    // neither reverse
    private String name; 
}

Do classes have to be on the same inheritance tree for them to have a Has-A relationship

No, e.g.:

class Person{
    // Person has-a name, 
    // where Person is not a String,
    // neither reverse
    private String name; 
}
最后的乘客 2024-12-30 13:49:08
E. The code is loosely coupled.

这是不正确的。它与员工类紧密耦合,为了松散耦合,它必须在接口或抽象类上工作。为了使其松散耦合,代码如下。

interface IEmployee{
...
...
}

class Employee implements IEmployee {
...
...
}

public class MyApp implements Mungeable {
    public void doMunging() { ; }
    //declare interface type
    private IEmployee emp;
    //use DI to inject  an implementation of IEmployee type in MyApp 
    public MyApp(IEmployee object){
    emp = object;
    }
    public static void main(String[] args) {
        emp.setName("bob");
        System.out.print(emp.getName());
    } 
}
E. The code is loosely coupled.

It's not correct. It is tightly coupled with employee class, for it to be loosely coupled, it must either work on interface or an abstract class. To make it loosely coupled, code would be as following.

interface IEmployee{
...
...
}

class Employee implements IEmployee {
...
...
}

public class MyApp implements Mungeable {
    public void doMunging() { ; }
    //declare interface type
    private IEmployee emp;
    //use DI to inject  an implementation of IEmployee type in MyApp 
    public MyApp(IEmployee object){
    emp = object;
    }
    public static void main(String[] args) {
        emp.setName("bob");
        System.out.print(emp.getName());
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文