Final---Used for classes, methods, and before.
The final class---cannot be inherited.
The final method---not be overwritten.
The final class cannot be inherited.
If we do not want a class to be inherited, we use final to modify the class. This class will not be inherited. For example, we set up the math class, which involves the mathematical calculation methods we want to do, the algorithms are not necessary to modify, and there is no need to be inherited, we set it to the final type.
Copy CodeThe code is as follows:
Declaring a final class math
Final class math{
public static $PI = 3.14;
Public Function __tostring () {
Return "This is the math class. ";
}
}
$math = new Math ();
Echo $math;
Declaring class Supermath inherits from the math class
Class Supermath extends Math {
}
Execution is an error and the final class cannot be inherited.
?>
Program Run Results
Copy CodeThe code is as follows: Fatal Error:class Supermath may not inherit from final Class (Math) in E:\PHPProjects\test.php on line 14
Final method cannot be overridden
If you do not want a method in the class to be overridden by the quilt class, we can set this method to be the final method, just precede the method with the final modifier.
If this method is overridden by the quilt class, an error will occur.
Copy CodeThe code is as follows:
Declaring a final class math
Class math{
public static $PI = 3.14;
Public Function __tostring () {
Return "This is the math class. ";
}
Public final function Max ($a, $b) {
Return $a > $b? $a: $b;
}
}
Declaring class Supermath inherits from the math class
Class Supermath extends Math {
Public final function Max ($a, $b) {}
}
Execution is an error and the final method cannot be overridden.
?>
Program Run Results
Copy CodeThe code is as follows: Fatal Error:class Supermath may not inherit from final Class (Math) in E:\PHPProjects\test.php on line 16
http://www.bkjia.com/PHPjc/321642.html www.bkjia.com true http://www.bkjia.com/PHPjc/321642.html techarticle Final---Used for classes, methods, and before. The final class---cannot be inherited. The final method---not be overwritten. The final class cannot be inherited. If we do not want a class to be inherited, we use fina ...