一,基本语法
class MyClass(BaseClass):def __init__(self):print('...')
class MyDefineClass(object):def __init__(self):print('继承自object类')MyDefineClass.__init__(None)
# 属性访问 me = MyDefineClass()
# 实例对象'''
继承自object类
继承自object类
'''
二,父类在其它模块
class MyClass(ModuleName.BaseClass):def __init__(self):print('...')
- 相同目录下的Father模块的father类
# father模块
class father(object):var = 99def __init__(self):print('父类构造函数。')def raise_son(self):print('我要养育我的孩子。')def show(self):print('i am your dad~')
- Extend模块
from Father import fatherclass Child1(father):def __init__(self):print("继承father模块的Father类")def show(self):print('i am your son~')def test_child1():ch = Child1()ch.raise_son() # 拥有父类的非私有方法ch.show() # 拥有父类的方法, 被重写后就是自己的方法啦。print(ch.var) # 拥有父类的属性test_child1()
'''
继承father模块的Father类
我要养育我的孩子。
i am your son~
99
'''
- 单继承
extend模块
class people:# 基本属性name = ''age = ''count = 0 # 类变量# 私有属性,类的外部无法访问__height = 0def __init__(self, name, age, height):self.name = nameself.age = ageself.__height = heightpeople.count += 1print(self.__height)def show(self):print(f'I am {self.name}, age {self.age}, height {self.__height}', people.count)def test_people():p = people('jack', 13, 170)p.show()p1 = people('rose', 17, 176)p1.show()
father模块
# 单继承调用父类的构造函数
from Father import people
class student(people):grade = ''def __init__(self, name, age, height, grade):# 掉用父类的__init__函数people.__init__(self, name, age, height)self.grade = grade# 覆写父类的方法def show(self):print(f'I am {self.name}, age {self.age} and in {self.grade}')def test_stu():s = student('rye', 19, 168, 'colleage three')s.show()
# test_stu()'''
168 # people类里面的身高
I am rye, age 19 and in colleage three
'''
三,python 多继承
- 可以继承多个类
class MyClass(base1, base2, base3):def __init__(self):print('...')
- 调用父类方法优先搜索左边的类(下面有demo)
# 一个父类
class speaker():topic = ''name = ''def __init__(self, topic, name):self.topic = topicself.name = namedef show(self):print(f'I am a speecher, name {self.name}, my topic is {self.topic}')# sample 继承speaker 和 student,见上面的student
class sample(speaker, student): # speaker的方法优先def __init__(self, topic, name, age, height, grade):speaker.__init__(self, topic, name) student.__init__(self, name, age, height, grade)def test_sample():T = sample('python', 'jack', 25, 180, 10)T.show() # 多重继承里面方法调用优先级是括号左边的父类# test_sample()
'''
180 people中打印的身高
I am a speecher, name jack, my topic is python
'''
-
尽量不要用多重继承!!!
-
只会降低性能,一个个的找父类的方法
-
还会把代码搞复杂
四,方法重写
- super(type, self) type 子类 self是子类的实例;如果在类里面使用 直接super()
# 重写
class parent:def say(self):print('调用父类方法')class child(parent):def say(self):print('调用子类方法')def test_overwirte():c = child()c.say() # 子类实例使用父类方法super(child, c).say() # 子类调用父类覆盖的方法# test_overwirte()
'''
调用子类方法
调用父类方法
'''
五,私有属性
-
类的私有属性
私有属性
self.__attribute, 只能在本身类使用
私有方法
self.__method_name, 只能在本来使用 -
私有属性
# 私有属性
class MyCounter():__private_count = 0public_count = 0def count(self):self.__private_count += 1self.public_count += 1print('私有变量 ', self.__private_count)def test_count():x = MyCounter()x.count()x.count()print('public 变量', x.public_count)# print(x.__private_count) 私有变量不能访问
'''
私有变量 1
私有变量 2
public 变量 2
'''
- 私有方法
class website:def __init__(self, name, url):self.name = nameself.__url = urldef show(self):print('name ', self.name)print('url ', self.__url)def __func(self):print('这是私有方法')def func(self):self.__func() # 私有方法只能本类调用print('这是共有方法')def test_site():w = website('csdn', 'csdn.net')w.func()w.show()# w.__func() # AttributeError: 'website' object has no attribute '__func' test_site()
'''
这是私有方法
这是共有方法
name csdn
url csdn.net
'''