接口列表 - java [英] Interface List - java

查看:165
本文介绍了接口列表 - java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们被要求替换ArrayList并在两个类中使用接口List。我一直在尝试但无济于事。如果有人可以帮助其中一个课程来展示它是如何完成的,我将非常感激。提前致谢。

We have been asked to replace ArrayList and use interface List instead in two classes. I've been trying but to no avail. If someone could help with one of the classes to show how it is done, I would be very appreciative. Thanks in advance.

import java.util.ArrayList;


 public abstract class Animal
  {
// Whether the animal is alive or not.
private boolean alive;
// The animal's field.
private Field field;
// The animal's position in the field.
private Location location;

/**
 * Create a new animal at location in field.
 * 
 * @param field The field currently occupied.
 * @param location The location within the field.
 */
public Animal(Field field, Location location)
{
    alive = true;
    this.field = field;
    setLocation(location);
}

/**
 * Make this animal act - that is: make it do
 * whatever it wants/needs to do.
 * @param newAnimals A list to add newly born animals to.
 */
abstract public void act(ArrayList<Animal> newAnimals);

/**


推荐答案

List是一个接口,因此无法实例化。将所有现在为ArrayList的声明替换为List,其中x是容器包含的类。但是,保持实例化相同( List< x> = new ArrayList< x>(); 有效)。这是一个简单的改变,但你提供的代码显然是不完整的。

"List" is an interface, therefore it cannot be instantiated. Replace all declarations that are now ArrayList with List, where x is the class contained by the container. But, leave the instantiation the same (List<x> = new ArrayList<x>(); is valid). It's a simple change but the code you provided is clearly incomplete.

冒着听起来像巨魔的风险,你也应该标记这个家庭作业或在其他地方寻找工作,除非您强烈认为此更改在您的代码中是合理的。

At the risk of sounding like a troll, you should also tag this "homework" or seek employment elsewhere unless you strongly feel that this change is justified in your code.

这篇关于接口列表 - java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆