简体   繁体   English

我将如何实现这个 Java 接口?

[英]How would I go about implementing this Java interface?

I am currently in the design mode for this problem:我目前处于这个问题的设计模式:

Implement the Speaker interface that is predefined.实现预定义的 Speaker 接口。 Create three classes that implement Speaker in various ways.创建三个以各种方式实现 Speaker 的类。 Create a driver class whose main method instantiates some of these objects and tests their abilities.创建一个驱动程序类,其主要方法实例化其中一些对象并测试它们的能力。

How would I go about designing this program and them moving into the coding stage.我将如何设计这个程序并让他们进入编码阶段。 I want to use these three classes to implement the Speaker interface class: Politician, Lecturer, and Pastor class.我想用这三个类来实现Speaker接口类:Politician、Lecturer和Pastor类。 The methods I want to use are:我想使用的方法是:

public void speak(); public void announce (String str);

Now for my design and coding, how would I go about to declare and object reference variable and have that variable have multiple references?现在对于我的设计和编码,我将如何声明和对象引用变量并使该变量具有多个引用?

It's simple really.真的很简单。 In brief:简单来说:

class ClassA implements Speaker
{
   public void speak(){
          System.out.println("I love Java") ; //implement the speak method
    }
}
class ClassB implements Speaker //follow the example of ClassA
class ClassC implements Speaker //same as above

Speaker[] speakers = new Speakers{new ClassA(),new ClassB(),new ClassC()} ;

for(Speaker speaker: speakers)
   speaker.speak(); //polymorphically call the speak() method defined in the contract.

See "What is an Interface?"请参阅“什么是接口?” http://docs.oracle.com/javase/tutorial/java/concepts/interface.html This will hopefully get you started with the basics you're looking for. http://docs.oracle.com/javase/tutorial/java/concepts/interface.html这有望让您开始了解您正在寻找的基础知识。

The start of the implementation would look something like the following...实施的开始将类似于以下内容...

class Politician implements Speaker
{
  public void speak()
  { 
    // Method implementation
  }
  public void announce (String str)
  {
    // Method implementation
  }
}
class Lecturer implements Speaker
{
  public void speak()
  { 
    // Method implementation
  }
  public void announce (String str)
  {
    // Method implementation
  }
}
class Lecturer implements Speaker
{
  public void speak()
  { 
    // Method implementation
  }
  public void announce (String str)
  {
    // Method implementation
  }
}

public static void main(String [] args)
{
   Speaker s1 = new Politician();
   Speaker s2 = new Pastor();
   Speaker s3 = new Lecturer();
   // Do something...
}

Use a Factory Method Design Pattern.. Check this article at http://en.wikipedia.org/wiki/Factory_method_pattern使用工厂方法设计模式.. 在http://en.wikipedia.org/wiki/Factory_method_pattern查看这篇文章

Your code could look something like this, if you use the Factory pattern如果您使用工厂模式,您的代码可能看起来像这样

public class SpeakerFactory {

      enum SpeakerEnum { POLITICIAN, LECTURER, PASTOR} ;
      Speaker getSpeaker(SpeakerEnum speakerType) {
          switch (speakerType) {

          case POLITICIAN : return new Politician();

          ....

          }
      }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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