Home Backend Development PHP Tutorial PHP面向对象详解(三)_PHP

PHP面向对象详解(三)_PHP

May 28, 2016 pm 01:12 PM
php object-oriented object-oriented

面向对象 对象概念是面向对象技术的核心。在显示世界里我们所面对的事情都是对象,如计算机、电视机、自行车等。在面向对象的程序设计中,对象是一个由信息及对信息进行处理的描述所组成的整体,是对现实世界的抽象.

对象的主要三个特性

对象的行为:可以对 对象施加那些操作,开灯,关灯就是行为。
对象的形态:当施加那些方法是对象如何响应,颜色,尺寸,外型。
对象的表示:对象的表示就相当于身份证,具体区分在相同的行为与状态下有什么不同。

面向对象模型

面向对象的概念:

oop(面向对象的编程)它能是其代码更加简洁易于维护并且具有更强的可重性

代码如下:


final public function test(){


依此类推,对于不想有子类的父类,在类名那里写上final

代码如下:


final class BaseClass{


代码如下:


//定义一个接口
interface ICanEat{
 public function eat($food);
}


可以看到,接口中并没有方法的具体实现,但必须有方法!

那么,下面是,“人类会吃”

//具体对象,连接到接口
class Human implements ICanEat{
public function eat($food){
 echo "Human eating ".$food.".<br/>";
 }
}
$obj=new Human();
$obj->eat("shit");
Copy after login

请忽略我给出的“食物”。

代码如下:


var_dump($obj instanceof ICanEat);


会返回boolean值。

--更多的栗子

interface ICanPee extends ICanEat{
 public function pee();
}
class Demon implements ICanPee{
 public function pee(){
 echo "Can demon pee&#63;";
 }
 public function eat($food){
 echo "Can demon eat ".$food;
 }
}
$ghost=new Demon();
$ghost->pee();
$ghost->eat("shit");
Copy after login

 接口本质上也是类,可以被继承/继承,但是使用继承接口的接口,所有父类、“爷类”的方法都要有具体实现。

--小结

/**
 * 接口
 * 1. 接口的基本概念和基本使用方法
 * 2. 接口里面的方法没有具体的实现
 * 3. 实现了某个接口的类必须提供接口中定义的方法
 * 4. 不能用接口创建对象,但是能够判断某个对象是否实现了某个接口
 * 5. 接口可以继承接口(interface extends interface)
 * 6. 接口中定义的所有方法都必须是公有,这是接口的特性。
 */

aaaaaaaaaaaaaa

bu xiang xie le....................

ming tian yao ge ..............

以上内容是小编给大家介绍的PHP面向对象详解(三),希望大家喜欢。

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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to implement object-oriented event-driven programming using Go language How to implement object-oriented event-driven programming using Go language Jul 20, 2023 pm 10:36 PM

How to use Go language to implement object-oriented event-driven programming Introduction: The object-oriented programming paradigm is widely used in software development, and event-driven programming is a common programming model that realizes the program flow through the triggering and processing of events. control. This article will introduce how to implement object-oriented event-driven programming using Go language and provide code examples. 1. The concept of event-driven programming Event-driven programming is a programming model based on events and messages, which transfers the flow control of the program to the triggering and processing of events. in event driven

What is the importance of @JsonIdentityInfo annotation using Jackson in Java? What is the importance of @JsonIdentityInfo annotation using Jackson in Java? Sep 23, 2023 am 09:37 AM

The @JsonIdentityInfo annotation is used when an object has a parent-child relationship in the Jackson library. The @JsonIdentityInfo annotation is used to indicate object identity during serialization and deserialization. ObjectIdGenerators.PropertyGenerator is an abstract placeholder class used to represent situations where the object identifier to be used comes from a POJO property. Syntax@Target(value={ANNOTATION_TYPE,TYPE,FIELD,METHOD,PARAMETER})@Retention(value=RUNTIME)public

Explore object-oriented programming in Go Explore object-oriented programming in Go Apr 04, 2024 am 10:39 AM

Go language supports object-oriented programming through type definition and method association. It does not support traditional inheritance, but is implemented through composition. Interfaces provide consistency between types and allow abstract methods to be defined. Practical cases show how to use OOP to manage customer information, including creating, obtaining, updating and deleting customer operations.

PHP Advanced Features: Best Practices in Object-Oriented Programming PHP Advanced Features: Best Practices in Object-Oriented Programming Jun 05, 2024 pm 09:39 PM

OOP best practices in PHP include naming conventions, interfaces and abstract classes, inheritance and polymorphism, and dependency injection. Practical cases include: using warehouse mode to manage data and using strategy mode to implement sorting.

Analyzing the Flyweight Pattern in PHP Object-Oriented Programming Analyzing the Flyweight Pattern in PHP Object-Oriented Programming Aug 14, 2023 pm 05:25 PM

Analyzing the Flyweight Pattern in PHP Object-Oriented Programming In object-oriented programming, design pattern is a commonly used software design method, which can improve the readability, maintainability and scalability of the code. Flyweight pattern is one of the design patterns that reduces memory overhead by sharing objects. This article will explore how to use flyweight mode in PHP to improve program performance. What is flyweight mode? Flyweight pattern is a structural design pattern whose purpose is to share the same object between different objects.

Are there any class-like object-oriented features in Golang? Are there any class-like object-oriented features in Golang? Mar 19, 2024 pm 02:51 PM

There is no concept of a class in the traditional sense in Golang (Go language), but it provides a data type called a structure, through which object-oriented features similar to classes can be achieved. In this article, we'll explain how to use structures to implement object-oriented features and provide concrete code examples. Definition and use of structures First, let's take a look at the definition and use of structures. In Golang, structures can be defined through the type keyword and then used where needed. Structures can contain attributes

Analysis of object-oriented features of Go language Analysis of object-oriented features of Go language Apr 04, 2024 am 11:18 AM

The Go language supports object-oriented programming, defining objects through structs, defining methods using pointer receivers, and implementing polymorphism through interfaces. The object-oriented features provide code reuse, maintainability and encapsulation in the Go language, but there are also limitations such as the lack of traditional concepts of classes and inheritance and method signature casts.

C# development experience sharing: object-oriented programming and design principles C# development experience sharing: object-oriented programming and design principles Nov 22, 2023 am 08:18 AM

C# (CSharp) is a powerful and popular object-oriented programming language that is widely used in the field of software development. During the C# development process, it is very important to understand the basic concepts and design principles of object-oriented programming (OOP). Object-oriented programming is a programming paradigm that abstracts things in the real world into objects and implements system functions through interactions between objects. In C#, classes are the basic building blocks of object-oriented programming and are used to define the properties and behavior of objects. When developing C#, there are several important design principles

See all articles