Python object-oriented, python object-oriented
1. Object-Oriented Programming ):
For beginners of programming languages, OOP is not an easy-to-understand programming method. The three features of OPP include inheritance, encapsulation, and polymorphism, OOP makes it easier for us to modify and expand code;
Encapsulation: encapsulate objective objects into abstract classes, and classes can only allow trusted classes or objects to operate on their own data and methods to hide untrusted information;
Inheritance: You can use all the functions of an existing class and extend these functions without re-writing the original class. Generally, a subclass can have only one base class, multi-inheritance can be implemented through multi-level inheritance. There are three types of inheritance concepts: Implementation inheritance, interface inheritance, and visual inheritance;
Implement inheritance: the ability to use the attributes and methods of the base class without additional encoding;
Interface inheritance: only the names of attributes and methods are used, but subclasses must provide implementation capabilities;
Visible inheritance: subclass uses the appearance of the base class and the ability to implement code;
The OOP development paradigm is roughly: dividing objects-> abstract classes-> organizing classes into hierarchical structures (Inheritance and synthesis)-> designing and implementing classes and instances;
Polymorphism: allows you to set a parent object to be equal to one or more other sub-objects, the parent object can be run in different ways based on the features of the sub-objects assigned to it. In short, the sub-class pointer can be assigned to the parent class pointer;
Encapsulation and inheritance are for code reuse, while polymorphism is for interface reuse;
The process of turning an abstract class into a specific object is called Instantiation;
1> class and constructor:
2> class variables and instance variables:
3> class inheritance:
ClassSchoolMember (object ):
Membur_nums = 0
Def_ Init _ (self, name, age, sex ):
Self. name = name
Self. age = age
Self. sex = sex
Self. enroll ()
DefEnroll (self ):
SchoolMember. membur_nums + = 1
# Print ("\ 033 [32: 1 m The [% s] memburs SchoolMember [% s] is enrolled! \ 033 [0 m "% (self. membur_nums, self. name ))
Print ("The [% s] memburs SchoolMember [% s] is enrolled! "% (Self. Membur_nums,Self. Name ))
DefTell (Self):
Print ("Hello my name is % s"%Self. Name)
ClassTeacher (SchoolMember ):
Def_ Init __(Self, Name, age, sex, course, salary ):
Super (Teacher,Self)._ Init __(Name, age, sex)
Self. Course = course
Self. Salary = salary
DefTeaching (Self):
Print ("Teacher [% s] is teaching [% s]"% (Self. Name,Self. Course ))
ClassStudent (SchoolMember ):
Def_ Init __(Self, Name, age, sex, course, tuition ):
Super (Student,Self)._ Init __(Name, age, sex)
Self. Course = course
Self. Tuition = tuition
DefPay_tuition (Self):
Print ("Cao, student [% s] paying tuition [% s]"% (Self. Name,Self. Tuition ))
T1 = Teacher ("Alex",22,'F',"PY",1000)
T2 = Teacher ("Tenglan",25,'N'/',"PY",900)
S1 = Student ("SanJiang",24,"Female","Python",15000)
S2 = Student ("BaoAn",23,"F","Python",5000)
T1.tell ()
T1.teaching ()
S1.tell ()
S1.pay _ tuition ()
4> static methods of classes Class methods and attributes:
* *** Python does not support polymorphism and does not use polymorphism. polymorphism is used in java and c;
Applications with Polymorphism:
Class Members can be divided into three categories: fields, methods, and attributes;
1) fields: fields include common fields and static fields. They are different in definition and usage. The most essential difference is that the locations stored in the memory are different. Common fields belong to objects, static fields belong to the class. Common fields need to be accessed through objects, and static fields are accessed through classes;
Only one static field is saved in the memory, and one common field is saved in each object;
Static Field === class variable, common field === instance variable;
2) attribute (Class Method): the decorator changes the method to a class method and can only be called through the class. The instance cannot be called directly;
Basic use of attributes:
Static Method of the class: the class variable cannot be renewed or the instance variable cannot be accessed;
Class property to change the method to the property:
Set the attribute value:
Delete attribute settings:
4> New and classic categories:
1) Multi-inheritance: Search for breadth is preferred for new categories, and search for depth is preferred for classic classes (in 3.0, the search for breadth is preferred );
2) Class description: __doc __;
3) destroy an instance in the class: __del _ (destructor). After the program is executed, the instance is destroyed;
4) _ dict _ traverses all the variables in the class in dictionary form;
5> advantages of reflection:
ImportSys
ClassWebServer (object ):
Def_ Init _ (self, host, port ):
Self. host = host
Self. port = port
DefStart (self ):
Print ("Server is starting ...")
DefStop (self ):
Print ("Server is stopping ...")
DefRestart (self ):
Self. stop ()
Self. start ()
DefTest_run (self, name ):
Print ("Running ...", Name, self. host)
If_ Name _ ="_ Main __":
Server = WebServer ('Localhost',333)
# Print (sys. argv [1])
IfHasattr (server, sys. argv [1]):
Func = getattr (server, sys. argv [1])# Getting the server. start memory address
Func ()# Server. start ()
# Setattr (server, "run", test_run) # class instance, attribute, Method Name
# Server. run (server, "che ")
# Delattr (server, "host") # Delete objects in an instance
# Print (server. host)
Delattr (WebServer,"Start")# Deleting methods in a class
Print (server. restart ())
# Pai_dic = {
# "Start": server. start,
# "Stop": server. stop,
# "Restart": server. restart,
#}
# If sys. argv [1] = "start ":
# If sys. argv [1] in each _dic:
# Define _dic [sys. argv [1] ()