编写类时接口的用途是什么?
这是我在网上看到的一个例子。
<?php
interface Chargeable {
public function getPrice();
}
class Employee implements Chargeable {
protected $price;
public function getPrice() {
return $this->price;
}
}
$product = new Employee();
?>
答案 0 :(得分:33)
这是我学习界面并理解它们的方法之一。
想象一下这种情况:
abstract class Plane {
public function openDoors();
}
interface Fliers {
public function fly();
}
现在让我们使用它们:
class Boeing747 extends Plane implements Fliers {
public function fly() {
// some stuff
}
public function openDoors() {
// do something
}
}
和
class Tweety implements Fliers{
public function fly() {
// some stuff
}
}
波音747是 Plane 可以飞行而Tweety是鸟而不是飞行但是Tweety对“openDoors”毫无意义。
关键是接口可以由不同类型的对象实现,但类不能。正如你所看到的那样,除了两者都可以飞行之外,波音747和特威蒂没有任何共同之处。
答案 1 :(得分:11)
接口是面向对象编程中的一个概念,它支持多态。基本上,接口就像一个契约,实现它的类同意提供某些功能,以便它们可以像使用接口的其他类一样使用
您的示例显示了保证他们具有getPrice方法的类。然后,您可以编写利用具有此方法的对象的代码,而不必担心它是什么类。
答案 2 :(得分:0)
接口允许您将接口与实现分开。当您希望在代码中具有正交性时,这很方便。
基本上,您将能够创建接受Chargeable
的函数,并且只要它实现Chargeable
就能够传递任何对象。如果您需要更改Employee
课程,这可以使您在未来的路上保持灵活性。此外,它允许您的方法接受任何“收费”的对象。
答案 3 :(得分:0)
在具有多重继承而非接口的语言中,您有抽象类。在PHP中没有多重继承,所以你有接口。一个类可以实现各种接口。唯一的一点是保证你的班级有一定的方法。
答案 4 :(得分:0)
此刻我正在努力解决这个问题(而且我认为我正在阅读与OP相同的书......)。
在我看来,接口只是对实现接口的类强制执行“契约”义务,以实现接口中出现的函数/属性。然而,实现接口的类/对象是不是可以以独特的方式这样做,因为没有定义实现?
那里没有涉及实际的继承吗?
答案 5 :(得分:-1)
Firstable,接口结构为实现外部通信的类提供接口。简单的例子是电视。电视是一个类,它上面的按钮是接口。
使用接口的建议:
1-Java不支持多重继承。如果我们想从不同的类中添加两个不同的方法我们不能(扩展ClassA,ClassB)不可能实现A,B没问题。
2 - 另一个是安全性和灵活性可以给出一个更具体的例子 如果我们想要这个类的某些方法无法访问,我们怎么能这样做? 多态性+接口我们可以做到这一点 如果我们不想走路和吠叫方法应该无法到达
abstract class Animal {
String name;
int age;
public Animal(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
class Dog extends Animal implements Run {
public Dog(String name, int age) {
super(name, age);
// TODO Auto-generated constructor stub
}
@Override
public void run() {
// TODO Auto-generated method stub
}
public void bark() {
}
}
class Cat extends Animal implements Climb {
public Cat(String name, int age) {
super(name, age);
}
@Override
public void climb() {
// TODO Auto-generated method stub
}
public void walk() {
}
}
public class Main {
public static void main(String[] args) {
// if we want that some of methods of the class are not reachable how
// can we do that?
// polymorphism + interface we can do that
// for example if we do not want walk and bark methods should be
// unreachable
Climb cat = new Cat("boncuk", 5);
Run dog = new Dog("karabas", 7);
// see we cannot reach the method walk() or bark()
dog.walk(); // will give an error. since there is no such a method in the interface.
}
}}
enter code here
答案 6 :(得分:-2)
让我举一个例子来理解这类类的朦胧
public interface transport{
public double getSpeed(){return(0/*the speed*/);}
public void setSpeed(){return(0/*the speed*/);}
}
class car extend transport{
//implementation of transport interface
public double getSpeed(){return(speed/*the speed*/);}
public void setSpeed(){speed=250;}
}
class train extend transport{
//implementation of transport interface
....
}
class plane extend transport{
//implementation of transport interface
....
}
所以接口类是一般概念。