Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of python continuing to find objects

Shulou Source: shulou.com Published: 2022-06-02 02:09:19 09月12日 Update

This article will explain in detail the example analysis of python continuing to find a partner. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Three object-oriented features: encapsulation, inheritance and polymorphism

1. Encapsulate (improve the security of the program) class Car: def _ init__ (self,brand): self.brand=brand def start (self): print ('bike has been pedaled') car=Car ('bike') car.start () print (car.brand)

Running result

The bike has been pedaled away

Bicycle

At first, it reported that there was no definition of name. I didn't know what was wrong with the boss for a while. It turned out to be the sixth line.

Self.name, it was written at that time.

What are the methods in stu? Just write it like this.

It can be accessed outside the class via _ Student (class name) _ _ age (which you do not want to be accessed)

Class Student: def _ _ init__ (self,name,age): self.name=name self.__age=age # age does not want to be used outside the class So add two _ def show (self): print (self.name,self.__age) stu=Student ('Zhang San', 20) stu.show () # use name and ageprint (stu.name) print (dir (stu)) print (stu._Student__age) Zhang San 20 sheets [_ Student__age','_ class__','_ _ delattr__','_ _ dict__','_ dir__' '_ _ doc__',' _ _ eq__','_ _ format__','_ _ ge__','_ _ getattribute__','_ _ gt__','_ _ hash__','_ _ init__','_ _ init_subclass__','_ _ le__','_ lt__','_ module__','_ ne__','_ _ new__' '_ _ reduce__',' _ _ reduce_ex__','_ _ repr__','_ _ setattr__','_ _ sizeof__','_ _ str__','_ _ subclasshook__','_ _ weakref__', 'name',' show'] 202, inheritance (improve code reusability)

Class Person (object): def _ init__ (self,name,age): self.name=name self.age=age def info (self): print (self.name,self.age) class Student (Person): def _ init__ (self,name,age,stu_no): super (). _ init__ (name,age) self.stu_no=stu_noclass Teacher (Person): def _ init__ (self,name,age) Teacherofyear): super (Teacher, self). _ _ init__ (name,age) self.teacherofyear=teacherofyearstu=Student ('Zhang San', 20 recordings' 1001') teacher=Teacher ('Li Si', 34 Jing 10) stu.info () teacher.info ()

Zhang San 20

Li Si 34

3. Method rewriting

At this time, you can only output the student number, which does not meet the demand.

Class Person (object): def _ init__ (self,name,age): self.name=name self.age=age def info (self): print (self.name,self.age) class Student (Person): def _ init__ (self,name,age,stu_no): super (). _ init__ (name) Age) self.stu_no=stu_no def info (self): print (self.stu_no) class Teacher (Person): def _ _ init__ (self,name,age,teacherofyear): super (Teacher, self). _ init__ (name,age) self.teacherofyear=teacherofyearstu=Student ('Zhang San', 20 dagger 1001') teacher=Teacher ('Li Si', 34 Mague 10) stu.info () teacher.info ()

1001

Li Si 34

Look at the overload below.

Class Person (object): def _ init__ (self,name,age): self.name=name self.age=age def info (self): print (self.name,self.age) class Student (Person): def _ init__ (self,name,age,stu_no): super (). _ init__ (name,age) self.stu_no=stu_no def info (self): super (Student) Self). Info () print (self.stu_no) class Teacher (Person): def _ _ init__ (self,name,age,teacherofyear): super (Teacher, self). _ _ init__ (name,age) self.teacherofyear=teacherofyearstu=Student ('Zhang San', 20 penny 1001) teacher=Teacher ('Li Si') 34 stu.info 10) print ('- -') teacher.info ()

Running result

Zhang San 20

1001

-

Li Si 34

Export teaching seniority

Class Person (object): def _ init__ (self,name,age): self.name=name self.age=age def info (self): print (self.name,self.age) class Student (Person): def _ init__ (self,name,age,stu_no): super (). _ init__ (name,age) self.stu_no=stu_no def info (self): super (Student) Self). Info () print (self.stu_no) class Teacher (Person): def _ _ init__ (self,name,age,teachofyear): super (). _ init__ (name,age) self.teachofyear=teachofyear def info (self): super (). Info () print ('teaching age', self.teachofyear) stu=Student ('Zhang San', 20 dint 1001') teacher=Teacher ('Li Si') 34 stu.info 10) print ('- -') teacher.info ()

Running result

Zhang San 20

1001

-

Li Si 34

Teaching age 10

4. Object class

5. Polymorphism (to improve the extensibility and maintainability of the program)

Java is a static language, and python is a dynamic language.

6. Special methods and special attributes special methods

Two special methods-create

1 initialize init

2new

Special attribute

The start of two underscores and the end of two underscores are special attributes.

Bind two properties

Class A: passclass B: passclass C (AMagneB): def _ _ init__ (self,name,age): self.name=name self.age=age# creates an object of class C xdiscover C ('Jack',20) # x is an instance object of class C print (x.discovery discovery 20} age': 20} used by pycharm

Clicking on the plus sign will release it, and clicking on the minus sign will shrink it like this, indicating that all the shrinking content belongs to this category.

Look at the arrow on the far left, which means to override the method in the person class

English

This is the end of the article on "sample Analysis of python continuing to find objects". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Zhang San Li Si special method object two attribute bicycle that is teaching age article result run example analysis polymorphism underline content more program Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MariaDB Huawei OPPO Reno Redmi