<?php

interface ICanEat{
public function eat($food);
}

class Human implements ICaneat{

public function eat($food){

echo "Human eating".$food."\n";

}

}

class Animal implements ICaneat{

public function eat($food){

echo "Animal eating".$food."\n";

}

}

// stay ICanEat Under interface eat() Method , stay Human Classes and Animal The result of implementation in class is different , This is polymorphism

$person = new Human();

$person->eat('apple');

$animal = new Animal();

$animal->eat('banana');

function checkeat($person){

if($person instanceof ICanEat){

$persong->eat('food');

}else{

echo "not find";

}

}

// The same line of code , When passing in implementation objects of different interfaces , The performance is different , This is polymorphism

checkeat($person);

checkeat($animal);

The output is zero

Human eating food

Animal eating food

?>

php note ( 7、 ... and )PHP More articles on Polymorphism of objects

  1. ( turn )Qt Model/View Learning notes ( 7、 ... and )——Delegate class

    Qt Model/View Learning notes ( 7、 ... and ) Delegate  class Concept And MVC Different modes ,model/view Structure has no completely separate components for interacting with users . In general , view Responsible for presenting the data To the user , also ...

  2. python cookbook The third edition of study notes 10 : Classes and objects ( One )

    Classes and objects : We often print an object to get some information about it . class pair:     def __init__(self,x,y):         self.x=x         self. ...

  3. php note ( Two )PHP Class and object Static Static keywords

      PHP Class and object Static Static keywords Static properties and methods can be called without instantiating a class , Use the class name directly :: Method name . Static properties do not allow objects to use -> Operator call . class Car { pr ...

  4. [javase Learning notes ]-6.2 The relationship between classes and objects

    In this section, let's look at the relationship between classes and objects . We learn java Language , The purpose is to use java Language describes things in real life . So how do we describe narration . This leads to the class , When we actually implement it , It is embodied in the form of class . ...

  5. delphi Experts break through the nature of object-oriented classes and objects in learning notes

    Knowledge point 1: Heap and stack There are two types of memory available to each application : Pile up (heap) And the stack (stack). Heaps are also called “ Free storage area ”, The allocation and release of memory space must be controlled by the programmer . for example , use GetMem Function gets a ...

  6. delphi Experts break through the nature of object-oriented classes and objects in learning notes ( There's a compilation of explanations good)

    Knowledge point 1: Heap and stack There are two types of memory available to each application : Pile up (heap) And the stack (stack). Heaps are also called “ Free storage area ”, The allocation and release of memory space must be controlled by the programmer . for example , use GetMem Function gets a ...

  7. php note ( 6、 ... and )PHP Object interface between class and object

    Interface implementation <?php //interface Keyword is used to define an interface interface ICanEat{ // The methods in the interface don't need to be implemented public function eat($food); ...

  8. php note ( 5、 ... and )PHP Advanced features of objects of classes and objects

    Object comparison , When all properties of two instances of the same class are equal , You can use the comparison operator == Judge , When you need to determine whether two variables are references to the same object , You can use the congruent operator === Judge . class Car { } $a = ...

  9. php note ( 3、 ... and )PHP Access control for classes and objects

    Access control through keywords public,protected and private To achieve . Class members defined as public can be accessed anywhere . Class members defined as protected can be accessed by themselves and their subclasses and parents . Class members defined as private can only ...

Random recommendation

  1. bye OI

    NOIP2016 The final chapter I'm weak, but I'm also mentally handicapped and hand handicapped bye OI Hello, college entrance examination Hello tomorrow " The days without tears will be much easier But my heart still hurts " ------------------------------ ...

  2. python Built in functions 3

    int( [x[, radix]]) Convert to a string or number to a pure integer . If the parameter is a string , It must contain a possibly signed decimal number as a Python whole Count , It's possible to embed spaces . With radix The cardinality given by the parameter is the base ...

  3. Analyze a C Assembly code generated by a language program -《Linux Kernel analysis 》Week1 Homework

    Signature information Guo Chunyang Please indicate the source of original works reprinted :<Linux Kernel analysis >MOOC Course http://mooc.study.163.com/course/USTC-1000029000 C Source code this ...

  4. SOM Self organizing mapping networks course

    summary SOM It's a Finnish professor Teuvo Kohonen A neural network algorithm is proposed , It provides a way to represent high-dimensional data in low dimensional space ( It's usually one-dimensional or two-dimensional ). The process of reducing vector dimensions , It's called vector quantization (vector quantis ...

  5. C++ call C# Generated DLL All kinds of problems with documents

    C++ call C# Generated DLL file : First, choose to build a C# Class library of , Then write the required functions according to the requirements after , about C++ Call the process need to pay attention to a few : 1. Use #using <....some.dll> Pointed out that ...

  6. MmSystem Play Wav Format sound

    //MmSystem Play Wav Format sound  //MmSystem Support *.wav Sound format snd ->SoundRecorderuses MmSystem; // quote MmSystem// Play the system sound ...

  7. WPF The form is placed at the bottom of the desktop

    stay WPF Set the Topmost Property to put the form on top forever , But it didn't provide Bottommost Property to bottom the form . If you want to put the form at the bottom of the desktop , You need to use Windows API To implement the . The solution is as follows : 1 ...

  8. HDU2199,HDU2899,HDU1969,HDU2141--( Simple dichotomy )

    Dichotomy is a very effective strategy to reduce the time cost , I don't think it's reasonable to list two topics separately , Dichotomy should be considered as an optimization method These questions are simple, using the dichotomy method to optimize , Two points seems simple , But if you don't pay attention, you make mistakes . I'm writing two ...

  9. Write native Node.js modular

    Introduction : When Javascript We're having a performance bottleneck , Or need to be enhanced Javascript When you can , You need to rely on native Module to achieve . Application scenarios On a daily basis , We often need to put the original Node.js Module as a dependency and in the item ...

  10. Delphi establish ActiveX Control , Implementation of security interface and no interface code

    Delphi establish OCX Control is very convenient , but IE The security authentication pop-up when calling is very troublesome , Sometimes OCX And there's no interface ,IE You need to hide it when calling , Very inconvenient . stay DELPHI Created in OCX Implement the security interface and modify part of the code in creating events To achieve safety ...