Python专题, 语言

Python中类和继承的使用

这是之前的一篇:Python和Numpy库的常用语句,主要提及Python中循环,判断,函数,文件写入等操作,本篇给出Python中类和继承的使用例子。

学习代码如下:

"""
This code is supported by the website: https://www.guanjihuan.com
The newest version of this code is on the web page: https://www.guanjihuan.com/archives/37730
"""

class Animal:
    age0 = 0  # 类的属性(变量)

    def __init__(self, name): # 如果子类定义了__init__方法,它将覆盖父类的__init__方法
        self.name = name
        print('父类的初始化实例!')

    def make_sound(self): # 方法(函数)
        print(f'Animal {self.name} is making sound.')  # 如果子类中有相同名字的方法,那么会覆盖它


class Dog(Animal):  # 继承Animal类
    def __init__(self, name, age):  # 在创建对象时初始化属性,其中,self也可以用其他名字,但默认用这个,表示内部的一个实例
        self.name = name  # 实例的属性(变量)
        self.age = age  # 实例的属性(变量)
        print('创建一个类的实例!')

    def bark(self): # 方法(函数)
        print(f"Dog {self.name} is barking.")


print(Animal.age0)  # 类的属性
print(Dog.age0)  # 类的属性
print()

an_animal = Animal('零号')   # 创建类的实例
print(an_animal.name) # 访问实例的属性
an_animal.make_sound() # 调用实例的方法
print()

my_dog = Dog(name="一号", age=3) # 创建类的实例
print(f"小狗的名字为:{my_dog.name}") # 访问实例的属性
print(f"小狗年龄为:{my_dog.age} 岁") # 访问实例的属性
my_dog.bark()  # 调用实例的方法
my_dog.make_sound()  # 调用实例的方法
my_dog.age = 4 # 更新属性值
print(f"小狗年龄修改为:{my_dog.age} 岁")
print()

another_dog = Dog(name="二号", age=1)  # 创建另一个实例
print(f"小狗的名字为:{another_dog.name}")  # 访问实例的属性
print(f"小狗年龄为:{another_dog.age} 岁")  # 访问实例的属性
another_dog.bark()  # 调用实例的方法
another_dog.make_sound()  # 调用实例的方法

运行结果:

0
0

父类的初始化实例!
零号
Animal 零号 is making sound.

创建一个类的实例!
小狗的名字为:一号
小狗年龄为:3 岁
Dog 一号 is barking.
Animal 一号 is making sound.
小狗年龄修改为:4 岁

创建一个类的实例!
小狗的名字为:二号
小狗年龄为:1 岁
Dog 二号 is barking.
Animal 二号 is making sound.
99 次浏览

【说明:本站主要是个人的一些笔记和代码分享,内容可能会不定期修改。为了使全网显示的始终是最新版本,这里的文章未经同意请勿转载。引用请注明出处:https://www.guanjihuan.com

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Captcha Code