Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the three object-oriented features of python?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you what are the three major object-oriented features of python. I hope you will gain something after reading this article. Let's discuss it together.

I. introduction of the three major features of object-oriented

Python is an object-oriented language, and it also supports three major features of object-oriented programming: inheritance, encapsulation (hiding) and polymorphism.

1. Encapsulate (hide)

Hide the properties and implementation details of the object, and provide only the necessary methods.

Through the "private properties, private methods" way, to achieve "encapsulation". Python pursues concise grammar

2. Inheritance

Inheritance can make subclasses have the characteristics of parent classes and improve the reusability of the code. The design is a kind of incremental evolution. When the original parent class design remains unchanged, new functions can be added or existing algorithms can be improved.

3. Polymorphism

Polymorphism means that the same method call produces different behaviors due to different objects. Examples of this can be found everywhere in life: the same method of rest, different people have different methods of rest. Zhang San rest is to sleep, Li Si rest is to play games, programmers rest is to "knock a few lines of code".

II. Inheritance

Inheritance is not only an important feature of object-oriented programming, but also an important means to realize "code reuse".

If a new class inherits from a designed class, it directly has the characteristics of the existing class, which greatly reduces the work.

Difficulty. The existing class, we call it "parent class or base class", the new class, we call it "subclass or derived class".

1. Grammar format

Python supports multiple inheritance, and a subclass can inherit multiple parent classes. The syntax format of inheritance is as follows:

Class subclass name (parent class 1 [, parent class 2.]):

Class body: if no parent class is specified in the class definition, the default parent class is the object class. In other words, object is the parent of all classes, and it defines some default implementations common to all classes, such as: _ _ new__ ().

When you define a subclass, you must call the constructor of the parent class in its constructor. The call format is as follows:

Parent class name. _ _ init__ (self, parameter list)

Class Person: def _ init__ (self,name,age): self.name = name self.__age = age def say_age (self): print ("age is:", self.__age) class Student (Person): def _ init__ (self,name,age,grade): self.grade = grade Person.__init__ (self,name) Age) the # constructor contains calls to the parent constructor. According to need, not necessary. The subclass does not automatically call the _ _ init__ () of the parent class, we must call it explicitly. If _ _ name__ = ='_ _ main__': s=Student ('Zhuge', 18d1) s.say_age () 2, inheritance and rewriting of class members

1. Member inheritance: the subclass inherits all the members of the parent class except the constructor.

two。 Method rewriting: subclasses can redefine methods in the parent class, which overrides the methods of the parent class, also known as "override"

Class Person: def _ init__ (self,name,age): self.name = name self.__age = age def say_age (self): print ("age is:", self.__age) class Student (Person): def _ init__ (self,name,age,grade): self.grade = grade Person.__init__ (self,name) Age) def say_age (self): print (self.name, "age is:", self.age) # constructor contains calls to the parent constructor. According to need, not necessary. The subclass does not automatically call the _ _ init__ () of the parent class, we must call it explicitly. If _ _ name__ = ='_ _ main__': s=Student ('Zhuge', 18d1) s.say_age () 3, super () get the parent class definition

In the subclass, if you want to get the method of the parent class, we can do it through super ().

Super () represents the definition of the parent class, not the parent object.

Class A: def say (self): print ("A:" Self) print ("say AAA") class B (A): def say (self): # A.say (self) calls the parent class's say method super (). Say () # calls the parent class's method print ("say BBB") if _ name__ = "_ _ main__: b = B () b.say () 4, design pattern _ factory pattern implementation

The factory pattern separates the creator from the caller, using special factory classes to select implementation classes and create objects for unified management and control.

Class CarFactory: def createCar (self,brand): if brand = = "1": return one () elif brand = = "2": return two () elif brand = = '3brands: return three () else: return "unknown Brand Unable to create "class one: passclass two: passclass three: passfactory = CarFactory () C1 = factory.createCar (" 1 ") c2 = factory.createCar (" 2 ") print (C1) print (c2) 5, design pattern _ singleton pattern implementation

The core role of the singleton pattern (Singleton Pattern) is to ensure that there is only one instance of a class and to provide a global access point to that instance.

The singleton pattern generates only one instance object, which reduces the overhead on system resources. When the generation of an object requires more resources, such as reading configuration files and generating other dependent objects, it can generate a "singleton object" and then permanently reside in memory, thus greatly reducing overhead.

Class One: _ _ obj = None # is used to store this singleton _ _ init_flag = True def _ _ new__ (cls, * args * * kwargs): # refers to the class attribute in the One class if cls.__obj = = None: # object is the default parent class of the class cls.__obj = object.__new__ (cls) return cls.__obj def _ _ init__ (self,name): if One.__init_flag: print ("init....") Self.name = name One.__init_flag = Falseif _ name__=='__main__': a = One ("aa") print (a) b = One ("bb") print (b) after reading this article, I believe you have a certain understanding of "what are the three characteristics of python object-oriented". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report