"实现"在课堂上做了什么?

Web*_*net 32 php java

如果一个类实现另一个类......那意味着什么?我找到了这个代码示例:http://www.java2s.com/Code/Php/Class/extendsandimplement.htm

但遗憾的是它没有任何解释......

Way*_*man 107

实现意味着它接受接口指定的指定行为.请考虑以下界面:

public interface ISpeak
{
   public String talk();
}

public class Dog implements ISpeak
{
   public String talk()
   {
        return "bark!";
   }
}

public class Cat implements ISpeak
{
    public String talk()
    {
        return "meow!";
    }
}
Run Code Online (Sandbox Code Playgroud)

无论是CatDog类实现的ISpeak接口.

接口有什么好处,我们现在可以通过ISpeak接口引用这个类的实例.请考虑以下示例:

  Dog dog = new Dog();
  Cat cat = new Cat();

  List<ISpeak> animalsThatTalk = new ArrayList<ISpeak>();

  animalsThatTalk.add(dog);
  animalsThatTalk.add(cat);

  for (ISpeak ispeak : animalsThatTalk)
  {
      System.out.println(ispeak.talk());
  }
Run Code Online (Sandbox Code Playgroud)

此循环的输出将是:

吠!
喵!

接口提供了一种基于它们执行的操作以通用方式与类交互的方法, 而不会暴露实现类的内容.

例如,Java中最常用的接口之一就是Comparable.如果对象实现了此接口,则可以编写一个消费者可用于对对象进行排序的实现.

例如:

public class Person implements Comparable<Person>
{
  private String firstName;
  private String lastName;

  // Getters/Setters

  public int compareTo(Person p)
  {
    return this.lastName.compareTo(p.getLastName());  
  }
}
Run Code Online (Sandbox Code Playgroud)

现在考虑这段代码:

//  Some code in other class

List<Person> people = getPeopleList();

Collections.sort(people);
Run Code Online (Sandbox Code Playgroud)

这段代码的作用是为Person班级提供了一种自然的顺序.因为我们实现了Comparable接口,所以我们能够利用该Collections.sort()方法按照自然顺序ListPerson对象进行排序,在这种情况下,通过姓氏.

  • @Webnet:正确.在Java中,如果一个类指定它正在实现一个接口,它必须实现该接口的所有方法. (4认同)

Pla*_*ure 6

你应该研究Java的接口.一个快速的谷歌搜索显示这个页面,看起来很不错.

我喜欢把它interface视为一种"承诺":任何实现它的类都有一些可以预期的行为,因此你可以将一个实现类的实例放入一个接口类型的引用中.

一个简单的例子是java.lang.Comparable界面.通过在您自己的类中实现此接口中的所有方法,您声称您的对象彼此"可比",并且可以部分排序.

实现接口需要两个步骤:

  1. 声明接口是在类声明中实现的
  2. 为作为接口一部分的所有方法提供定义.

接口java.lang.Comparable只有一种方法,public int compareTo(Object other).所以你需要提供这种方法.

这是一个例子.鉴于此类RationalNumber:

public class RationalNumber
{
    public int numerator;
    public int denominator;

    public RationalNumber(int num, int den)
    {
        this.numerator = num;
        this.denominator = den;
    }
}
Run Code Online (Sandbox Code Playgroud)

(注意:在Java中使用公共字段通常是不好的做法,但我打算这是一个非常简单的普通旧数据类型,所以我不关心公共字段!)

如果我想能够比较两个RationalNumber实例(为了排序目的,可能?),我可以通过实现java.lang.Comparable接口来实现.为此,需要做两件事:为compareTo接口提供定义并声明接口已实现.

这是充实的类看起来的样子:

public class RationalNumber implements java.lang.Comparable
{
    public int numerator;
    public int denominator;

    public RationalNumber(int num, int den)
    {
        this.numerator = num;
        this.denominator = den;
    }

    public int compareTo(Object other)
    {
        if (other == null || !(other instanceof RationalNumber))
        {
            return -1; // Put this object before non-RationalNumber objects
        }

        RationalNumber r = (RationalNumber)other;

        // Do the calculations by cross-multiplying. This isn't really important to
        // the answer, but the point is we're comparing the two rational numbers.
        // And no, I don't care if it's mathematically inaccurate.

        int myTotal = this.numerator * other.denominator;
        int theirTotal = other.numerator * this.denominator;

        if (myTotal < theirTotal) return -1;
        if (myTotal > theirTotal) return 1;
        return 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

你可能在想,这一切有什么意义呢?答案是当你在类似的方法这样:排序算法,只是希望"某种可比对象".(注意所有对象必须实现的要求java.lang.Comparable!)该方法可以列出任何类型的可比较对象,无论是Strings还是Integers或RationalNumbers.

注意:我在这个答案中使用了Java 1.4的实践.java.lang.Comparable现在是一个通用接口,但我没有时间解释泛型.