In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how php uses class inheritance to solve the problem of code duplication, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
The specific analysis is as follows:
Inheritance, to put it bluntly, is to create one or more subclasses for a class. To create a subclass, you must use the extends keyword in the class declaration, with the new class name first, extends in, and the parent class name after.
In the following example, we create two new classes, BookProduct and Cdproduct, both of which inherit from the ShopProduct class.
Because no constructor is defined in the subclass, the constructor of the parent class ShopProduct is automatically called when the BookProduct and Cdproduct classes are instantiated.
By default, the subclass inherits all the public and protected methods and properties of the parent class (but not the private methods and properties, which we'll talk about later). That is, we can call the getProducer () method on an object instantiated from the Cdproduct class, even though getProducer () is defined in the ShopProduct class.
Add the following code to it:
$product2 = new CdProduct ("PHP object-oriented", "Guo", "Bowl and ladle basin", 7 null, "7 hours"); print "good Life: {$product2-> getProducer ()}"
"; / / the result is: the good life: Guo Wanpan
Both subclasses inherit the public part of the parent class, but note that both the BookProduct and Cdproduct classes override the getSummaryLine () method, providing their own unique implementation that indicates that subclasses can extend and modify the functionality of the parent class.
However, the implementation of this method in the parent class seems a bit redundant because both of its subclasses override the method, but other subclasses may use its basic functionality. The existence of this method provides assurance for the client code: all ShopProduct objects will have a getSummaryLine () method, and both BookProduct and Cdproduct will use their own getSummaryLine () methods to access the $title property.
Maybe at first, inheritance is a concept that is not easy to understand. First of all, we can know that by defining a class that inherits from other classes, we ensure that a class has its free functionality and the functionality of its parent class. Then there is the "search" function of the subclass. when we call $product2-> getProducer (), we don't find the getProducer () method in the CdProduct class, so find out if there is this method in the ShopProduct class, call it if you have it, and report an error if you don't. The same is true for access to attributes.
If you look at the constructor of ShopProduct, you can see that we still manage data in the base class (parent class) that is supposed to be handled by subclasses: BookProduct should handle $numPages parameters and attributes; Cdproduct should handle $playLength parameters and attributes. To do this, we need to define the constructor separately in the subclass.
That's all of the article "how php uses class inheritance to solve the problem of code duplication". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.