一、类的基本概念与定义
1.类是指相同事物的集合,对象是指类中的具体实例。
2.创建类
class 类的名称():
'''类的说明文档'''
def __init__(self,参数1,参数2):
代码块1
def 方法名1(参数3,参数4……):
代码块2
def 方法名2(参数5,参数6……):
代码块3
类的定义方式,使用class关键字。在定义类时,可以定义变量(属性)和函数(方法)
类属性:在类中定义的变量,是公共属性,
访问:类属性可以通过类或类的实例访问到(对象.属性名)
修改:类属性只能通过类对象来修改,无法通过实例对象修改
实例属性:通过实例对象添加的属性属于实例属性(在init()方法中定义)
实例属性只能通过实例对象来访问和修改(对象.属性名),类对象无法访问修改
方法:在类中定义的函数(方法),可以通过该类的所有实例来访问(对象.方法名())
实例方法:在类中定义,以self为第一个参数的方法都是实例方法
实例方法在调用时,python会将调用对象作为self传入
实例方法可以通过实例和类去调用
当通过实例调用时,会自动将当前调用对象作为self传入
当通过类调用时,不会自动传递self,此时我们必须手动传递self
类方法的第一个参数是cls,也会被自动传递,cls就是当前的类对象
实例方法的第一个参数是self,而类方法的第一个参数是cls
类方法可以通过类去调用,也可以通过实例调用,没有区别
3.创建对象
对象=类名(实参)
- 创建一个变量(对象名)
- 在内存中创建一个新对象(类的实例),如果有init(self)方法则执行
- 将对象的id赋值给变量
创建完对象后就可以通过对象名使用类中的方法。使用形式如下:
对象.方法名(参数)
import time
class Microwave():
'''微波炉类的文档说明'''
def __init__(self,name):
self.name=name
self.time=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
self.bread(10)
def print_info(self):
print(' 微波炉的名字是:',self.name,'\n','购买时间是:',self.time)
def bread (self,temp=3):
self.temp = temp
print(" 开始烤面包,时长为:",self.temp,'分钟')
pass
##类的定义##
a = Microwave('zhangsan')
print(a.name,a.time)
print(a.temp)
##类的使用##
二.类的属性
类的属性是指给每个对象赋予的标签信息。所有实例对象共享同一份类属性内存空间
1.属性的使用
a=Microwave('zhangsan')
a.bread()
print(a.temp)
2.初始化的其他方法
import time
class Microwave():
'''微波炉类的文档说明'''
def __init__(self,name):
self.name=name
self.time=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
def print_info(self):
print(' 微波炉的名字:',self.name,'\n','购买时间:',self.time)
def bread (self,temp=3):
self.temp = temp
print(" 开始烤面包,时长:",self.temp,'分钟')
pass
##a = Microwave('zhangsan')
##print(a.name,a.time)
##print(a.temp)
a = Microwave('zhangsan')
a.bread()
print(a.temp)
在类中可以定义一些特殊方法
特殊方法都是以__开头,__结尾的方法
特殊方法一般不需要自己调用(不建议手动调用),特殊方法将会在特殊的时刻自动调用
_init_()是常用的特殊方法
创建对象时执行,创建以后结束执行(可以用来对新创建的对象初始化属性)
创建对象的流程:bread= trmp(3)
创建一个变量bread
在内存中创建一个新对象(Microwave实例)
_init_(self)方法执行
将对象的id赋值给bread
3.属性的修改
import time
class Microwave():
'''微波炉类的文档说明'''
def __init__(self,name):
self.name=name
self.time=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
def print_info(self):
print(' 微波炉的名字是:',self.name,'\n','购买时间是:',self.time)
def bread (self,temp=3):
self.temp = temp
print(" 开始烤面包,时长为:",self.temp,'分钟')
pass
a = Microwave('zhangsan')
a.name = 'lisi'
a.time = '2022年6月7日'
print(a.name,a.time)
通过a.name='lisi',a.time='2022年6月7日'直接对属性name和time进行赋值。
4.获取类的文档说明
a = Microwave('zhangsan')
print(a.__doc__)
三.类的继承
1.核心作用
代码复用:子类自动获取父类的属性和方法 。
扩展功能:子类可新增属性/方法或重写父类逻辑。
继承方式如下:
class 子类名(父类名):
def __init__(self,子参数):
super().__init__(父参数)
代码块
def 方法1():
import time
class Microwave():
'''微波炉类的文档说明'''
def __init__(self,name):
self.name=name
self.time=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
def print_info(self):
print(' 微波炉的名字是:',self.name,'\n','购买时间是:',self.time)
def bread (self,temp=3):
self.temp = temp
print(" 开始烤面包,时长为:",self.temp,'分钟')
pass
##父类##
class New_Microwave(Microwave):
'''从Microwave继承的子类'''
def __init__(self,name,light):
super().__init__(name)
self.light = light
##子类##
a=New_Microwave('zhangsan',100)
a.print_info()
##使用子类##
2.在子类中添加方法
class New_Microwave(Microwave):
'''从Microwave继承的子类'''
def __init__(self,name,light):
super().__init__(name)
self.light = light
def set_light(self,add_light):
self.light=self.light+add_light
print("当前灯光强度为:",self.light)
pass
a=New_Microwave('zhangsan',100)
a.set_light(50)
b = Microwave('zhangsan')
b.set_light(50)
3.覆盖父类的方法
class New_Microwave(Microwave):
'''从Microwave继承的子类'''
def __init__(self,name,light):
super().__init__(name)
self.light = light
def print_info(self):
print(' 微波炉的名字是:',self.name,'\n','购买时间是:',self.time,'\n','当前灯光强度时:',self.light)
a=New_Microwave('zhangsan',100)
a.print_info()
四.导入其他文件中的类
1.直接导入
import c
2.导入文件中的指定类
from c import Microwave
3.导入文件中的全部类
from c import *
4.给导入的类设置别名
from c import Microwave as Mw
五.在类中使用另一个类方法
class ClassB:
def method_b(self):
print("ClassB 的方法被调用")
class ClassA:
def method_a(self):
b_instance = ClassB() # 实例化ClassB
b_instance.method_b() # 调用ClassB的方法

74

被折叠的 条评论
为什么被折叠?



