首頁 > 後端開發 > Python教學 > 詳解詳細介紹l了Python類別的繼承

詳解詳細介紹l了Python類別的繼承

迷茫
發布: 2017-03-25 17:50:18
原創
1656 人瀏覽過

Python類別的繼承(進階5)

1. python中什麼是繼承

python中什麼是繼承:

  • 新類別不必從頭編寫

  • 新類別從現有的類別繼承,就自動擁有了現有類別的所有功能

  • 新類別只需要編寫現有類別缺少的新功能

繼承的好處:

  • #複用已有程式碼

  • 自動擁有了現有類別的所有功能

  • 只需要寫缺少的新功能

繼承的特點:

  • 子類別和父類別是is關係

python繼承的特點:

  • 總是從某個類別繼承

  • 不要忘記呼叫super().init

2.python中繼承一個類別

class Person(object):
    def init(self, name, gender):
        self.name = name
        self.gender = gender
class Teacher(Person):
    def init(self, name, gender, course):
        super(Teacher, self).init(name, gender)
        self.course = course

t = Teacher('Alice', 'Female', 'English')
print t.name
print t.course
登入後複製

3. python中判斷類型

函數isinstance()可以判斷一個變數的類型,既可以用在Python內建的資料型別如str、list、dict,也可以用在我們自訂的類,它們本質上都是資料類型。

class Person(object):
    def init(self, name, gender):
        self.name = name
        self.gender = gender

class Student(Person):
    def init(self, name, gender, score):
        super(Student, self).init(name, gender)
        self.score = score

class Teacher(Person):
    def init(self, name, gender, course):
        super(Teacher, self).init(name, gender)
        self.course = course

t = Teacher('Alice', 'Female', 'English')

print isinstance(t, Person)
print isinstance(t, Student)
print isinstance(t, Teacher)
print isinstance(t, object)
登入後複製

4. python中多態

class Person(object):
    def init(self, name, gender):
        self.name = name
        self.gender = gender
    def whoAmI(self):
        return 'I am a Person, my name is %s' % self.name

class Student(Person):
    def init(self, name, gender, score):
        super(Student, self).init(name, gender)
        self.score = score
    def whoAmI(self):
        return 'I am a Student, my name is %s' % self.name

class Teacher(Person):
    def init(self, name, gender, course):
        super(Teacher, self).init(name, gender)
        self.course = course
    def whoAmI(self):
        return 'I am a Teacher, my name is %s' % self.name
        
        
import json

class Students(object):
    def read(self):
        return r'["Tim", "Bob", "Alice"]'

s = Students()

print json.load(s)
登入後複製

5. python中多重繼承

除了從一個父類別繼承外,Python允許從多個父類別繼承,稱為多重繼承。 Java不能多繼承

class A(object):
    def init(self, a):
        print 'init A...'
        self.a = a

class B(A):
    def init(self, a):
        super(B, self).init(a)
        print 'init B...'

class C(A):
    def init(self, a):
        super(C, self).init(a)
        print 'init C...'

class D(B, C):
    def init(self, a):
        super(D, self).init(a)
        print 'init D...'
        
        
class Person(object):
    pass

class Student(Person):
    pass

class Teacher(Person):
    pass

class SkillMixin(object):
    pass

class BasketballMixin(SkillMixin):
    def skill(self):
        return 'basketball'

class FootballMixin(SkillMixin):
    def skill(self):
        return 'football'

class BStudent(BasketballMixin):
    pass

class FTeacher(FootballMixin):
    pass

s = BStudent()
print s.skill()

t = FTeacher()
print t.skill()
登入後複製

6. python中取得物件資訊

除了用isinstance() 判斷它是否是某種類型的實例外,還有沒有別的方法取得到更多的資訊呢?

首先可以用type() 函數取得變數的類型,它傳回一個Type 物件

#dir() 函數取得變數的所有屬性

dir()傳回的屬性是字串列表,如果已知一個屬性名稱,要取得或設定物件的屬性,就需要用getattr() 和setattr( )函數了

class Person(object):
    def init(self, name, gender):
        self.name = name
        self.gender = gender

class Student(Person):
    def init(self, name, gender, score):
        super(Student, self).init(name, gender)
        self.score = score
    def whoAmI(self):
        return 'I am a Student, my name is %s' % self.name

print type(123) # <type &#39;int&#39;>

s = Student(&#39;Bob&#39;, &#39;Male&#39;, 88)
print s  # <class &#39;main.Student&#39;>

print dir(123) # [&#39;abs&#39;, &#39;add&#39;, &#39;and&#39;, &#39;class&#39;, &#39;cmp&#39;, &#39;coerce&#39;, &#39;delattr&#39;, &#39;p&#39;, &#39;pmod&#39;, &#39;doc&#39;, &#39;float&#39;, &#39;floorp&#39;, &#39;format&#39;, &#39;getattribute&#39;, &#39;getnewargs&#39;, &#39;hash&#39;, &#39;hex&#39;, &#39;index&#39;, &#39;init&#39;, &#39;int&#39;, &#39;invert&#39;, &#39;long&#39;, &#39;lshift&#39;, &#39;mod&#39;, &#39;mul&#39;, &#39;neg&#39;, &#39;new&#39;, &#39;nonzero&#39;, &#39;oct&#39;, &#39;or&#39;, &#39;pos&#39;, &#39;pow&#39;, &#39;radd&#39;, &#39;rand&#39;, &#39;rp&#39;, &#39;rpmod&#39;, &#39;reduce&#39;, &#39;reduce_ex&#39;, &#39;repr&#39;, &#39;rfloorp&#39;, &#39;rlshift&#39;, &#39;rmod&#39;, &#39;rmul&#39;, &#39;ror&#39;, &#39;rpow&#39;, &#39;rrshift&#39;, &#39;rshift&#39;, &#39;rsub&#39;, &#39;rtruep&#39;, &#39;rxor&#39;, &#39;setattr&#39;, &#39;sizeof&#39;, &#39;str&#39;, &#39;sub&#39;, &#39;subclasshook&#39;, &#39;truep&#39;, &#39;trunc&#39;, &#39;xor&#39;, &#39;bit_length&#39;, &#39;conjugate&#39;, &#39;denominator&#39;, &#39;imag&#39;, &#39;numerator&#39;, &#39;real&#39;]

print dir(s) # [&#39;class&#39;, &#39;delattr&#39;, &#39;dict&#39;, &#39;doc&#39;, &#39;format&#39;, &#39;getattribute&#39;, &#39;hash&#39;, &#39;init&#39;, &#39;module&#39;, &#39;new&#39;, &#39;reduce&#39;, &#39;reduce_ex&#39;, &#39;repr&#39;, &#39;setattr&#39;, &#39;sizeof&#39;, &#39;str&#39;, &#39;subclasshook&#39;, &#39;weakref&#39;, &#39;gender&#39;, &#39;name&#39;, &#39;score&#39;, &#39;whoAmI&#39;]

print getattr(s, &#39;name&#39;) # Bob
setattr(s, &#39;name&#39;, &#39;Adam&#39;) 
print s.name # Adam

class Person(object):

    def init(self, name, gender, **kw):
        self.name = name
        self.gender = gender
        for k, v in kw.iteritems():
            setattr(self, k, v)


p = Person(&#39;Bob&#39;, &#39;Male&#39;, age=18, course=&#39;Python&#39;)
print p.age # 18
print p.course #Python
登入後複製

以上是詳解詳細介紹l了Python類別的繼承的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板