“实现”在课堂上做了什么?

时间:2010-12-29 21:42:01

标签: java php

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

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

7 个答案:

答案 0 :(得分:104)

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

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!";
    }
}

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());
  }

此循环的输出为:

  

树皮!
  喵!

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

例如,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());  
  }
}

现在考虑以下代码:

//  Some code in other class

List<Person> people = getPeopleList();

Collections.sort(people);

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

答案 1 :(得分:6)

你应该研究Java的接口。 Google快速搜索显示this page,看起来非常不错。

我喜欢将interface视为各种“承诺”:任何实现它的类都有某些可以预期的行为,因此您可以将实现类的实例放入接口类型引用。

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

实现接口需要两个步骤:

  1. 声明接口是在类声明中实现的
  2. 为作为界面一部分的所有方法提供定义。
  3. 接口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;
        }
    }
    

    (注意:在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;
        }
    }
    

    你可能在想,这一切有什么意义?答案是当你看到像this这样的方法时:排序算法只是期望“某种类似的对象”。 (注意所有对象必须实现java.lang.Comparable的要求!)该方法可以列出任何类似的对象列表,无论是String s还是Integer s还是RationalNumber s

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

答案 2 :(得分:2)

接口是通过类实现的。如果你愿意的话,它们是纯粹的抽象类。

在PHP中,当一个类从接口实现时,应严格遵循该接口中定义的方法。当类继承自父类时,可以更改方法参数。接口不是这种情况:

interface ImplementMeStrictly {
   public function foo($a, $b);
}

class Obedient implements ImplementMeStrictly {
   public function foo($a, $b, $c) 
      {
      }
}

将导致错误,因为接口未按定义实现。鉴于:

class InheritMeLoosely {
   public function foo($a)
      {
      }
}

class IDoWhateverWithFoo extends InheritMeLoosely {
   public function foo()
      {
      }
}

允许。

答案 3 :(得分:2)

接口可以被视为方法定义列表(没有任何主体)。如果一个类想要实现和接口,它就会签订一个契约,说它将为接口中列出的所有方法提供一个实现。有关详细信息,请参阅http://download.oracle.com/javase/tutorial/java/concepts/

答案 4 :(得分:0)

在Java中,类可以实现接口。有关详细信息,请参阅http://en.wikipedia.org/wiki/Interface_(Java)。不确定PHP。

希望这有帮助。

答案 5 :(得分:0)

接口定义了所有实现类必须实现的方法的简单契约。当类实现接口时,它必须为其所有方法提供实现。

我猜这张海报假设了一定程度的语言知识。

答案 6 :(得分:0)

它被称为接口。许多OO语言都有此功能。您可能需要阅读这里的php说明:http://de2.php.net/interface