Python Class & Inheritance

本文介绍了Python中的类(class)、初始化方法(init)、self关键字、类函数、单继承、多继承以及函数重写。强调了self代表实例而非类,且不是Python的保留关键字。讨论了如何访问实例属性和私有属性,以及继承规则和函数覆盖。
摘要由CSDN通过智能技术生成

class

class myclass:
  
  i = 12345
  
  def f(self):
    return 'willy is handsome'
  
#將 myclass 實例化
x = myclass()

#訪問 class 裡的屬性 & 方法
print(x.i)
print(x.f())

>>> 12345
>>> willy is handsome

這裡看到 f(self) 的 self 指的是 class 用來創建 instance 本身 (下面有詳細介紹)
然後將 x 物件化成 myclass 的樣子

init()

class id:
  def __init__(self , name , age):
    self.n = name
    self.a = age

x = id('willy' , 21)
print(x.n , x.a)

>>> willy 21

init(self , name , age) 這個構造函數的左右下劃線都是兩個,我只用了一個,導致錯誤 TypeError: object() takes no parameters

self

  • self 代表 class 的 instance,而非 class
class test:
  def prt(self):
    print(self)
    print(self.__class__)
    
x = test()

print(x.prt())

>>> <__main__.test object at 0x7f1681830940>
>>> <class '__main__.test'>

self 代表當前 instance x 的 address,而 self.class 是指向 class
self 不是 python 的 keyword,所以隨便亂換個名字也可以

class function

class people:
  
  # 定義 basic attribute
  name = ''
  age = 0
  
  # 定義 private attribute , 外部無法直接訪問
  __phone = 0
  
  def __init__(self,n,a,p):
    self.name = n
    self.age = a
    self.__phone = p
    
  def ptr(self):
    print("%s is %d years old" %(self.name,self.age))
    
x = people('willy',21,'0988XXXXXX')
x.ptr()

>>> willy is 21 years old

比如說 x.name 會得到 ‘willy’ 而 x.age 會得到 21 ,但是 x.__phone 卻得到 AttributeError: 'people' object has no attribute '__phone'
private function 也一樣 e.g. def ptr(self): 變成 def __ptr(self):
但如果將 ptr function 改成 print("%s is %d years old and phone %s" %(self.name,self.age,self.__phone))
會得到 willy is 21 years old and phone 0988XXXXX , 因為他是內部引用而不是外部引用

single inheritance

class people:
  
  # 定義 basic attribute
  name = ''
  age = 0
  
  # 定義 private attribute
  __phone = 0
  
  def __init__(self,n,a,p):
    self.name = n
    self.age = a
    self.__phone = p
    
  def ptr(self):
    print("%s is %d years old" %(self.name,self.age))
    
# single inheritance

class student(people):
  
  grade = ''
  
  def __init__(self,n,a,p,g):
    
    # 調用 father class 的 constructor
    people.__init__(self , n , a , p)
    self.grade = g
    
  # rewrite father class function
  def ptr(self):
    print("%s is %d years old and study in %s" %(self.name,self.age,self.grade))
          
x = student('willy',21,'0988XXXXXX','Third grade')
x.ptr() 

>>> willy is 21 years old and study in Third grade3

multiple inheritance

# other class

class favorite:
  
  name = ''
  food = ''
  
  def __init__(self,n,f):
    self.name = n
    self.food = f
  def ptr(self):
    print("%s's favorite food is %s" %(self.name,self.food))
    
# multiple inheritance

class mix(favorite,student):
  
  def __init__(self,n,a,p,g,f):
    favorite.__init__(self,n,f)
    student.__init__(self,n,a,p,g)
  
x = mix('willy',21,'09XXXX','third grade','拉麵')
x.ptr()

>>> willy's favorite food is 拉麵

繼承 function 相同會優先引用左邊的(左到右),在這 favorite 在左邊

rewrite function

class father:
  def say(self):
    print("I'm your father")
  
class child(father):
  def say(self):
    print("I'm your child")
    
s = child()  # son instance
s.say()      # son rewrite function
super(child,s).say()  # 用 child 調用 father 已經被 override 的 say function

>>> I'm your child
>>> I'm your father
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值