Python Learning notes-Object oriented

Source: Internet
Author: User

I. What is object-oriented programming 1, process-oriented programming

Process-oriented: The core is the process of two words, the process is to solve the problem of the steps, is to do first, what to do. Writing a program based on this idea is like designing a pipeline, which is a mechanical way of thinking.
Advantages: Complex process flow, simplifying
Cons: Poor scalability

2. Object-Oriented Programming

Object-oriented: The core is the object of two words, the object is the combination of features and skills. Writing programs based on this idea is like creating a world in which the world is made up of objects and is a "God-like" way of thinking.
Advantages: Strong Scalability
Cons: High programming complexity, prone to over-design problems

Second, class and object

An object is a combination of features and skills, and a class is a combination of features and skills that are similar in a series of objects.

    • In the real world: must be the existing object of a specific existence, after the summary of the class
    • In the program: Be sure to define the class first, then produce the object
    Object 1:        characteristics:            School = Tsinghua            name =lionel            sex = male            age =18        Skills:            Learning to            select    Subjects 2:        characteristics:            School = Tsinghua            name =xiaohong            sex = female            age =28        Skill:            Learning            Course    Object 3:        characteristics:            School = Tsinghua            name =daming            sex = male            age =38        Skill:            Learning to            choose Course
The real world stands in the school's perspective
School students in the program:        Similar characteristics of Tsinghua students            School = Tsinghua        Similar skills            Learning            Course Selection
School Student class in the program
#the class body code executes immediately at the defining stage of the class.classStudent:school='Oldboy'    defLearn (self):Print('is learning')    defChoose_course (self):Print('Choose Course')    Print('=====run')Print(Student.__dict__)
classes and objects in PythonThird, property lookup 1, Class properties: Data Properties, function properties
    • The data properties of the class are shared by all objects and the IDs are the same.
    • The function properties of a class are bound to objects, called binding methods, and memory addresses are different.
2, Instance properties: Data attributes 3, class attributes and additions and deletions to check
classStudent:school='Tsinghua'    defLearn (self):Print('is learning')    defChoose_course (self):Print('Choose Course')    Print('=====run')#View class PropertiesPrint(Student.school)#Data PropertiesPrint(Student.learn)#Function Properties"""=====run Tsinghua <function Student.learn at 0x101a60488>"""#Adding class Propertiesstudent.country=' China'Print(student.country)"""=====runchina"""#Modifying class PropertiesStudent.school='Peking University'Print(Student.school)"""=====run Peking University"""#Delete class PropertiesdelStudent.countryPrint(Student.country)
additions and deletions of class attributes4. Class and Object exercises
classStudent:school='Oldboy'    def __init__(self,name,age,sex,course): Self.name=name Self.age=Age Self.sex=Sex Self.course=Coursedefxue_xi ():Print('learning makes me happy')    defXuan_ke (self):Print('My name is%s, I choose to learn%s'%(self.name,self.course)) S1=student ('Lionel', 18,'male','python')Print(S1.school)Print(Student.__dict__)Print(S1.__dict__) Student.xue_xi () Student.xuan_ke (S1) S1.xuan_ke ()"""Tsinghua {' __module__ ': ' __main__ ', ' School ': ' Tsinghua ', ' __init__ ': <function student.__init__ at 0x101c60488> ' Xue_xi ' : <function student.xue_xi at 0x101c60598>, ' Xuan_ke ': <function student.xuan_ke at 0x101c60620>, ' __dict__ '  : <attribute ' __dict__ ' of ' Student ' objects>, ' __weakref__ ': <attribute ' __weakref__ ' of ' Student ' objects> ' __doc__ ': none}{' name ': ' Lionel ', ' age ': ' Sex ': ' Male ', ' course ': ' Python '} learning makes me happy my name is Lionel, choose to learn Python I call Lionel, Choose to learn Python"""
PracticeThe three major characteristics of the Class 1, static properties: Access to the Class property can also access to the instance properties
class:def __init__(Self,name,owner,width,length,heigh): Self.name=name Self.owner=owner Self.width=width self.length=length Self.heigh=Heigh @property#Access to class properties can also be accessed to instance properties    defCal_area (self):#print ('%s ' live%s total area is%s '% (self.owner,self.name,self.length*self.width))        returnself.length*SELF.WIDTHR1=room ('School District Room','Lionel', 100,100,100)Print(R1.cal_area)
Static Properties2. Class method: Only access to class properties, cannot access to instance properties
classRoom:tag=1def __init__(Self,name,owner,width,length,heigh): Self.name=name Self.owner=owner Self.width=width self.length=length Self.heigh=Heigh @property#Access to class properties can also be accessed to instance properties    defCal_area (self):returnself.length*self.width @classmethod#only access to class properties, cannot access to instance properties    defTell_info (cls,x):Print('--->', Cls.tag,x) Room.tell_info (10)"""---> 1"""
class Method3. Static methods: Neither access to class properties nor access to instance properties
classRoom:tag= 1def __init__(self, name, owner, width, length, heigh): Self.name=name Self.owner=owner Self.width=width self.length=length Self.heigh=Heigh @property#Access to class properties can also be accessed to instance properties    defCal_area (self):returnSelf.length *self.width @classmethod#only access to class properties, cannot access to instance properties    defTell_info (CLS, x):Print('--->', Cls.tag, x) @staticmethod#neither access to class properties nor access to instance properties    defStudent (a,b,c):Print('%s%s%s is learning'%(A,B,C)) Room.student ('daming','Xiaohong','Lionel') R1=room ('School District Room','Lionel', 100,100,100) r1.student ('Alex','Egon','Lionel')"""daming Xiaohong Lionel is learning daming Xiaohong Lionel is learning"""
Static Methods

Note: Although static methods and class methods are prepared for classes, they can be used if the instance is to be used, but it is easy to confuse the instance when it is invoked, not knowing what you are going to do.

V. What is a combination?

Composition refers to the combination of classes in which an object of another class is used as a data property, called a class.

classSchool:def __init__(SELF,NAME,ADDR): Self.name=name Self.addr=AddrdefZhao_sheng (self):Print('%s is enrolling'%self.name)classCourse:def __init__(self,name,price,period,school): Self.name=name Self.price=Price Self.period=period Self.school=SCHOOLS1=school ('Oldboy','Beijing') S2=school ('Oldboy','Nanjing') S3=school ('Oldboy','Shanghai') msg="""1 Old boys Beijing Campus 2 old boys Nanjing Campus 3 Old boys Shanghai Campus""" whileTrue:Print(msg) Menu={        '1': S1,'2': S2,'3': s3} Choice=input ('Select the school >>:') School_obj=Menu[choice] Name=input ('Course Name: >>') Price=input ('course fee >>:') Period=input ('course cycle >>:') New_course=Course (name,price,period,school_obj)Print('Course '%s ' belongs to '%s ' '%s ' School'% (NEW_COURSE.NAME,NEW_COURSE.SCHOOL.NAME,NEW_COURSE.SCHOOL.ADDR))
Combination

Python Learning Notes-object-oriented

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.