Python Learning Series (eight) (object-oriented basis)

Source: Internet
Author: User
Tags export class

Python Learning Series (eight) (object-oriented basis) Python Learning Series (vii) (database programming) one, object-oriented
1, domain : A variable that belongs to an object or class. There are two types, instance variables -objects that belong to each instance/class, and class variables -that belong to the class itself. 2, class method : An object can also use a function that belongs to a class to have functionality, such that a function is called a method of a class . fields and methods are called properties of a class . Class is created with the class keyword, and the properties of the class are listed in an indented block . 3,self: There is only one special difference between the methods of a class and normal functions----they must have an extra first parameter name, but they cannot be assigned a value at the time of the call, and Python will provide it. This particular variable refers to the object itself, which is named Self by convention. Self is equivalent to the self pointer in C + + and this in java,c#. create a class:
1 class Person : 2     Pass 3  4 p= person()5print  p6 >>> ============= RESTART ================================7 >>>8 <__main__. Person instance at 0x02a99b98>
View Code 4. How to use the object: 1)
1 class Person : 2     def Sayhi (self): 3         Print ' hello,how is you? ' 4  5 p= Person()6 p.sayhi ()
View Code 2) using the __init__ method
1 classPerson :2     def __init__(self,name):3Self.name=name4        5     defSayhi (self):6         Print 'hello,my name is', Self.name7  8P=person ('John')9P.sayhi ()
View Code

3) using the class and object variables

1 classPerson :2     " "represents a person." "3population=04     def __init__(self,name):5         " "initializes the person ' s data." "6Self.name=name7         Print '(Initializing%s)'%Self.name8Person.population +=19        Ten     def __del__(self): One         " "I ' m dying." " A         Print '%s says bye.'%Self.name -Person.population-=1 -         ifperson.population==0: the             Print " "I ' m the last one." " -         Else: -             Print 'There is still%d people left.'%person.population -             +     defSayhi (self): -         " "greeting by the person. +   A Really,that ' s all it does." " at         Print 'hello,my name is%s.'%Self.name -   -     defHowmany (self): -         " "Prints the current population." " -         ifPerson.population==1: -             Print " "I ' m the only one here." " in         Else: -             Print 'We have%d person here.'%person.population to   +     -P=person ('John') the P.sayhi () * P.howmany () $  Panax NotoginsengP1=person ('John1') - P1.sayhi () the P1.howmany () +   A P.sayhi () theP.howmany ()
View Code

Property Reference : Using Self variables to reference the variables and methods of the same object.

Ii. Inheritance
1, inheritance is used to describe the type and subtype relationships between classes. 2, polymorphic : A subtype can replace a parent type in any situation where the parent type is required, that is, the object can be considered an instance of the parent class. 3, Basic class ( superclass ) and subclass ( Export class ) 4, Example:
1 classSchoolmember:#public properties of teachers and students, base class2     " "represents any schoolmember." "3     def __init__(self,name,age):4         " "initializes the person ' s data." "5Self.name=name6Self.age= Age7         Print '(Initializing schoolmember%s)'%Self.name8  9     defTell (self):Ten         " "Tell my details." " One         Print " "name:%s\nage:%s" "%(self.name,self.age) A   - classTeacher (Schoolmember):#Teacher Sub-category -     " "represents any Teacher." " the     def __init__(self,name,age,salary): -         " "initializes the person ' s data." " -Schoolmember.__init__(Self,name,age)#Inheritance -self.salary=Salary +         Print '(Initializing Teacher%s)'%Self.name -   +     defTell (self): A         " "Tell my details." " atSchoolmember.tell (self)#Inheritance -         Print " "salary:%d" "%self.salary -   - classStudent (Schoolmember):#Student sub-category -     " "represents any Student." " -     def __init__(self,name,age,marks): in         " "initializes the person ' s data." " -Schoolmember.__init__(Self,name,age)#Inheritance toself.marks=Marks +         Print '(Initializing Student%s)'%Self.name -   the     defTell (self): *         " "Tell my details." " $Schoolmember.tell (self)#InheritancePanax Notoginseng         Print " "marks:%d" "%Self.marks -   the   +T=teacher ('Mrs.jhon', 40,4000) AS=student ('ZHANGBC', 23,90) themembers=[T,s] +  forMemberinchMembers : - Member.tell () $     Print "'
View Code
Third, summary after nearly a year, writing unfamiliar, think or study more practical bar. This space is short, the understanding, the follow-up gradually perfect.

Python Learning Series (eight) (object-oriented basis)

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.