python 参数类型的多态_【Python】面向对象:类与对象\封装\继承\多态

本文介绍了Python面向对象编程的基础概念,包括类与对象的定义、创建和使用,以及封装、继承和多态等核心特性。通过具体示例展示了如何在Python中实现面向对象的设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

六、Python面向对象--类与对象\封装\继承\多态

1、什么是面向对象编程

1.1 程序设计的范式:程序可控,易于理解

1.2 抽象并建立对象模型

1.3 程序是不同对象相互调用的逻辑。每个对象在代码中抽象出来其实就是类;描述类特征的成为属性;具体到某个类(独立的类)则成为实例

2、类与对象

2.1 类:是多个类似事物的统称。能够帮助我们快速理解和判断事物的性质

2.2 数据类型:不同的数据类型属于不同的类,使用内置函数type()可以查看数据类型

2.3 对象: (eg)100、40、999都是int类下包含的相似的不同个例,这个个例专业术语称为实例或对象,python中一切皆对象

2.4 类的创建

① 语法:通过class关键字定义一个类;类名以大写字母开头

② 类的组成:类属性、实例方法、静态方法、类方法

2.5 对象的创建

① 对象的创建又称为类的实例化

② 语法:实例名 = 类名()

③ 意义:有了实例,就可以调用类中的内容

2.6 类属性、类方法、静态方法

① 类属性:类中方法外的变量称为类属性,被该类的所有对象所共享

② 类方法:[email protected],使用类名直接访问的方法

③ 静态方法:[email protected],使用类名直接访问的方法

④ 一个例子

class Student: # Student 为类的名称(类名)由一个或多个单词组成,每个单词的首字母大写,其余小写

native_pace = ‘吉林‘ # 直接写在类里的变量,称为类属性

# 初始化方法

def __init__(self, name, age):

self.name = name # self.name 称为实例属性,进行了一个赋值操作,将局部变量的name的值赋给实例属性

self.age = age

# 实例方法

def eat(self): # 采用类名.方法名(类的对象)调用时,需要传入self

print(‘学生吃东西~~~~‘)

# 静态方法:[email protected]

@staticmethod

def method(): # 静态方法中不允许写self

print(‘这是静态方法‘)

# 类方法:[email protected]

@classmethod

def cm(cls): # 类方法中写cls,采用类名.方法名(类的对象)调用的时候不需要传入cls

print(‘这是类方法‘)

# 在类之内定义的称为方法,在类之外定义的称为函数

def drink(): # 定义在类之外,叫做函数

print(‘喝水‘)

print(Student) # ==>

print(type(Student)) # ==>

print(id(Student)) # ==>1669260589880

# 创建Student类的对象:根据类对象创建出来的对象,叫做实例对象

stu1 = Student(‘张三‘, 20)

print(stu1)

print(type(stu1))

print(id(stu1))

# 对象调用类方法一,可以直接采用 对象名.方法名()

stu1.eat() # ==>学生吃东西~~~~

print(stu1.name) # ==>张三

print(stu1.age) # ==>20

# 对象调用类方法二

Student.eat(stu1) # 类名.方法名(类的对象)-->实际上就是方法定义出的self

# 类属性的使用方式

print(Student.native_pace)

stu2 = Student(‘李四‘, 33)

print(stu1.native_pace)

print(stu2.native_pace)

Student.native_pace = ‘天津‘ # 修改类属性后,访问时所有值都改变

print(stu1.native_pace)

print(stu2.native_pace)

# 类方法的使用方式

Student.cm()

# 静态方法的使用方式

Student.method()

2.7 动态绑定属性和方法

① python是动态语言,在创建对象之后,可以动态地绑定属性和方法

② 一个例子

class Student:

def __init__(self, name, age):

self.name = name

self.age = age

def eat(self):

print(self.name + ‘在吃饭‘)

# 一个Student类可以创建多个Student的实例对象,每个实例对象可以相同也可以不同

stu1 = Student(‘张三‘, 20)

stu2 = Student(‘李四‘, 33)

# 为stu2动态绑定性别属性,但是stu1不存在性别属性

stu2.gender = ‘女‘

print(stu1.name, stu1.age)

print(stu2.name, stu2.age, stu2.gender)

# 为stu1单独绑定一个动态函数

def show():

print(‘定义在类之外的,称函数‘)

stu1.show = show # 动态绑定方法

stu1.show()

stu2.show() # stu2没有绑定这个方法,所以执行报错

3、面向对象三大特征

3.1 封装:提高程序的安全性

① 将数据(属性)和行为(方法)包装到类对象中,在方法内部对属性进行操作,在类对象的外部调用方法。这样,无需关心方法内部的具体实现细节,从而隔离了复杂度

② 在python中没有专门的修饰符用于属性的私有,如果该属性不希望在类对象外部被访问,前面使用两个‘_’.

③ 一个例子

class Student:

def __init__(self, name, age):

self.name = name

self.__age = age # 年龄不希望在类的外部被使用,所以加了两个__

def show(self):

print(self.name, self.__age)

stu = Student(‘张三‘, 20)

stu.show()

# 在类的外部使用name和age

print(stu.name)

print(stu.__age) # 出现报错,不能直接使用。AttributeError: ‘Student‘ object has no attribute ‘__age‘

print(dir(stu)) # 查看stu可以使用的全部属性和方法

print(stu._Student__age) # 在类的外部访问

3.2 继承:提高代码的复用性

① 语法

class 子类类名(父类1, 父类2, ...):

pass

② 如果一个类没有继承任何类,则默认继承object

③ python支持多继承

④ 定义子类时,必须在其构造函数中调用父类的构造函数:新类不需要重头编写,子类只需要实现缺少的新功能,继承父类所有的属性、功能

class Person(object): # Person 继承object类

def __init__(self, name, age):

self.name = name

self.age = age

def info(self):

print(self.name, self.age)

class Student(Person):

def __init__(self, name, age, stu_no):

super().__init__(name, age)

self.stu_no = stu_no

class Teacher(Person):

def __init__(self, name, age, teach_year):

super().__init__(name, age)

self.teach_year = teach_year

stu = Student(‘张三‘, 20, ‘1001‘)

tea = Teacher(‘老师‘, 33, 10)

stu.info()

tea.info()

⑤ 函数isinstance()可以判断一个变量的类型class Person(object)

class Person(object):

def __init__(self, name, gender):

self.name = name

self.gender = gender

class Student(Person):

def __init__(self, name, gender, score):

super(Student, self).__init__(name, gender)

self.score = score

class Teacher(Person):

def __init__(self, name, gender, course):

super(Teacher, self).__init__(name, gender)

self.course = course

p = Person(‘Tim‘, ‘Male‘)

s = Student(‘Bob‘, ‘Male‘, 88)

t = Teacher(‘Alice‘, ‘Female‘, ‘English‘)

# 在继承链上,一个父类的实例不能是子类类型,因为子类比父类多了一些属性和方法

print(isinstance(p, Person)) # ==>True # p是Person类型

isinstance(p, Student) # ==>False # p不是Student类型

isinstance(p, Teacher) # ==>False # p不是Teacher类型

# 在一条继承链上,一个实例可以看成它本身的类型,也可以看成它父类的类型

isinstance(s, Person) # ==>True # s是Person类型

isinstance(s, Student) # ==>True # s是Student类型

isinstance(s, Teacher) # ==>False # s不是Teacher类型

# isinstance也可以用于Python自有数据类型的判断

s = ‘this is a string.‘

n = 10

isinstance(s, int) # ==> False

isinstance(n, str) # ==> False

3.3 多态:提高程序的可扩展性和可维护性

① 多态就是“具有多种形态”,即使不知道一个变量所引用的对象到底时什么类型,仍然可以通过这个变量调用方法,在运行过程中根据变量所引用对象的类型,动态决定调用哪个对象中的方法

4、方法重写

4.1 如果子类对继承自父类的某个属性或者方法不满意,可以在子类中对其(方法体)进行重新编写

4.2 子类重写后的方法中可以通过super().xxx()调用父类中被重写的方法

4.3 一个例子

class Person(object): # Person 继承object类

def __init__(self, name, age):

self.name = name

self.age = age

def info(self):

print(self.name, self.age)

class Student(Person):

def __init__(self, name, age, stu_no):

super().__init__(name, age)

self.stu_no = stu_no

# 重写父类中的info方法

def info(self):

super().info() # 会先执行父类中的info方法

print(self.stu_no)

class Teacher(Person):

def __init__(self, name, age, teach_year):

super().__init__(name, age)

self.teach_year = teach_year

# 重写父类中的info方法

def info(self):

super().info() # 会先执行父类中的info方法

print(self.teach_year)

stu = Student(‘张三‘, 20, ‘1001‘)

tea = Teacher(‘老师‘, 33, 10)

stu.info()

tea.info()

5、object类

5.1 object类时所有类的父类,因此所有类都有object的属性和方法

5.2 内置函数dir()可以查看那指定对象的所有属性

5.3 object类有一个__str__()方法,用于返回一个对于“对象的描述”,对应于内置函数str()经常用于print()方法,帮助我们查看对象的信息,所以我们经常会对__str__()重写

class Student():

def __init__(self, name, age, stu_no):

self.name = name

self.age = age

self.stu_no = stu_no

def __str__(self):

return ‘我的名字是{}. 年龄是{}‘.format(self.name, self.age)

stu = Student(‘张三‘, 20, ‘1001‘)

print(stu) # 默认会调用__str__()方法

print(type(stu))

原文:https://www.cnblogs.com/xuanling/p/14422786.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值