Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 类中接口的用途_Php - Fatal编程技术网

Php 类中接口的用途

Php 类中接口的用途,php,Php,编写类时接口的用途是什么 这是我在网上看到的一个例子 <?php interface Chargeable { public function getPrice(); } class Employee implements Chargeable { protected $price; public function getPrice() { return $this->price; } } $product = new Emplo

编写类时接口的用途是什么

这是我在网上看到的一个例子

<?php
interface Chargeable {
    public function getPrice();
}

class Employee implements Chargeable {
    protected $price;

    public function getPrice() {
        return $this->price;
    }
}

$product = new Employee();

?>

接口是面向对象编程中的一个概念,它支持多态性。基本上一个接口就像一个契约,通过这个契约,实现它的类同意提供某些功能,以便它们可以像其他使用该接口的类一样使用


您的示例显示了一些类,这些类保证它们具有可用的getPrice方法。然后,您可以编写代码,利用具有此方法的对象,而不必担心它是什么类型的类。

接口允许您将接口与实现分离。当您希望在代码中具有正交性时,这非常方便


基本上,您将能够创建接受可计费的函数,并且只要它实现了可计费的,就能够在其中传递任何对象。如果您需要更改
Employee
类,这将使您能够灵活处理。它还允许您的方法接受任何“可收费”的对象。

以下是我学习和理解接口的方法之一

想象一下这个场景:

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
    }
}
以及:

Boeing747是一架会飞的飞机,Tweety是一只会飞的鸟,但Tweety“开门”毫无意义


关键是接口可以由不同类型的对象实现,但类不能。正如您所看到的,Boeing747和Tweety没有任何共同之处,只有两者都可以飞行。

在具有多重继承而不是接口的语言中,您有抽象类。在PHP中没有多重继承,所以您有接口。一个类可以实现各种接口。唯一的一点是保证类具有特定的方法集

目前我自己也在努力摸索这本书(我想我读的是与OP相同的书……)

在我看来,接口只是对实现接口的类强制执行“契约”义务,以实现接口中出现的函数/属性。然而,实现接口的类/对象是否可以以一种独特的方式实现接口,因为实现没有定义


没有涉及实际的继承,是吗?

让我举个例子来了解这种类型的类的定义

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 
....
}

因此,接口类是一个普遍的概念。

首先,接口结构为实现对外通信的类提供接口。简单的例子是电视。电视是一个类,上面的按钮是接口

使用接口的建议:

1-Java不支持多重继承。如果我们想从不同的类中添加两个不同的方法,我们不能(扩展ClassA,ClassB)实现A,B没有问题

2-另一个adv是安全性和灵活性,可以给出更具体的示例 如果我们想要类的某些方法是不可访问的,我们怎么能做到呢? 多态+接口我们可以做到 如果我们不想要走路和吠叫的方法应该是不可能实现的

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

我同意这是一个非常有用的答案。如果他们的(传单=打开的门&平面=飞行)功能被颠倒,并且使用类平面而不是传单来执行飞行功能,那么就不能这样说了。所以tweety类扩展了飞机的飞行。而类Boeing747实现了传单和扩展平面,分别打开门和飞行。我唯一能想到的是,它可以帮助节省内存,因为接口不能定义任何内容,抽象类可以。请看这个。回答不好。或者根本不回答。接口“实现”而不是扩展接口被实现,而不是扩展。此外,接口背后的思想是,实现接口的任何对象都可以作为参数传递给需要接口类型的对象的方法。典型的例子是比较器和可比表。我是否使用historyOverflow?
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