国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Backend Development PHP Tutorial PHP object-oriented programming (oop) study notes (1) - abstract classes, object interfaces, instanceof and contract programming_PHP tutorial

PHP object-oriented programming (oop) study notes (1) - abstract classes, object interfaces, instanceof and contract programming_PHP tutorial

Jul 13, 2016 am 10:28 AM
instanceof abstract class

1. Abstract class in PHP

PHP 5 supports abstract classes and abstract methods. Classes defined as abstract cannot be instantiated. Any class must be declared abstract if at least one method in it is declared abstract. A method defined as abstract only declares its calling method (parameters) and cannot define its specific function implementation. A class can be declared abstract by using the abstract modifier in its declaration.

It can be understood that an abstract class serves as a base class and leaves specific details to successors. By abstracting concepts, you can create scalable architectures in your development projects.

Copy code The code is as follows:

abstract class AbstractClass
{
code...
}

1.1, Abstract method

Use the abstract keyword to define abstract methods. Abstract methods only retain the method prototype (the signature after the method body is removed from the method definition), which includes access levels, function keywords, function names and parameters. It does not contain ({}) or any code inside brackets. For example, the following code is an abstract method definition:

Copy code The code is as follows:

abstract public function prototypeName($protoParam);

When inheriting an abstract class, the subclass must define all abstract methods in the parent class; in addition, the access control of these methods must be the same (or more relaxed) as in the parent class. In addition, the method calling methods must match, that is, the type and number of required parameters must be consistent.

1.2. About abstract classes

A class must be declared as an abstract class as long as it contains at least one abstract method.
Methods declared as abstract must contain the same or lower access level when implemented.
Instances of abstract classes cannot be created using the new keyword.
Methods declared as abstract cannot contain function bodies.
If the extended class is also declared as an abstract class, you do not need to implement all abstract methods when extending the abstract class. (If a class inherits from an abstract class, it must also be declared abstract when it does not implement all abstract methods declared in the base class.)
1.3. Use abstract classes

Copy code The code is as follows:

abstract class Car
{???
??? abstract function getMaxSpeend();
}
class Roadster extends Car
{
??? public $Speend;
??? public function SetSpeend($speend = 0)
??? {
??????? $this->Speend = $speend;
??? }
??? public function getMaxSpeend()
??? {
??????? return $this->Speend;
??? }
}
class Street
{
??? public $Cars ;
??? public $SpeendLimit ;
??? function __construct( $speendLimit = 200)
??? {
??????? $this -> SpeendLimit = $speendLimit;
??????? $this -> Cars = array();
??? }
??? protected function IsStreetLegal($car)
??? {
??????? if ($car->getMaxSpeend() < $this -> SpeendLimit)
??????? {
??????????? return true;
??????? }
??????? else
??????? {
??????????? return false;
??????? }
??? }
??? public function AddCar($car)
??? {
??????? if($this->IsStreetLegal($car))
??????? {
??????????? echo 'The Car was allowed on the road.';
??????????? $this->Cars[] = $car;
??????? }
??????? else
??????? {
??????????? echo 'The Car is too fast and was not allowed on the road.';
??????? }
??? }
}
$Porsche911 = new Roadster();
$Porsche911->SetSpeend(340);
$FuWaiStreet = new Street(80);
$FuWaiStreet->AddCar($Porsche911);
/**
?*
?* @result
?*
?* The Car is too fast and was not allowed on the road.[Finished in 0.1s]
?*
?*/
?>


2.對(duì)象接口

使用接口(interface),可以指定某個(gè)類必須實(shí)現(xiàn)哪些方法,但不需要定義這些方法的具體內(nèi)容。

接口是通過 interface 關(guān)鍵字來定義的,就像定義一個(gè)標(biāo)準(zhǔn)的類一樣,但其中定義所有的方法都是空的。

接口中定義的所有方法都必須是公有,這是接口的特性。

接口是一種類似于類的結(jié)構(gòu),可用于聲明實(shí)現(xiàn)類所必須聲明的方法。例如,接口通常用來聲明API,而不用定義如何實(shí)現(xiàn)這個(gè)API。

大多數(shù)開發(fā)人員選擇在接口名稱前加上大寫字母I作為前綴,以便在代碼和生成的文檔中將其與類區(qū)別開來。

2.1接口實(shí)現(xiàn)(implements)

要實(shí)現(xiàn)一個(gè)接口,使用 implements 操作符(繼承抽象類需要使用 extends 關(guān)鍵字不同),類中必須實(shí)現(xiàn)接口中定義的所有方法,否則會(huì)報(bào)一個(gè)致命錯(cuò)誤。類可以實(shí)現(xiàn)多個(gè)接口,用逗號(hào)來分隔多個(gè)接口的名稱。

實(shí)現(xiàn)多個(gè)接口時(shí),接口中的方法不能有重名。
接口也可以繼承,通過使用 extends 操作符。
類要實(shí)現(xiàn)接口,必須使用和接口中所定義的方法完全一致的方式。否則會(huì)導(dǎo)致致命錯(cuò)誤。
接口中也可以定義常量。接口常量和類常量的使用完全相同,但是不能被子類或子接口所覆蓋。
2.2使用接口的案例

復(fù)制代碼 代碼如下:

abstract class Car
{???
??? abstract function SetSpeend($speend = 0);
}
interface ISpeendInfo
{
??? function GetMaxSpeend();
}
class Roadster extends Car implements ISpeendInfo
{
??? public $Speend;
??? public function SetSpeend($speend = 0)
??? {
??????? $this->Speend = $speend;
??? }
??? public function getMaxSpeend()
??? {
??????? return $this->Speend;
??? }
}
class Street
{
??? public $Cars ;
??? public $SpeendLimit ;
??? function __construct( $speendLimit = 200)
??? {
??????? $this -> SpeendLimit = $speendLimit;
??????? $this -> Cars = array();
??? }
??? protected function IsStreetLegal($car)
??? {
??????? if ($car->getMaxSpeend() < $this -> SpeendLimit)
??????? {
??????????? return true;
??????? }
??????? else
??????? {
??????????? return false;
??????? }
??? }
??? public function AddCar($car)
??? {
??????? if($this->IsStreetLegal($car))
??????? {
??????????? echo 'The Car was allowed on the road.';
??????????? $this->Cars[] = $car;
??????? }
??????? else
??????? {
??????????? echo 'The Car is too fast and was not allowed on the road.';
??????? }
??? }
}

$Porsche911 = new Roadster();
$Porsche911->SetSpeend(340);
$FuWaiStreet = new Street(80);
$FuWaiStreet->AddCar($Porsche911);
/**
?*
?* @result
?*
?* The Car is too fast and was not allowed on the road.[Finished in 0.1s]
?*
?*/
?>

3、類型運(yùn)算符 instanceof

instanceof 運(yùn)算符是 PHP5 中的一個(gè)比較操作符。他接受左右兩邊的參數(shù),并返回一個(gè)boolean值。

確定一個(gè) PHP 變量是否屬于某個(gè)一類 CLASS 的實(shí)例
檢查對(duì)象是不是從某個(gè)類型繼承
檢查對(duì)象是否屬于某個(gè)類的實(shí)例
確定一個(gè)變量是不是實(shí)現(xiàn)了某個(gè)接口的對(duì)象的實(shí)例

復(fù)制代碼 代碼如下:

echo $Porsche911 instanceof Car;
//result:1

echo $Porsche911 instanceof ISpeendInfo;
//result:1

4.Contract Programming

Design by Contract or Design by Contract (DbC) is a method of designing computer software. This method requires software designers to define formal, precise and verifiable interfaces for software components. In this way, a priori conditions, a posteriori conditions and invariants are added to traditional abstract data types. The "contract" or "contract" used in the name of this method is a metaphor because it is somewhat similar to the situation of a business contract.

A programming practice of implementing a declared interface before writing a class. This method is very useful in ensuring the encapsulation of classes. Using contract programming techniques, we can define the functionality of a view before creating an application, much like an architect draws a blueprint before building a building.

5.Summary

Abstract classes are classes declared using the abstract keyword. By marking a class as abstract, we can defer implementation of the declared methods. To declare a method as abstract, simply remove the method entity containing all curly braces and end the line of code where the method is declared with a semicolon.

Abstract classes cannot be instantiated directly, they must be inherited.

If a class inherits from an abstract class, it must also be declared abstract when it does not implement all abstract methods declared in the base class.

In an interface, we can declare a method prototype without a method body, which is very similar to an abstract class. The difference between them is that interfaces cannot declare any methods with method bodies; and the syntax they use is also different. In order to force uncovering rules on a class, we need to use the implements keyword instead of the extends keyword.

In some cases we need to determine whether a class is a type of a specific class, or whether it implements a specific interface. instanceof is suitable for this task. instanceof checks three things: whether the instance is of a specific type, whether the instance inherits from a specific type, and whether the instance or any of its ancestor classes implement a class-specific interface.

Some languages ??have the ability to inherit from multiple classes, this is called multiple inheritance. PHP does not support multiple inheritance. The idea is that it provides the function of declaring multiple interfaces for a class.

Interfaces are useful for declaring rules that a class must follow. Contractual programming technology uses this feature to enhance encapsulation and optimize workflow.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/788637.htmlTechArticle1. Abstract classes in PHP PHP 5 supports abstract classes and abstract methods. Classes defined as abstract cannot be instantiated. Any class, if at least one method in it is declared abstract...
Statement of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
Application of interfaces and abstract classes in design patterns in Java Application of interfaces and abstract classes in design patterns in Java May 01, 2024 pm 06:33 PM

Interfaces and abstract classes are used in design patterns for decoupling and extensibility. Interfaces define method signatures, abstract classes provide partial implementation, and subclasses must implement unimplemented methods. In the strategy pattern, the interface is used to define the algorithm, and the abstract class or concrete class provides the implementation, allowing dynamic switching of algorithms. In the observer pattern, interfaces are used to define observer behavior, and abstract or concrete classes are used to subscribe and publish notifications. In the adapter pattern, interfaces are used to adapt existing classes. Abstract classes or concrete classes can implement compatible interfaces, allowing interaction with original code.

Does golang have abstract classes? Does golang have abstract classes? Jan 06, 2023 pm 07:04 PM

Golang has no abstract classes. Golang is not an object-oriented (OOP) language. It has no concepts of classes, inheritance, and abstract classes. However, there are structures (structs) and interfaces (interfaces) in golang, which can be indirectly implemented through the combination of struct and interface. Abstract classes in object languages.

What is the difference between interfaces and abstract classes in PHP? What is the difference between interfaces and abstract classes in PHP? Jun 04, 2024 am 09:17 AM

Interfaces and abstract classes are used to create extensible PHP code, and there is the following key difference between them: Interfaces enforce through implementation, while abstract classes enforce through inheritance. Interfaces cannot contain concrete methods, while abstract classes can. A class can implement multiple interfaces, but can only inherit from one abstract class. Interfaces cannot be instantiated, but abstract classes can.

What is the difference between an abstract class and an interface in PHP? What is the difference between an abstract class and an interface in PHP? Apr 08, 2025 am 12:08 AM

The main difference between an abstract class and an interface is that an abstract class can contain the implementation of a method, while an interface can only define the signature of a method. 1. Abstract class is defined using abstract keyword, which can contain abstract and concrete methods, suitable for providing default implementations and shared code. 2. The interface is defined using the interface keyword, which only contains method signatures, which is suitable for defining behavioral norms and multiple inheritance.

What does instanceof do? What does instanceof do? Nov 14, 2023 pm 03:50 PM

The function of instanceof is to determine whether an object is an instance of a certain class, or whether it implements a certain interface. instanceof is an operator used to check whether an object is of a specified type. Usage scenarios of instanceof operator: 1. Type checking: can be used to determine the specific type of an object, so as to perform different logic according to different types; 2. Interface judgment: can be used to determine whether an object implements an interface, so as to determine whether an object implements an interface. The definition of the interface calls the corresponding method; 3. Downward transformation, etc.

An in-depth discussion of the similarities and differences between Golang functional interfaces and abstract classes An in-depth discussion of the similarities and differences between Golang functional interfaces and abstract classes Apr 20, 2024 am 09:21 AM

Both functional interfaces and abstract classes are used for code reusability, but they are implemented in different ways: functional interfaces through reference functions, abstract classes through inheritance. Functional interfaces cannot be instantiated, but abstract classes can. Functional interfaces must implement all declared methods, while abstract classes can only implement some methods.

Inner class implementation of interfaces and abstract classes in Java Inner class implementation of interfaces and abstract classes in Java Apr 30, 2024 pm 02:03 PM

Java allows inner classes to be defined within interfaces and abstract classes, providing flexibility for code reuse and modularization. Inner classes in interfaces can implement specific functions, while inner classes in abstract classes can define general functions, and subclasses provide concrete implementations.

Java Interfaces and Abstract Classes: The Road to Programming Heaven Java Interfaces and Abstract Classes: The Road to Programming Heaven Mar 04, 2024 am 09:13 AM

Interface: An implementationless contract interface defines a set of method signatures in Java but does not provide any concrete implementation. It acts as a contract that forces classes that implement the interface to implement its specified methods. The methods in the interface are abstract methods and have no method body. Code example: publicinterfaceAnimal{voideat();voidsleep();} Abstract class: Partially implemented blueprint An abstract class is a parent class that provides a partial implementation that can be inherited by its subclasses. Unlike interfaces, abstract classes can contain concrete implementations and abstract methods. Abstract methods are declared with the abstract keyword and must be overridden by subclasses. Code example: publicabstractcla

See all articles