"""
封装:根据职责将属性和方法封装到一个抽象的类中
继承:实现代买的重用 相同的代码不需要重复的写
"""
class Animal:
def eat(self):
print('吃~~~')
def drink(self):
print('喝~~~')
def run(self):
print('跑~~~')
def sleep(self):
print('睡~~~')
class Cat(Animal):
# def eat(self):
# print('吃~~~')
# def drink(self):
# print('喝~~~')
# def run(self):
# print('跑~~~')
# def sleep(self):
# print('睡~~~')
def call(self):
print('喵喵')
class Dog(Animal):
# def eat(self):
# print('吃~~~')
# def drink(self):
# print('喝~~~')
# def run(self):
# print('跑~~~')
# def sleep(self):
# print('睡~~~')
def bark(self):
print('汪汪')
fentiao = Cat()
fentiao.eat()
fentiao.drink()
fentiao.run()
fentiao.sleep()
fentiao.call()
wangcai = Dog()
wangcai.eat()
wangcai.bark()
"""
继承具有传递性
当父类方法不能满足子类的需求的时候
可以对方法进行重写
1.覆盖父类方法
2.对父类的方法进行扩展
"""
class Animal:
def eat(self):
print('吃~~~')
def drink(self):
print('喝~~~')
def run(self):
print('跑~~~')
def sleep(self):
print('睡~~~')
class Cat(Animal):
def call(self):
print('喵喵')
class HelloKitty(Cat):
def speak(self):
print('我可以说日语!!')
def call(self): ##如果子类重写了父类的方法,在运行中,只会调用在子类中重写的方法而不会调用父类方法
# 调用原本在父类中封装的方法
#super().call()
# Cat.call(self) python2.x
print('$#%$#%$#')
kt = HelloKitty()
kt.eat()
kt.run()
kt.call()
kt.speak()
"""
类的结构
术语--实例
1.使用面向对象开发,第一步是设计类
2.使用 类名() 创建对象,创建对象的动作有两步
1.在内存中为对象分配空间
2.调用初始化方法__init___ 为对象初始化
3.对象创建后,内存中就有了一个对象的实实在在的存在--实例
因此:
1.创建出来的对象叫做类的实例
2.创建对象的动作叫做实例化
3.对象的属性叫做实例属性
4.对象调用的方法叫做实例方法
在程序执行时:
1.对象各自拥有自己的实例属性
2.调用对象的方法,可以通过self
访问自己的属性
调用自己的方法
结论:
1.每一个对象都有自己独立的内存空间,保存各自不同的属性
2.多个对象的方法,在内存中之有一份,在调用方法时,
需要把对象的引用传递到方法内部
"""
class Bird:
def __init__(self):
self.hungry = True
def eat(self):
if self.hungry:
print('@#%#%#@')
self.hungry =False
else:
print('No Thanks')
# 在鸟的基础上 增加唱歌的功能
class SongBird(Bird):
def __init__(self):
super().__init__()
self.sound = 'aaaaa'
def sing(self):
print(self.sound)
brid = SongBird()
brid.eat()
brid.sing()
多继承
"""
多继承:子类拥有多个父类叫多继承,拥有所有父类的属性和方法
"""
class A:
def test(self):
print('A----test方法')
def demo(self):
print('A----demo方法')
class B:
def test(self):
print('B----test方法')
def demo(self):
print('B----demo方法')
class C(B,A):
##当父类的方法或属性同名时,按括号中的父类的先后顺序执行,谁在前执行谁
pass
c = C()
c.test()
c.demo()
“”"
新式类和旧式(经典)类:
object是Python为所有对象提供的基类,提供有一些内置的属性和方法,可以使用dir函数查看
新式类:以object为基类的类
经典类 不以object为基类的类
在python3.X中定义的类时,如果没有指定父类,会默认使用object作为基类--
python3.x中定义的类都是新式类
在python2.x中定义类时,如果没有指定父类,则不会以object作为基类
为保证编写的代码能够同时在python2.x和python3.x运行
今后在定义类时,如果没有父类,建议统一继承自object
[dd@foundation0 bin]$ /usr/local/python3/bin/python3
Python 3.6.4 (default, Aug 10 2018, 11:14:49)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
... pass
...
>>>
>>> dir(A)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
>>> class B(object):
... pass
...
>>> dir(B)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
"""
class A(object):
pass
class C(A):
pass
python3:
python2: