有关系

发布于 2024-09-19 11:35:12 字数 1610 浏览 5 评论 0原文

公共类电梯 () { 按钮firstFloorbutton = ButtonFactory.getButtonInstance(this, 1); 按钮 secondaryFloorbutton = ButtonFactory.getButtonInstance(this, 2); 按钮thirdFloorbutton = ButtonFactory.getButtonInstance(this, 3); 按钮 FourthFloorbutton = ButtonFactory.getButtonInstance(this, 4);

 Fan fan1 = FanFactory.getFanInstance(this);

 Light light1 = LightFactory.getInstance(this);

 private void goUp()
 {
    .....
 }

 private void goDown()
 {
   ......
 }

   .............   

 }

        ============================

 public class Button()
 {

   Elevator E;
   int floor;

   public button (Elevator E, int floor )
   {
    this.E = E;
    this.floor = floor;
   }

   public void buttonPressed()
   {

   //logic to determine which floor the elevator is currently at and determine whether to go up or down to reach "this.floor"

   E.goUp();  // if need to go up

   else

   E.goDown()   // if need to go down

   }
} 


    ==========================


    public class ButtonFactory()
    {

      public Button getButtonInstance(Elevator E, int floor)
      {
         Button b =new Button(E, floor);
         return b;
      }
    }

     ==================

    public class FanFactory(){ .................}

     =====================

    public class LightFactory() { ........... }


     ==========================

Elevator 类和 Button 类之间存在什么样的关系?

根据 Kathy 和 Bert (SCJP) 第 92 页:HAS-A 关系基于使用而不是继承。换句话说,如果类 A 中的代码引用了类 B 的实例,则类 A HAS-A B。

在我的示例中,电梯类代码引用了 Button 的实例,而 Button 引用了 Elevator 类的实例。

任何人都可以澄清这一点。

public class Elevator ()
{
Button firstFloorbutton = ButtonFactory.getButtonInstance(this, 1);
Button secondFloorbutton = ButtonFactory.getButtonInstance(this, 2);
Button thirdFloorbutton = ButtonFactory.getButtonInstance(this, 3);
Button fourthFloorbutton = ButtonFactory.getButtonInstance(this, 4);

 Fan fan1 = FanFactory.getFanInstance(this);

 Light light1 = LightFactory.getInstance(this);

 private void goUp()
 {
    .....
 }

 private void goDown()
 {
   ......
 }

   .............   

 }

        ============================

 public class Button()
 {

   Elevator E;
   int floor;

   public button (Elevator E, int floor )
   {
    this.E = E;
    this.floor = floor;
   }

   public void buttonPressed()
   {

   //logic to determine which floor the elevator is currently at and determine whether to go up or down to reach "this.floor"

   E.goUp();  // if need to go up

   else

   E.goDown()   // if need to go down

   }
} 


    ==========================


    public class ButtonFactory()
    {

      public Button getButtonInstance(Elevator E, int floor)
      {
         Button b =new Button(E, floor);
         return b;
      }
    }

     ==================

    public class FanFactory(){ .................}

     =====================

    public class LightFactory() { ........... }


     ==========================

What kind of relationship exist between the Elevator and Button class?

According to Kathy and Bert (SCJP) page 92 : HAS-A relationship are based on usage rather than inheritance. In other words, class A HAS-A B if code in class A has a reference to an instance of class B.

In my example Elevator class code have a reference to a instance of Button and Button have a reference to instance of Elevator class.

Can anyone please clarify on this.

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

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

发布评论

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

评论(2

陌伤浅笑 2024-09-26 11:35:12

电梯有一个按钮。实际上,它有四个按钮,但每个按钮之间都存在一种关系。

Has-a 是一个不太正式的术语,用于指代两种更正式的关系:关联和聚合。在这两种情况下,关系中的一方都有指向另一方的指针,但它们是通过语义来区分的:在关联关系中,第一方了解另一方,但并不完全支配它(想想你和一位同事) ,或者老板,或者下属),而在聚合关系中,后者是前者的一部分,没有独立存在(想想你和你的肝脏)。在这种情况下,我想说按钮更具体地位于与电梯的聚合关系的从属端,而不仅仅是关联关系。

关联的其他示例可能是客户和销售员,或者部门和员工。聚合、订单和订单行,或者结构和组件。有趣的极端情况是类别和产品、订单和发票。

这种关系的一个实际后果是对象的生存期:在关联中,如果第一个对象死亡,第二个对象可能会存活,但在聚合中,它就会死亡。想想你的电梯:如果你删除了一个电梯(或者将其从实时数据结构中删除并至少让它被垃圾收集),你会希望按钮继续存在吗?

The Elevator has a button. Actually, it has four, but with each of those buttons, there's a has-a relationship.

Has-a is a somewhat informal term used to refer to two more formal kinds of relationship: association and aggregation. In both cases, one party in the relationship has a pointer to the other, but they're distinguished by semantics: in an association relationship, the first party knows about the other, but doesn't completely dominate it (think you and a colleague, or a boss, or a subordinate), whereas in an aggregation relationship, the latter party is part of the former party, and has no independent existence (think you and your liver). In this case, i'd say the Button is more specifically on the subordinate end of an aggregation relationship with the Elevator, not merely an association relationship.

Other examples of association might be a Customer and a Salesman, or a Department and an Employee. Of aggregation, an Order and and OrderLine, or a Structure and a Component. Interesting corner cases are a Category and a Product, and an Order and an Invoice.

One practical consequence of the kind of relationship is object lifetime: in an association, if the first object dies, the second may live, but in an aggregation, it will die. Think about your Elevator: if you deleted one (or removed it from your live data structures and let it be garbage collected, at least), would you want the Buttons to survive?

辞慾 2024-09-26 11:35:12

它是一个Has-A关系,这是一种记住组合模型的简单方法。 Button类由Elevator类组成;即Button 类有一个Elevator 类。

It is a Has-A relationship, which is a simple way of remember the composition model. Button class is composed of Elevator class; i.e. Button class has a Elevator class.

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