Object-oriented Python

Source: Internet
Author: User

1 Object-oriented programming (Object-oriented programming):
for beginners in programming languages, OOP is not an easy-to-understand programming approach, and the three main features of OPP are inheritance, encapsulation, polymorphism, OOP also makes it easier for us to modify and extend the code;
    Encapsulation: encapsulate objective things into abstract classes, and classes can put their own data and methods only let the trusted class or object operation, to the untrusted information hiding;
  Inheritance: You can use all the functionality of an existing class and extend it without rewriting the original class; In general, a subclass can have only one base class, to implement multiple inheritance,                               can be implemented through multilevel inheritance; there are three types of implementations of inheritance concepts: implementation inheritance, interface inheritance and visual inheritance, and
implementation inheritance: the ability to use the properties and methods of a base class without additional coding;
Interface inheritance: Only the names of properties and methods are used, but subclasses must provide the ability to implement them;
Visual inheritance: Subclasses use the appearance of the base class and the ability to implement code;
The  oop development paradigm is broadly organized as a hierarchical structure with abstract classes, such as objects (Inheritance and composition), design and implementation of classes and instances;
Polymorphism: A technique that allows the parent object to be set equal to one or more of his sub-objects, after which the parent can run differently depending on the attributes of the child object currently assigned to it; simple                     that allows pointers of subclass types to be assigned to a pointer to the parent class type;
encapsulation and inheritance are for code reuse, and polymorphism is for interface reuse;
a pumping The process of turning a class into a concrete object is called instantiation;                         &NB     Sp            

1> class and Construction methods:

Variables and instance variables for the 2> class:

        

Inheritance of the 3> class:
         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:1m the [%s] memburs Schoolmember [%s] is enrolled!\033[0m"% (self.membur_nums,self.name))
          
Print ("The [%s] memburs Schoolmember [%s] is enrolled!"% ( Self. Membur_nums, Self . Name))
def
Tell ( Self):
Print (
"Hello My name is%s"% Self . Name)
Class
Teacher (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))
Class
Student (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", A,' F ',"PY", +)
T2 = Teacher (
"Tenglan", -,' n /A ',"PY", the)

S1 = Student (
"Sanjiang", -,"Female","Python",15000)
S2 = Student (
"Baoan", at,"F","Python", the)

T1.tell ()
T1.teaching ()
S1.tell ()
S1.pay_tuition ()

Static method class methods and properties for the 4> class:
Python does not support polymorphism and is not polymorphic, polymorphism is used in Java and C #;
Applications for polymorphism:

        

The members of a class can be divided into three main categories: fields, methods, and properties;

          

1) fields: Fields include normal fields and static fields, they are different in definition and use, and the most essential difference is the memory of the location, the ordinary field belongs to the object, the static field belongs to the class, the ordinary field needs to be accessed through the object, the static field through the class Access
Static field in memory only one copy, normal field in each object to save one copy;
static field = = = Class variable, ordinary field = = = Instance variable;
2) Attribute (class method): The adorner in which the method becomes a class method, can only be called through the class, the instance can not be called directly;
Basic use of attributes:

The static method of the class: cannot access the class variable, nor can it access the instance variable;

          

Class, change the method to a property:

          

To set a value for a property:

          

To delete a property's setting value:

          

4> new class and classic class:
1) Multiple inheritance: New class with breadth first find, classic class with depth first search (3.0 is breadth first);

2) Description Information of class: __doc__;

3) Instance destruction in class: __del__ (destructor), execution of instance destruction after execution of the program;

        

4) __dict__ All variables under the class in the form of a dictionary;

The Magical 5> reflex:

         ImportSys
         classWebServer (object):
           def__init__ (Self,host,port):
Self.host = Host
Self.port = Port

           defStart (self):
Print"Server is starting ...")
def
Stop (self):
Print"Server is stopping ...")
def
Restart (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]) #获取server. Start memory address
          
func ()#server. Start ()
# setattr (Server, "Run", Test_run) #类的实例, properties, method name
# Server.run (server, "Che")

# delattr (Server, "host") #删除实例中的对象
# Print (server.host)

        
delattr (WebServer,"Start") #删除类中的方法
          
Print (Server.restart ())
        
# cmd_dic = {
# "Start": Server.start,
# "Stop": Server.stop,
# "Restart": Server.restart,
# }
#if sys.argv[1] = = "Start":
# if SYS.ARGV[1] in Cmd_dic:
# Cmd_dic[sys.argv[1]] ()
 

Object-oriented Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.