1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-24 07:02:15 +01:00
Files
archived-doc-zh/language/oop5/final.xml
2024-10-16 15:25:06 +08:00

133 lines
3.1 KiB
XML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 16f66c05a4060a7d673ae1c70b656d65009407b0 Maintainer: Haohappy Status: ready -->
<!-- CREDITS: Luffy -->
<sect1 xml:id="language.oop5.final" xmlns="http://docbook.org/ns/docbook">
<title>Final 关键字</title>
<para>
final 关键字通过在定义方法、属性和常量之前加上 <literal>final</literal> 来防止被子类覆盖。
如果一个类被声明为 final则不能被继承。
</para>
<para>
<example>
<title>Final 方法示例</title>
<programlisting role="php">
<![CDATA[
<?php
class BaseClass {
public function test() {
echo "BaseClass::test() called\n";
}
final public function moreTesting() {
echo "BaseClass::moreTesting() called\n";
}
}
class ChildClass extends BaseClass {
public function moreTesting() {
echo "ChildClass::moreTesting() called\n";
}
}
// 产生 Fatal error: Cannot override final method BaseClass::moreTesting()
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>Final 类示例</title>
<programlisting role="php">
<![CDATA[
<?php
final class BaseClass {
public function test() {
echo "BaseClass::test() called\n";
}
// 由于类已经是 final所以 final 关键字是多余的
final public function moreTesting() {
echo "BaseClass::moreTesting() called\n";
}
}
class ChildClass extends BaseClass {
}
// 产生 Fatal error: Class ChildClass may not inherit from final class (BaseClass)
?>
]]>
</programlisting>
</example>
</para>
<example>
<title>PHP 8.4.0 起可用的 final 属性示例</title>
<programlisting role="php">
<![CDATA[
<?php
class BaseClass {
final protected string $test;
}
class ChildClass extends BaseClass {
public string $test;
}
// Results in Fatal error: Cannot override final property BaseClass::$test
?>
]]>
</programlisting>
</example>
<example xml:id="language.oop5.final.example.php81">
<title>PHP 8.1.0 起可用的 final 常量示例</title>
<programlisting role="php">
<![CDATA[
<?php
class Foo
{
final public const X = "foo";
}
class Bar extends Foo
{
public const X = "bar";
}
// Fatal error: Bar::X cannot override final constant Foo::X
?>
]]>
</programlisting>
</example>
<note>
<simpara>
从 PHP 8.0.0 起,除了<link linkend="language.oop5.decon.constructor">构造函数</link>之外,私有方法也不能声明为 final 。
</simpara>
<simpara>
声明为 <link linkend="language.oop5.visibility-members-aviz"><literal>private(set)</literal></link> 的属性是隐式的 <literal>final</literal>
</simpara>
</note>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->