Home Backend Development PHP Tutorial Final journey of this life

Final journey of this life

Mar 04, 2021 am 10:51 AM
final

First of all, the division of transactions cannot be infinitely refined, so there is definitely no need for infinite subclasses. Once subclasses appear infinitely, it may not bring convenience in solving problems, but infinite consumption of memory resources. Therefore, PHP provides a termination mechanism so that classes cannot be inherited.

1. Basic syntax: final class class name

<?php
    final class Man{}//最终类
?>
Copy after login

2. The final class cannot be inherited

<?php

final class Man{}//最终类

class Man2 extends Man{}		//报错:无法从final类继承
?>
Copy after login

3.finalThe keyword not only modifies the class to indicate that the class cannot be inherited, but also modifies the method, indicating that the method cannot be overridden

<?php
//父类
class People{
    public function name(){}		//普通方法
    public final function age(){}	//最终方法
}
//子类
class Man extends People{
    //重写
    public function name(){}		//没问题
    public function age(){}		//致命错误:不能重写父类中的最终方法
}
?>
Copy after login

Summary: The final keyword modification represents the possibility of being unchangeable in variables, represents that it cannot be inherited in classes, and cannot be overridden in methods.

Recommended: php tutorial,php video tutorial

The above is the detailed content of Final journey of this life. For more information, please follow other related articles on the PHP Chinese website!

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 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)

The difference between final, finally, and finalize in Java The difference between final, finally, and finalize in Java Feb 19, 2024 pm 12:16 PM

The difference between final, finally, and finalize in Java requires specific code examples. In Java programming, you often encounter the three keywords final, finally, and finalize. Although they are spelled similarly, they have different meanings and usages. This article will explain the differences between these three keywords in detail and give code examples to help readers better understand. 1. Final keyword The final keyword can be used for classes, methods and variables. Its function is to make the modified class

In Java, is it possible to define a constant using only the final keyword? In Java, is it possible to define a constant using only the final keyword? Sep 20, 2023 pm 04:17 PM

A constant variable is a variable whose value is fixed and only one copy exists in the program. Once you declare a constant variable and assign a value to it, you cannot change its value again throughout the program. Unlike other languages, Java does not directly support constants. However, you can still create a constant by declaring a variable static and final. Static - Once you declare a static variable, they will be loaded into memory at compile time, i.e. only one copy will be available. Final - Once you declare a final variable, its value cannot be modified. Therefore, you can create a constant in Java by declaring the instance variable as static and final. Example Demonstration classData{&am

What is the function of java final keyword What is the function of java final keyword Nov 25, 2022 pm 04:26 PM

In Java, final can be used to modify classes, methods and variables. The final modified class means that the class cannot be inherited by any other class, which means that this class is a leaf class in an inheritance tree, and the design of this class has been considered perfect and does not need to be modified or extended. The method in the final modified class means that the class cannot be inherited by any other class and cannot be overridden; that is, the method is locked to prevent the inherited class from changing it. final modifies a variable in a class, indicating that the variable cannot be changed once it is initialized.

How are final objects created in Java? How are final objects created in Java? Apr 11, 2024 pm 02:00 PM

There are two ways to create final objects in Java: declaring a final variable or declaring a class using the final modifier. When a final variable is declared, the object is created through an initializer; when a final class is declared, the class instance is immutable. Importantly, references to final objects can still change, but the objects they point to are immutable.

final variable in java final variable in java Sep 24, 2023 pm 08:49 PM

Final variables can only be explicitly initialized once. A reference variable declared as Final can never be reassigned to refer to a different object. However, the data within the object can be changed. Therefore, the state of the object can change, but the reference cannot. For variables, the final modifier is usually used with static to make the constant a class variable. Example publicclassTest{ finalintvalue=10; //Thefollowingareexamplesofdeclaringconstants: &a

Special syntax in PHP: Static, Final, Abstract and other keywords Special syntax in PHP: Static, Final, Abstract and other keywords May 11, 2023 pm 04:00 PM

PHP is a popular open source server-side scripting language widely used in web development. The PHP language is not only easy to learn and use, but also supports a variety of programming paradigms, object-oriented programming, functional programming, etc. In PHP, there are some special syntax keywords, such as Static, Final, Abstract, etc. These keywords have special functions in object-oriented programming. This article will introduce these keywords in detail. Static keyword In PHP, the Static keyword has two uses

How many non-access modifiers are there in Java? How many non-access modifiers are there in Java? Aug 30, 2023 pm 06:01 PM

Java provides some other modifiers to provide functionality beyond visibility. These modifiers are called non-access modifiers. Static Members declared as static are common to all instances of the class. Static members are class-level members that are stored in class memory. Final This modifier is used to restrict further modifications to a variable, method, or class. The value of a variable declared final cannot be modified once it obtains its value. The Final method cannot be overridden in a subclass, nor can a subclass of the Final class be created. Abstract This modifier can be used with a class or method. You cannot apply this modifier to variables and constructors. Methods declared abstract must be modified in subclasses. You cannot instantiate a class declared abstract. Synchronous This modifier is used to control multiple threads

Java Error: Abuse of final keyword, how to solve and avoid it Java Error: Abuse of final keyword, how to solve and avoid it Jun 24, 2023 pm 09:00 PM

As a very popular programming language, Java is widely used in the Internet, mobile devices and other fields. For Java developers, they often encounter some errors or problems, one of which is the abuse of the final keyword. In Java, the final keyword is often used to modify variables, methods, classes, etc. It indicates that the attribute cannot be changed after it is defined. The final keyword can help developers ensure the immutability of objects and avoid problems such as race conditions, but misuse of the final keyword

See all articles