Home >Backend Development >PHP Problem >How can php implement interface

How can php implement interface

coldplay.xixi
coldplay.xixiOriginal
2020-10-30 15:52:222764browse

php method to implement the interface: implement it through the interface definition, the code is [[modifier] class 39312d935ffcf6a91fab9fca05b9db5e [extends parent class name] [implements interface list]{[public] [static] [ final] constant;}].

How can php implement interface

php method to implement the interface:

The idea of ​​the interface is to specify an interface that implements the interface A series of functions that a class must implement. Generally, we use interface to declare an interface and declare some methods (i.e. functions) in the interface. Note that we only declare that we do not need to implement this function. Then, use class to declare a class and implements to use the interface, and then implement the methods declared in the interface in the class.

The general definition method is as follows:

Interface definition:

[修饰符] interface 接口名 [extends 父接口名列表]{
 
[public] [static] [final] 常量;
[public] [abstract] 方法;
}

Modifier: Optional, used to specify the access permission of the interface, the optional value is public. If omitted the default access permissions are used.

Interface name: A required parameter, used to specify the name of the interface. The interface name must be a legal Java identifier. Generally, capital letters are required.

extends Parent interface name list: Optional parameter, used to specify which parent interface the interface to be defined inherits from. When using the extends keyword, the parent interface name is a required parameter.

Methods: The methods in the interface are only defined but not implemented.

Interface implementation:

[修饰符] class <类名> [extends 父类名] [implements 接口列表]{
}

Modifier: Optional parameter, used to specify the access permission of the class. The optional values ​​are public, abstract and final.

Class name: A required parameter, used to specify the name of the class. The class name must be a legal Java identifier. Generally, capital letters are required.

extends Parent class name: Optional parameter, used to specify which parent class the class to be defined inherits from. When using the extends keyword, the parent class name is a required parameter.

implements Interface list: Optional parameter, used to specify which interfaces this class implements. When using the implements keyword, the interface list is a required parameter. When there are multiple interface names in the interface list, separate them with commas.

Example:

Interface definition:

public interface CalInterface   
{  
    final float PI=3.14159f;//定义用于表示圆周率的常量PI  
    float getArea(float r);//定义一个用于计算面积的方法getArea()  
    float getCircumference(float r);//定义一个用于计算周长的方法getCircumference()  
}

Interface implementation:

public class Cire implements CalInterface   
{  
    public float getArea(float r)   
    {  
        float area=PI*r*r;//计算圆面积并赋值给变量area  
        return area;//返回计算后的圆面积  
    }  
    public float getCircumference(float r)   
    {  
        float circumference=2*PI*r;      //计算圆周长并赋值给变量circumference  
        return circumference;           //返回计算后的圆周长  
    }  
    public static void main(String[] args)   
    {  
        Cire c = new Cire();  
        float f = c.getArea(2.0f);  
        System.out.println(Float.toString(f));  
    }  
}

Note that if the method specified in the interface is not implemented, a fatal error will be generated .

If you want to know more about programming learning, please pay attention to the php training column!

The above is the detailed content of How can php implement interface. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn