Concise Python Tutorial Learn note 7

Source: Internet
Author: User

11. Object-Oriented Programming

(1) Introduction

Slightly

(2) Self

Suppose you have a class called MyClass and an instance of this class MyObject .

When you call the method MyObject.method(arg1, arg2) of this object,

This will be automatically converted MyClass.method(MyObject, arg1, arg2) by Python-that's how it self works .

(3) class

Slightly

(4) Methods of the object

Slightly

(5) __init__ method

Slightly

(6) Variables for classes and objects

A variable of a class   is shared by all objects (instances) of a class. There is only one copy of the class variable, so when an object changes the variables of the class, the change is reflected on all other instances.

variable of Object   owned by each object/instance of the class. So each object has its own copy of the domain, that is, they are not shared, and in different instances of the same class, although the variables of the object have the same name, they are unrelated.

1 #Coding=utf-82 classPerson (object):3     """represents a person."""4Population = 0#class Variables5 6     def __init__(self, name):7         """initializes the person ' s data."""8Self.name = Name#instance Variable9         Print "(Initializing%s)"%Self.nameTen  One         #When this is created, he/she A         #adds to the population -Person.population + = 1 -  the     def __del__(self): -         """I am dying.""" -         Print "%s says bye."%Self.name -  +Person.population-= 1 -  +         ifPerson.population = =0: A             Print "I am the last one." at         Else: -             Print "There is still%d people left."%person.population -  -     defSay_hi (self): -         """greeting by the person. -          in really, that's all it does.""" -         Print "Hi, my name is%s."%Self.name to  +     defHow_many (self): -         """Prints the current population.""" the         ifPerson.population = = 1: *             Print "I am the only person here." $         Else:Panax Notoginseng             Print "We havve%d persons here."%person.population -  the  + if __name__=="__main__": A     Print "*"* 20 +"Swaroop Begin"+"*"* 20 theSwaroop = Person ("Swaroop") + Swaroop.say_hi () - Swaroop.how_many () $  $     Print "*"* 20 +"Kalam Begin"+"*"* 20 -Kalam = person ("Abdul Kalam") - Kalam.say_hi () the Kalam.how_many () - Wuyi     Print "*"* 20 +"Say_hi && How_many"+"*"* 20 the Swaroop.say_hi () - Swaroop.how_many () Wu  -     Print "*"* 20 +"End"+"*"* 20
Object Methods

Output:

(7) Inheritance

1 #-*-coding:utf-8-*-2 3 4 classSchoolmember (object):5     """represents any school member."""6 7     def __init__(self, Name, age):8Self.name =name9Self.age = AgeTen         Print "(Initialized schoolmember:%s)"%Self.name One  A     defTell (self): -         """Tell my details.""" -         Print "name:\ "%s\" age:\ "%s\ ""%(Self.name, Self.age) the  -  - classTeacher (schoolmember): -     """represents a teacher.""" +  -     def __init__(self, name, age, salary): +Schoolmember.__init__(Self, name, age)#calling the initialization of the parent class ASelf.salary =Salary at         Print "(Initialized teacher:%s"%Self.name -  -     defTell (self): -Schoolmember.tell (self)#calling the method of the parent class through the class, no need to create an object -         Print "salary:\ "%d\""%self.salary -  in  - classStudent (schoolmember): to     """represents a student.""" +  -     def __init__(self, name, age, marks): theSchoolmember.__init__(self, name, age) *Self.marks =Marks $         Print "(Initialized student:%s)"%Self.namePanax Notoginseng  -     defTell (self): the Schoolmember.tell (self) +         Print "marks:\ "%d\""%Self.marks A  the  + if __name__=="__main__": -     Print "**"* 20 +"Teacher"+"**"* 20 $t = Teacher ("Mrs.shrividya", 40, 30000) $  -     Print "**"* 20 +"Student"+"**"* 20 -s = Student ("Swaroop", 22, 75) the  -     Print "**"* 44WuyiMembers =[T, S] the      forMemberinchMembers : -Member.tell ()
Inheritance

Output:

Concise Python Tutorial Learn note 7

Related Article

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.