PHP面向对象编程学习之一
继承
1、父类里面定义的类成员可以不用在子类中重复定义,节约了编程时间和代价
2、同一个父类的子类拥有相同的父类定义的类成员,因此外部代码调用他们的时候可以一视同仁。
3、子类可以修改和调整父类定义的类成员
<?php class Animal { private $weight; public function getWeight() { return $this->weight; } public function setWeight($w) { $this->weight = $w; } } class Dog extends Animal { /** *子类新增方法 */ public function Bark() { echo "Wang~~Wang~~~ "; } } $myDog = new Dog(); $myDog->setWeight(20); echo "Mydog's weight is ".$myDog->getWeight().'<br/>'; $myDog->Bark(); ?>
1、面向对象的三种权限
(1)public:共有的类成员,可以在任课地方访问
(2)protected:受保护的类成员,可以被其自身以及其子类访问
(3)private:私有的类成员,只能被自身访问。
静态关键词(static)
1、静态属性用于保存类的共有数据
2、静态方法里面只能访问静态属性
3、静态成员不需要实例化对象就可以访问
4、类的内部可以通过self或者是static关键词访问自身静态成员
5、通过parent关键字可以访问父类的静态成员
6、可以通过类的名称在类定义外部访问静态成员
final成员
1、对于不想被任何类继承的类可以在class之前添加final关键字
2、对于不想被子类重写的方法,可以在方法定义的前面添加final关键字
数据访问
1、parent关键词可以用于访问父类中被子类重写的方法
2、self关键字可以访问类自身的成员方法,也可以用于访问自身的静态成员和类常量;不能用于访问类自身的属性;使用常量的时候不需要再常量名称前面添加'$'符号
3、static关键字用于访问类自身定义的的静态成员,访问静态属性时需要在属性前面添加‘$’符号。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The reason for the error is NameResolutionError(self.host,self,e)frome, which is an exception type in the urllib3 library. The reason for this error is that DNS resolution failed, that is, the host name or IP address attempted to be resolved cannot be found. This may be caused by the entered URL address being incorrect or the DNS server being temporarily unavailable. How to solve this error There may be several ways to solve this error: Check whether the entered URL address is correct and make sure it is accessible Make sure the DNS server is available, you can try using the "ping" command on the command line to test whether the DNS server is available Try accessing the website using the IP address instead of the hostname if behind a proxy

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

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

Before introducing the usage of self in Python, let’s first introduce the classes and instances in Python. We know that the most important concepts of object-oriented are classes and instances. Classes are abstract templates, such as abstract things like students. , can be represented by a Student class. Instances are specific "objects" created based on classes. Each object inherits the same methods from the class, but its data may be different. 1. Take the Student class as an example. In Python, the class is defined as follows: classStudent(object):pass(Object) indicates which class the class inherits from. The Object class is all

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.

Inobject-orientedprogramming,inheritanceallowsustocreatenewclassesthatinheritthepropertiesandmethodsofanexistingclass.Thispowerfulconceptenablescodereuse,modularity,andextensibilityinourprograms.Beforedivingintoaccessingparentclassattributes,let'shav

The role and usage of static in C language: 1. Variable scope; 2. Life cycle; 3. Internal function; 4. Modify global variables; 5. Modify function; 6. Other uses; Detailed introduction: 1. Variable scope, when If there is the static keyword before a variable, then the scope of the variable is limited to the file in which it is declared. In other words, the variable is a "file-level scope", which is very useful for preventing the "duplicate definition" problem of variables; 2. Life cycle, static variables are initialized once when the program starts executing, and destroyed when the program ends, etc.

1. static Please look at the following program first: publicclassHello{publicstaticvoidmain(String[]args){//(1)System.out.println("Hello, world!");//(2)}} Have seen this Segment programs are familiar to most people who have studied Java. Even if you have not learned Java but have learned other high-level languages, such as C, you should be able to understand the meaning of this code. It simply outputs "Hello, world" and has no other use. However, it shows the main purpose of the static keyword.
