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 core concepts and knowledge points of Python object-oriented programming

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces what is the core concept of Python object-oriented programming, the content is very detailed, interested friends can use it for reference, I hope it can be helpful to you.

Core concepts of object-oriented programming: encapsulation, abstraction, polymorphism, inheritance, combinatorial encapsulation:

The first concept: in object-oriented programming, objects centralize variables and methods in one place, that is, the object itself.

The second concept: it refers to hiding the internal data of the class to avoid direct access by client code.

Class Data: def _ init__ (self): self.list1= [22 index 33 index 44 5 5 print 66] def chang_list (self,index,num): self.list1 [index] = numdata1=Data () data1.list1 [0] = 100print (data1.list1) data2=Data () data2.chang_list (0100) print (data2.list1) abstraction:

It refers to the process of stripping off many characteristics of things so that they only retain the most basic materials. In object-oriented programming, abstract techniques are used when using classes to model objects.

Import abcclass Animal (metaclass=abc.ABCMeta): @ abc.abstractmethod # Abstract object method def sound (self): pass @ abc.abstractclassmethod# Abstract Class method def test1 (cls): pass @ abc.abstractstaticmethod# Abstract static method def test2 (self): passclass Dog (Animal): def sound (self): print ("wang wang") @ classmethod def test1 (cls): print ("class of method") @ staticmethod def test2 (): print ("static of method") dog=Dog () dog.sound () dog.test1 () Dog.test1 () dog.test2 () Dog.test2 () inheritance:

1), put the common properties and methods into the parent class, and only consider the unique properties and methods.

2), the method that overrides the parent class is to override the parent class method, and only the methods overridden in the subclass are called in the run time, not the methods in the parent class.

3) A subclass has a parent class called single inheritance, and a subclass can have multiple parent classes and have all the properties and methods of the parent class.

Class Student: def study (self): print ("students want to learn") def practice (self): print ("learn to practice") class HighStudent (Student): def study_compute (self): print ("high school classmates also learn computers") hs=HighStudent () hs.study () hs.practice () hs.study_compute () polymorphism:

1) refers to the ability to provide interfaces for different basic forms (data types), and interfaces refer to functions and methods.

2), as the name implies, polymorphism is the meaning of various forms of expression, it is a mechanism, an ability, not a key word. It is implemented in the inheritance of the class and reflected in the method call of the class.

3) Polymorphism means that the variable does not know what the referenced object is, and behaves differently according to the referenced object.

4) Polymorphism is based on encapsulation and inheritance, and different subclass objects call the same method, resulting in different execution effects. It can increase the flexibility of the code, on the premise of inheriting and rewriting the methods of the parent class, and calling the methods will not affect the internal design of the class.

Class Animals (object): def _ init__ (self,name): self.name=name def eat (self): print (self.name+ "is eating") class Cat (Animals): def _ init__ (self,name): super (Cat, self). _ init__ (name) class Pig (Animals): def _ init__ (self,name): super (Pig) Self). _ init__ (name) class Person (): def feedAnimal (self,animal): animal.eat () cat=Cat ("tom") pig=Pig ("peiqi") tony=Person () tony.feedAnimal (cat) tony.feedAnimal (pig) combination:

1), through the combination technique to save one object action variable in another object, you can simulate the ownership relationship.

2) it is a method of combining objects or classes into more complex data structures or component implementations.

3) in a combination, an object can be used to call member functions in other modules, so that cross-module calls of basic functions can be achieved without inheritance.

Class Monkey (): def _ _ init__ (self,name,owner): self.name=name self.owner=ownerclass Persons (): def _ init__ (self,name): self.name=namemike=Persons ("mike") mickMouse=Monkey ("goldeMonkey") Mike) print (mickMouse.owner.name) class A (object): def A1 (self): print ("A1") class B (object): def b1 (self): print ("b1") A (). A1 () bb=B () bb.b1 () what are the core concepts of Python object-oriented programming. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report