python学习34:继承


1. 继承

  • 继承具有传递性;
  • 当父类方法的实现不能满足子类的需求的时候,可以对父类方法进行重写:1. 覆盖父类方法;2. 对父类的方法进行扩展。
class Animal:
    def eat(self):
        print('吃!!!')
    def drink(self):
        print('喝!!!')
    def run(self):
        print('跑!!!')
    def sleep(self):
        print('睡!!!')

class Cat(Animal):
	# Cat类继承了Animal类
	def call(self):
        print('喵喵!!')

class Hellokitty(Cat):
    def speak(self):
        print('我可以说话')
    def call(self):
        # 调用原本在父类中封装的方法
        super().call()
        # Cat.call(self) python2.x
        # 针对子类特有的需求 编写代码
        print('#%@@#$#@%')

class Dog(Animal):
    pass

cat = Hellokitty()
cat.eat()
cat.drink()
cat.run()
cat.call()

dog = Dog()
dog.run()
dog.eat()

在这里插入图片描述

2. 类的结构

  • 使用面向对象开发,第一步是设计类;
  • 使用 类名() 创建对象,创建对象的动作有两种:1. 在内存中为对象分配空间;2. 调用初始化方法__init__为对象初始化。
  • 对象创建后,内存中就有了一个对象(时时在在的实例)。因此,1. 创建出来的对象叫做类的实例;2. 创建对象的动作叫做实例化;3. 对象的属性叫做实例属性;4. 对象调用的方法叫做实例方法。
  • 在程序执行时:1. 对象各自拥有自己的实例属性;2. 调用对象的方法,可以通过self访问自己的属性,调用自己的方法。
  • 每一个对象都有自己独立的内存空间,保存各自不同的属性;多个对象的方法,在内存中只有一份,在调用方法时,需要把对象的引用传递到方法内部。
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(A,B):
    pass
c = C()
c.test()
c.demo()

在这里插入图片描述

3. 新式类和旧式(经典)类

  object是python为所有对象提供的基类,提供有一些内置的属性和方法,可以使用dir函数查看。新式类:以object为基类的类。经典类:不以object为基类的类。
  在python3.x中定义类时,如果没有指定父类,会默认使用object作为基类—python3.x中定义的类都是新式类。
  在python2.x中定义类时,如果没有指定父类,则不会以object作为基类。
  为保证编写的代码能够同时在python2.x和python3.x运行,今后在定义类时,如果没有父类,建议同意继承自object。

# python2.x:经典类,在没有父类时,没有使用object作为基类

[dd@foundation0 bin]$ python
Python 2.7.5 (default, Aug  2 2016, 04:20:16)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
...     pass
...
>>> dir(A)
['__doc__', '__module__']
>>> class A(object):
...     pass
...
>>> dir(A)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
# python3.x:类为新式类

[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 B :
... 	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 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 Bird(object):
    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 = 'aaaaaa'
    def sing(self):
        print(self.sound)

bird = SongBird()
bird.eat()
bird.eat()
bird.sing()

在这里插入图片描述

发布了106 篇原创文章 · 获赞 1 · 访问量 2372

猜你喜欢

转载自blog.csdn.net/weixin_43384009/article/details/104105149
今日推荐