Class07
Overwrite (rewrite) and final keywords
Overwrite (rewrite): Writes a function that is exactly the same as the parent function in a subclass, implementing subclass-specific functionality
Final keyword: Final translation comes to mean "final". Final defined function or class, cannot be overridden by the quilt class, if overridden will error
<?Phpdate_default_timezone_set ("PRC");/** rewrite and final * 1. A method in a subclass that exactly matches the parent class can complete the override of the parent class method * 2. For classes that do not want to be inherited by any class, you can add the final keyword * 3 to the class. For methods that do not want the quilt class to be rewritten (overwrite, modified), you can To add the final keyword before the method definition*/classBaseClass { Public functionTest () {Echo"Baseclass::test () called\n"; } Public functionmoretesting () {Echo"Baseclass::moretesting () called\n"; }}classChildClassextendsBaseClass {//parameters are not necessarily exactly the same as the parent class when overriding Public functionMoretesting ($tmp=NULL) { Echo"Childclass::moretesting () called\n"; }}//Results in Fatal error:cannot override final Method baseclass::moretesting ()$obj=NewChildClass ();$obj-moretesting ();?>
CLASS08 Data Access Supplement
* 1. The parent keyword can be used to invoke a class member * 2 that is overridden by the parents class. The Self keyword can be used to access a member method of the class itself, or to access its own static members and class constants, not to access the properties of the class itself, or to access the class constants without adding the $ sign * 3 in front of the constant name . The static keyword is used to access statically defined members of the class, and it is necessary to add the $ symbol before the property name when accessing the static property
<?Phpdate_default_timezone_set ("PRC");/** * Data Access Supplement * 1. The parent keyword can be used to invoke a class member that is overridden by the parent class * 2. The Self keyword can be used to access a member method of the class itself, or to access its own static members and class constants; cannot be used to access properties of the class itself Access the class constant without adding the $ sign * 3 to the name of the constant. The static keyword is used to access statically defined members of the class, and it is necessary to add the $ symbol before the property name when accessing the static property*/classBaseClass { Public functionTest () {Echo"Baseclass::test () called\n"; } Public functionmoretesting () {Echo"Baseclass::moretesting () called\n"; }}classChildClassextendsBaseClass { Public $height= "198cm"; Private Static $sValue= "Static Value"; ConstConst_value = ' A constant VALUE '; Public functionmoretesting () {Echo"Childclass::moretesting () called\n"; Parent:: Moretesting ();//The parent keyword can access other members of the class itself that have the overridden member//step 3 self keywordSelf::called (); //the step 4 self keyword can access the constants defined by the class Echo"Const_value:". Self::const_value. " \ n "; //constants cannot be assigned to modify//Self::const_value = 11; The step 5 static keyword can be accessed by adding a $ symbol when a static property is called Echo"Svalue:".Static::$sValue." \ n "; } //Step 3 Public functioncalled () {Echo"Childclass::called () called\n"; } }//Results in Fatal error:cannot override final Method baseclass::moretesting ()$obj=NewChildClass ();$obj-moretesting ();?>
Class09 interface
Interfaces and classes differ in that the method of the interface definition is not implemented specifically, so it can be said that "a class that does not implement a specific method"; The method to implement the interface must be implemented by a generic class. The advantage of introducing an interface is that the function functions to be written are listed in the engineering development, which makes it easier for other developers to call
/** * Interface * 1. Basic concepts and basic usage of the interface * 2. The method inside the interface does not have a specific implementation * 3. A class that implements an interface must provide the method defined in the interface * 4. You cannot create an object with an interface, but you can tell if an object implements an interface * 5. Interfaces can inherit interfaces (interface extends interface) * 6. All methods defined in the interface must be public, which is the attribute of the interface. */
<?Phpdate_default_timezone_set ("PRC");/** * Interface * 1. Basic concept and basic usage of the interface * 2. The method in the interface does not have a specific implementation * 3. A class that implements an interface must provide the method defined in the interface * 4. You cannot create an object with an interface, but you can tell if an object implements an interface * 5. Interfaces can be Inherit interface (interface extends interface) * 6. All methods defined in the interface must be public, which is the attribute of the interface. */InterfaceIcaneat { Public functionEat$food);}//the human class implements the Icaneat interfaceclassHumanImplementsIcaneat {//is different from the implementation of the animal class. Public functionEat$food){ Echo"Human eating".$food. "\ n"; }}//the animal class implements the Icaneat interfaceclassAnimalImplementsIcaneat { Public functionEat$food){ Echo"Animal eating".$food. "\ n"; }}//Step1 Different classes can implement the same interface, defining different implementations of interface methods$man=NewHuman ();$man->eat ("Apple");$monkey=NewAnimal ();$monkey->eat ("Banana");//Step2 attempt to delete the human eat method and run//the class that implements an interface must provide the method defined in the interface//STEP3 cannot create an object with an interface, but can determine whether an object implements an interface//$EATOBJ = new Icaneat ();Var_dump($manInstanceof Icaneat);//determine if an object implements an interface//Step 4 interface can inherit interfaceInterfaceIcanpeeextendsIcaneat { Public functionpee ();}classHuman1Implementsicanpee{ Public functionpee () {}}?>
PHP Object-oriented Programming (IMOOC) code collection (iii)