Home php教程 php手册 PHP 面向对象 final类与final方法_php基础

PHP 面向对象 final类与final方法_php基础

May 17, 2016 am 09:01 AM
final method final class

final---用于类、方法前。
final类---不可被继承。
final方法---不可被覆盖。
final类不能被继承。
如果我们不希望一个类被继承,我们使用final来修饰这个类。这个类将无法被继承。比如我们设定的Math类,涉及了我们要做的数学计算方法,这些算法也没有必要修改,也没有必要被继承,我们把它设置成final类型。

复制代码 代码如下:


//声明一个final类Math
final class Math{
public static $pi = 3.14;

public function __toString(){
return "这是Math类。";
}
}
$math = new Math();
echo $math;

//声明类SuperMath 继承自 Math类
class SuperMath extends Math {
}
//执行会出错,final类不能被继承。

?>

程序运行结果

复制代码 代码如下:
Fatal error: Class SuperMath may not inherit from final class (Math) in E:\PHPProjects\test.php on line 14


final方法不能被重写
如果不希望类中的某个方法被子类重写,我们可以设置这个方法为final方法,只需要在这个方法前加上final修饰符。

如果这个方法被子类重写,将会出现错误。
复制代码 代码如下:


//声明一个final类Math
class Math{
public static $pi = 3.14;
public function __toString(){
return "这是Math类。";
}
public final function max($a,$b){
return $a > $b ? $a : $b ;
}
}
//声明类SuperMath 继承自 Math类
class SuperMath extends Math {
public final function max($a,$b){}
}
//执行会出错,final方法不能被重写。

?>


程序运行结果

复制代码 代码如下:
Fatal error: Class SuperMath may not inherit from final class (Math) in E:\PHPProjects\test.php on line 16
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

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)

What are the techniques for implementing forced inheritance of proxy final classes in Java programming? What are the techniques for implementing forced inheritance of proxy final classes in Java programming? Sep 06, 2023 am 10:28 AM

What are the techniques for implementing forced inheritance of proxy final classes in Java programming? In Java programming, a final class refers to a class that cannot be inherited. This limitation sometimes causes some confusion, especially when we want to extend based on a final class. However, there is a technique to achieve forced inheritance of proxy final classes. This article introduces this technique and demonstrates it with code examples. To understand the technique of forced inheritance to proxy final classes, first we need to clarify the concept of final classes.

How to use forced inheritance to proxy final classes in Java to increase code scalability? How to use forced inheritance to proxy final classes in Java to increase code scalability? Sep 06, 2023 pm 01:07 PM

How to use forced inheritance to proxy final classes in Java to increase code scalability? In traditional object-oriented programming, we often use inheritance to achieve code reuse and extension. However, there is a special class in the Java language, the final class, which prohibits other classes from inheriting it. This is very useful for situations where you need to limit the behavior of a class or protect the implementation details of a class, but it also brings certain challenges to the scalability of the code. In such cases, we can use forced inheritance proxies to bypass final

How to use forced inheritance proxy final class to reduce the coupling of code in Java programming? How to use forced inheritance proxy final class to reduce the coupling of code in Java programming? Sep 06, 2023 am 08:40 AM

How to use forced inheritance proxy final class to reduce the coupling of code in Java programming? In Java programming, we often face the problem of excessive code coupling. High coupling means that one class depends on the specific implementation details of another class, which makes our code difficult to maintain and extend. In order to solve this problem, we can use the technique of forced inheritance of the proxy final class to reduce the coupling of the code. This article explains how to use this technique and illustrates it with code examples. First, we need to understand what f is

How to use forced inheritance proxy final class to solve common design problems in Java programming? How to use forced inheritance proxy final class to solve common design problems in Java programming? Sep 06, 2023 am 08:33 AM

How to use forced inheritance proxy final class to solve common design problems in Java programming? In Java programming, final classes are designed as classes that cannot be inherited. However, sometimes we may need to extend a final class to solve some design problems. In this case, we can use forced inheritance of agents to solve this problem. This article explains how to use forced inheritance to proxy final classes and provides code examples. 1. Problem background In Java, the final keyword is used to

How to use forced inheritance to proxy final classes in Java to increase the readability of code? How to use forced inheritance to proxy final classes in Java to increase the readability of code? Sep 06, 2023 am 11:57 AM

How to use forced inheritance to proxy final classes in Java to increase the readability of code? Introduction: In Java programming, we often encounter the need to extend existing classes. However, with final classes, we cannot directly inherit from them, which limits our freedom when extending and modifying these classes. In order to solve this problem, we can use forced inheritance of proxy final classes to increase the readability and maintainability of the code. This article explains how to use this technique and provides relevant sample code. 1. What

How to use forced inheritance to proxy final classes in Java to improve code reusability? How to use forced inheritance to proxy final classes in Java to improve code reusability? Sep 06, 2023 pm 01:15 PM

How to use forced inheritance to proxy final classes in Java to improve code reusability? In Java programming, we often encounter situations where we need to reuse a certain class. At this time, code reusability becomes very important. Normally, we can achieve code reuse through inheritance, but in some special cases, the inherited class may be declared as a final class and cannot be inherited. So, do we have other ways to achieve code reuse? The answer is yes - use forced inheritance to proxy final

How to use forced inheritance to proxy final classes in Java to protect code security? How to use forced inheritance to proxy final classes in Java to protect code security? Sep 06, 2023 pm 03:57 PM

How to use forced inheritance to proxy final classes in Java to protect code security? Introduction: In Java, the final keyword is used to modify classes, methods and variables. When a class is declared final, it means that the class is not inheritable, that is, other classes cannot inherit from the class. However, in some cases, we may need to allow other classes to inherit our class, but do not want other classes to directly access or modify some critical code. At this time, we can use the method of forced inheritance of the proxy final class to protect the code.

How to use forced inheritance to proxy final classes in Java to achieve better code maintenance and upgrades? How to use forced inheritance to proxy final classes in Java to achieve better code maintenance and upgrades? Sep 06, 2023 am 09:43 AM

How to use forced inheritance to proxy final classes in Java to achieve better code maintenance and upgrades? Introduction: In Java programming, we often encounter situations where we need to inherit and rewrite some classes. However, sometimes the class we want to inherit is declared final and cannot be inherited, which brings certain troubles to code maintenance and upgrades. This article will introduce a solution to achieve better code maintenance and upgrades by forcing inheritance of proxy final classes. Text: In Java, if a class is declared as

See all articles