简体   繁体   English

实现和接口

[英]implementing and interfaces

I tried looking up tutorials and videos and I understand what implementing does, although I'm a bit confused as to how one would implement a class from the Java Library. 我尝试查找教程和视频,并且了解实现的作用,尽管我对如何实现Java库中的类有些困惑。 In my homework, I'm suppose to utilize the class, DataSet and make it so it accepts Comparable objects. 在我的作业中,我假设利用DataSet类,并使其接受Comparable对象。 The program is suppose to record the Min and Max values depending on the objects, in this case, I'm suppose to use strings. 该程序假设根据对象记录Min和Max值,在这种情况下,我假设使用字符串。 I wasn't sure if I needed any classes to implement the Comparable interface, so I made two classes just in case I was suppose to do so. 我不确定是否需要任何类来实现Comparable接口,所以我做了两个类以防万一。 My real question is how do I actually incorperate a String variable in the tester class to actually read and compare the object to another? 我真正的问题是如何在测试器类中实际合并一个String变量,以实际读取该对象并将其与另一个对象进行比较? thanks in advance. 提前致谢。

public class Word implements Comparable
{
private String str;

public Word()
{
    str = null;
}

public Word(String s)
{
    str = s;
}

 public int compareTo(Object other)
 {
    String n = (String) other;
    return str.compareTo(n);
 }

}

I wasn't sure which of the two classes would be suitable for implementing Although i think the String class below would not work at all b/c It's already a standard class so I wasn't too sure about using it 我不确定这两个类中的哪一个适合实施,尽管我认为下面的String类根本无法使用b / c这已经是一个标准类,所以我不太确定使用它

public class String implements Comparable
{
    public String s;

    public String()
    {
    s = null;
    }

public String(String str)
{
    s = str;
}
public int compareTo(Object other)
{
    String n = (String) other;
   return s.compareTo(n);
}
}


public interface Comparable
{
public int compareTo(Object other);
}


public class DataSet
{
 private Object maximum;
 private Object least;
 private Comparable compare;
 private int count;

 public DataSet(Comparable s)
 {
   compare = s;
 }

 public void add(Object x)
 {

   if(count == 0)
   least = x;
   if(count == 0 || compare.compareTo(x) >=0)
   maximum = x;
   else if(compare.compareTo(x) <0)
   least = x;
   count++;

 }
 public Object getMaximum()
 {
  return maximum;
 }

 public Object getLeast()
 {
   return least;
 }

 }


public class DataSetTester
{
   public static void main(String[] args)
  {
  Comparable n = new Word("sand");
  DataSet data = new DataSet(n);


  data.add(new Word(man));



  System.out.println("Maximum Word: " + data.getMaximum());
  System.out.println("Least Word: " + data.getLeast());
 }
}

An interface is a contract that showes that your class contain all methodes that are implemented in the interface. 接口是一种契约,表明您的类包含该接口中实现的所有方法。 In this case the CompareTo(object other) . 在这种情况下, CompareTo(object other) The String class already implements the comparable interface so you don't need youre own class. String类已经实现了可比较的接口,因此您不需要自己的类。 I think your data set class should look something like this : 我认为您的数据集类应如下所示:

public class DataSet<T implements Comparable>
{
 private T maximum;
 private T least;
 private T count;


 public void add(T x)
 {

   if(count == 0){
     least = x;
     maximum = x;
   }
   else if(least.compareTo(x) > 0)
     least = x;
   else if(maximum.compareTo(x) < 0)
     maximum = x;
   count++;
 }

 public T getMaximum()
 {
  return maximum;
 }

 public T getLeast()
 {
   return least;
 }

 }

T is a generic type and in your case it should be String, Here is how you create a new Data set: DataSet<String> ds = new DataSet<String>; T是泛型类型,在您的情况下,它应该是String。这是创建新数据集的方式: DataSet<String> ds = new DataSet<String>;

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

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