java接口方法用法

时间:2016-03-28 19:38:37

标签: java interface implementation

您好我已经获得了以下界面,我需要使用此界面实现包,我有点混淆如何使用这些方法,任何帮助/提示将不胜感激。我需要在包中添加商品,它应该能够接受重复的商品。然后我删除了一个项目并找到了一个项目。

public interface Things<T> extends Iterable<T> {
    public int size();
    public boolean add(T x);
    public T remove(T x);
    public T find(T x);
    public void clear();
}
//Implementing thing using bag
public class Bag<T> implements Things<T> {

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public String add(T x) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String remove(T x) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String find(T x) {
        // TODO Auto-generated method stub
        return null;
    }

}


public class ThingsDemo {


    public static void main(String args[])

    {
        How to I use add(), remove(), and find method here?


    }

}

2 个答案:

答案 0 :(得分:0)

这样的事情:

public interface Things<T> extends Iterable<T> {
    public int size();
    public boolean add(T x);
    public T remove(T x);
    public T find(T x);
    public void clear();
}
//Implementing thing using bag
public class Bag<T> implements Things<T> {
    private ArrayList<T> list = new ArrayList<T>();

    @Override
    public int size() {
        return list.size();
    }

    @Override
    public boolean add(T x) {
        list.add(x);
    }

    @Override
    //It's weird why the remove method returns T
    //Sine you already pass x of type T as parameter
    public T remove(T x) {
        list.remove(x);
        return x;
    }

    @Override
    public T find(T x) {
        //Since you already know x, what is find() searching for?
        return null;
    }

}


public class ThingsDemo {


    public static void main(String args[])

    {
        Bag<String> bag = new Bag<>();
        bag.add("cat");  
        bag.remove("cat");
    }
}

答案 1 :(得分:-1)

你走了。你不应该在你的包类中使用String返回类型。它应该类似于你在界面内声明的内容。请参阅添加样本的方法,并类似地实现了删除和其他方法。

public interface Things<T> extends Iterable<T> {
    public int size();
    public boolean add(T x);
    public T remove(T x);
    public T find(T x);
    public void clear();
}

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Bag<T> implements Things<T> {

List<T> data = new ArrayList<T>();

@Override
public int size() {
    data.size();
}

@Override
public boolean add(T x) {
    data.add(x);
    return false;
}

@Override
public T remove(T x) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public T find(T x) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void clear() {

}

@Override
public Iterator<T> iterator() {
    return null;
}
}

public class ThingsDemo {


public static void main(String args[])

{
    Things<String> things = new Bag<>();

    things.add("something");
    System.out.println(things.size());
    things.remove("something");
    System.out.println(things.size());

   }

}