Python6:oriented objective programming
生活随笔
收集整理的這篇文章主要介紹了
Python6:oriented objective programming
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.introduction
前面采用操作數據的函數或語句塊來設計程序,也就是面向過程編程。把數據和功能結合起來,用對象的包裹起來組織程序,就是面向對象編程。在大多數時候可以使用過程性編程,但是想要編寫大型程序時,就得使用面向對象的編程技術。類和對象是面向對象編程的兩個主要組建。類可以創建一個新類型,對象是這個類的實例。 對象可以使用普通的屬于對象的變量存儲數據,屬于一個對象或類的變量稱為域。對象基于累的函數實現功能(類的方法)。
2.self
類方法與普通函數只有一個特別的區別——必須有一個額外的第一個參數名稱,但是在調用這個方法的時候不為這個參數賦值,Python會提供這個值。這個特別的變量指對象本身,按照慣例是self。Python中的self等價于C++中的self指針和Java、C#中的this參考。
假如有一個類MyClass和類的實例MyObject。當調用這個對象的方法MyObject.method(arg1, arg2)的時候,由Python自動轉為MyClass.method(MyObject,arg1,arg2)——這就是self的原理了。
- 創建一個最簡單的類
3.對象的方法
類/對象可以擁有像函數一樣的方法,這些方法與函數的區別只是一個額外的self變量。class Person:def sayHello(self):print 'Hello,how are you?'; p = Person(); p.sayHello(); #be equal to #Person().sayHello();輸出為: Hello,how are you? 這里可以看到self的用法,sayHello()方法沒有任何參數,但是仍然在函數定義時有self。
4.__init__方法
Python類中有很多方法的名字有特殊的重要意義。__init__方法在類的一個對象被建立時,馬上運行。這個方法可以用來對對象做一些希望的初始化。class Person:def __init__(self,name):self.name = name;def sayHello(self):print 'Hello,how are you?',self.name; p = Person('Ziheng'); p.sayHello();輸出為: Hello,how are you? Ziheng
__init__方法類似于C++、C#和Java中的constructor 。
5.類與對象的方法
類與對象的數據部分只是與類和對象的名稱空間綁定的普通變量,即這些名稱只在這些類與對象的前提下有效。有兩種類型的域 ——類的變量和對象的變量,根據是類還是對象擁有這個變量而區分。類的變量由一個類的所有對象(實例)共享使用。只有一個類變量的拷貝,所以當某個對象對類的變量做了改動的時候,這個改動會反映到所有其他的實例上。
對象的變量由類的每個對象/實例擁有。因此每個對象有自己對這個域的一份拷貝,即它們不是共享的,在同一個類的不同實例中,雖然對象的變量有相同的名稱,但是是互不相關的。
class Person:'''Represents a person.'''population = 0def __init__(self, name):'''Initializes the person's data.'''self.name = nameprint '(Initializing %s)' % self.name# When this person is created, he/she# adds to the populationPerson.population += 1def __del__(self):'''I am dying.'''print '%s says bye.' % self.namePerson.population -= 1if Person.population == 0:print 'I am the last one.'else:print 'There are still %d people left.' % Person.populationdef sayHi(self):'''Greeting by the person.Really, that's all it does.'''print 'Hi, my name is %s.' % self.namedef howMany(self):'''Prints the current population.'''if Person.population == 1:print 'I am the only person here.'else:print 'We have %d persons here.' % Person.population swaroop = Person('Swaroop') swaroop.sayHi() swaroop.howMany()kalam = Person('Abdul Kalam') kalam.sayHi() kalam.howMany()swaroop.sayHi() swaroop.howMany()輸出為:
population屬于Person類,因此是一個類的變量。name變量屬于對象(它使用self賦值)因此是對象的變量。
就如同__init__方法一樣,還有一個特殊的方法__del__,它在對象消逝的時候被調用。對象消逝即對象不再被使用,它所占用的內存將返回給系統作它用。在這個方法里面,我們只是簡單地把Person.population減1。
6.繼承
class SchoolMember:'''Represents any school member.'''def __init__(self, name, age):self.name = nameself.age = ageprint '(Initialized SchoolMember: %s)' % self.namedef tell(self):'''Tell my details.'''print 'Name:"%s" Age:"%s"' % (self.name, self.age); class Teacher(SchoolMember):'''Represents a teacher.'''def __init__(self, name, age, salary):SchoolMember.__init__(self, name, age)self.salary = salaryprint '(Initialized Teacher: %s)' % self.namedef tell(self):SchoolMember.tell(self)print 'Salary: "%d"' % self.salary class Student(SchoolMember):'''Represents a student.'''def __init__(self, name, age, marks):SchoolMember.__init__(self, name, age)self.marks = marksprint '(Initialized Student: %s)' % self.namedef tell(self):SchoolMember.tell(self)print 'Marks: "%d"' % self.marks t = Teacher('Mrs. Shrividya', 40, 30000) s = Student('Swaroop', 22, 75) print # prints a blank line members = [t, s] for member in members:member.tell() # works for both Teachers and Students總結
以上是生活随笔為你收集整理的Python6:oriented objective programming的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: headerf.h
- 下一篇: 防止Visual C++应用程序缓冲区溢