【问题标题】:returning inner-class object from interface从接口返回内部类对象
【发布时间】:2017-03-11 05:22:25
【问题描述】:
/**
* Created by unibodydesignn on 11.03.2017. 
*/
public interface Enumeration
{
// Returns true if another element in the collection exists
public boolean hasNext();
// Returns the next element in the collection as an Object
public Object getNext(); }


/**
 * NameCollection implements a collection of names using
 * a simple array.

 */
public class NameCollection
{
String[] names;
//this array will be initiliazed at outside

NameCollection(String[] names)
{
    this.names = names;
}
/**
 * getEnumeration should return an instance of a class that
 implements
 * the Enumeration interface where hasNext() and getNext()
 * correspond to data stored within the names array.
 */
Enumeration getEnumeration ()
{

}

public boolean hasNext()
{
  //i will define this method here
}

public Object getNext()
{
 //i will define getNext() here
}

完成getEnumeration()方法,使其返回一个匿名内部类,对应于names数组的Enumeration接口 名称集合。然后编写一个创建 NamesCollection 的 main 方法 具有示例字符串数组的对象,通过以下方式检索此类的枚举 getEnumeration(),然后遍历枚举输出每个 使用 getNext() 方法命名。

我不明白这个问题的概念。我显然不知道该做什么或从哪里开始?我能找到 Java 的默认 hasNext() 定义吗? 这不是家庭作业。 它是 Absolute Java 书中的一个编程项目。第 13 章。P3。

【问题讨论】:

  • 我怀疑这本书是凭空提出这样一个问题的。您是否阅读并理解了前面的章节? “Java 的默认 hasNext() 定义”是什么意思?为什么 Java 应该为您的自定义接口提供默认定义?

标签: java class interface inner-classes enumeration


【解决方案1】:

完成方法getEnumeration(),使其返回一个匿名内部类,该内部类对应于NamesCollection中names数组的Enumeration接口。

练习的目的似乎是使用匿名类。 例如,不要像这样创建一个 named 类:

class NamesEnumeration implements Enumeration {
    @Override
    public boolean hasNext() {
        // ...
    }

    @Override
    public Object getNext() {
        // ...
    }
}

...说明会指导您改用 anonymous 类,如下所示:

    Enumeration getEnumeration() {
        return new Enumeration() {
            @Override
            public boolean hasNext() {
                // ...
            }

            @Override
            public Object getNext() {
                // ...
            }
        };
    }

重要的一点是匿名实现可以使用在其范围内可见的变量。最值得注意的是这个例子, 封闭NamesCollection 类的names 字段。

NamesCollection 类中, 您不需要 hasNextgetNext 方法。 所以这个类应该是这样的:

public class NameCollection {
    final String[] names;

    NameCollection(String[] names) {
        this.names = names.clone();
    }

    Enumeration getEnumeration() {
        return new Enumeration() {
            int currentIndex = 0;
            //  ^^^^^^^^^^^^ this is a hint for you

            @Override
            public boolean hasNext() {
                // ...
            }

            @Override
            public Object getNext() {
                // ...
            }
        };
    }
}

我做了一些小的改进,并添加了一个提示来帮助您完成实现。

最后,练习还要求添加一个main 方法来练习这个类。应该是这样的:

public static void main(String[] args) {
    String[] sample = {"hello", "world"};
    NameCollection namesCollection = new NameCollection(sample);
    Enumeration names = namesCollection.getEnumeration();
    while (names.hasNext()) {
        System.out.println(names.getNext());
    }
}

【讨论】:

    【解决方案2】:

    我不知道你说的那本书,但让我们了解要求:

    你需要做的是创建一个Enumeration Interface的实现,我不知道这一章是关于Interfaces,还是Enumarations

    1: “完成getEnumeration()方法,使其返回一个匿名内部类,对应NamesCollection中names数组的Enumeration接口”

    在这里您需要返回Enumeration 接口的实现,问题是创建一个匿名类(但我建议创建一个内部类,也许是私有内部类)。像这样,在NameCollection 类中:

    public Enumeration getEnumeration(){
      Enumeration enumerat = new Enumeration(){
        private int index = 0;
    
        public boolean hasNext(){
          return names.length > index;
        }
        public Object getNext(){
          return names[index++];
        }
      };
      return enumerat;
    }
    

    该方法返回Enumeration 类的实现,您可以使用该实现遍历您传递给NameCollection 类的构造函数的名称数组。

    2: "然后编写一个main方法,用一个字符串样本数组创建一个NamesCollection对象,通过getEnumeration()获取这个类的Enumeration,然后遍历枚举使用 getNext() 方法输出每个名称"

    这里你只需要为你的实现创建一个测试类:

    public class Main {
    
      public static void main(String[] args) {
    
        NameCollection nc = new NameCollection(new String[]{ "Adriane", "Beatriz" });
        Enumeration en = nc.getEnumeration();
        while( en.hasNext() ){
          System.out.printf("Name: %s \n", en.getNext() );
        }
    
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-29
      • 2014-04-11
      • 2017-02-24
      • 2011-10-07
      • 1970-01-01
      • 2017-05-15
      相关资源
      最近更新 更多