简体   繁体   中英

Class associated with an interface or abstract class

I have a trouble with understanding how a class can associate ( or have a composition relation ) with an interface or an abstract class. As far as i know, i cannot initiate an interface ( nor abstract class ).

How does this relation work and how can i use the class that has a composition relation with an interface? As i have attached below.

Another question: If i want to extend button class to a new class ( KeyBoard ), how the composition relation will effect on the KeyBoard class? Do i need to implement the interface? Can i ignore it?

按钮与按钮服务器的组成关系

Thank you

Simply spoken: You can not instantiate ButtonServer but you can instantiate SendButtonServer . And that will aggregate Button s.

When you subclass Button the SendButtonServer can use only what is defined in Button but nothing else. If you need your subclasses in SendButtonServer you would need to change the definition and replace Button . But how is not clear as per your question.

I have a trouble with understanding how a class can associate ( or have a composition relation ) with an iterface or an abstract class. As far as i know, i cannot initiate an interface ( nor abstract class ).

How does this relation work and how can i use the class that has a composition relation with an interface?

yes they cannot have instance, but in fact the effective type of the associated element is one of the sub types.

Example:

interface ButtonServer {
  public void buttonPressed();
};

class SendButtonAdapter implements ButtonServer {
  public void buttonPressed() {
    System.out.println("SendButtonAdapter buttonPressed");
  }
}

class DigitButtonAdapter implements ButtonServer {
  public void buttonPressed() {
    System.out.println("DigitButtonAdapter buttonPressed");
  }
}

class Button {
  private ButtonServer server;

  public Button(ButtonServer b) {
    server = b;
  }

  void pressed() {
    server.buttonPressed();
  }
}

class Main {
  public static void main(String argv[]) {
    Button b1 = new Button(new SendButtonAdapter());

    b1.pressed();

    Button b2 = new Button(new DigitButtonAdapter());

    b2.pressed();
  }
}

Compilation and execution:

pi@raspberrypi:/tmp $ javac Main.java 
pi@raspberrypi:/tmp $ java Main
SendButtonAdapter buttonPressed
DigitButtonAdapter buttonPressed
pi@raspberrypi:/tmp $ 

Another question: If i want to extend button class to a new class ( KeyBoard ), how the composition relation will effect on the KeyBoard class? Do i need to implement the interface? Can i ignore it?

It seems the domain is a phone, so Button is a key and KeyBoard will not inherits/implements ButtonServer because that have no sense, but you have an association KeyBoard -1----n_keys- Button

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM