【Python学习】—面向对象(九)

【Python学习】---面向对象(九)

一、初识对象





类中不仅可以定义属性来记录数据,也可以定义函数,用来记录行为,类中定义的属性(变量)我们称之成员变量,类中定义的行为(函数),我们称之为成员方法。

成员方法的定义语法


self的作用

  • 表示对象本身的意思
  • 只有通过self,成员方法才能访问类的成员变量
  • self出现在形参列表中,但是不占用参数位置,无需例会
bash 复制代码
class Student:
      name=None

      def say_hi(self):
          print(f"大家好,我是{self.name},欢迎大家多多关照")

stu=Student()
stu.name="张三"
stu.say_hi()

二、基于类创建对象

bash 复制代码
class Clock:
    id=None
    price=None

    def ring(self):
        import winsound
        winsound.Beep(2000, 3000)



clock1=Clock()
clock1.id='1112'
clock1.price=233
print(f"闹钟ID:{clock1.id},价格{clock1.price}")
clock1.ring()

clock2=Clock()
clock2.id='21122'
clock2.price=99.00
print(f"闹钟ID:{clock2.id},价格{clock2.price}")
clock2.ring()

三、构造方法

Python类可以使用:_init_()方法,称为构造方法

可以实现:

  • 在创建类对象(构造类)的时候,会自动执行
  • 在创建类对象( 构造类)的时候,将传入的参数自动传递给_init_方法使用
bash 复制代码
class Student:
    name = None
    age=None
    tel=None

    def __init__(self,name,age,tel):
        self.name=name
        self.age=age
        self.tel=tel
        print("student类创建了一个对象")

stu=Student("Cai",20,'13222222222')
print(stu.name)
print(stu.age)
print(stu.tel)

构造方法注意事项


四、内置方法

__init__构造方法,是Python类内置的方法之一,这些内置的类方法,各自有各自特殊的功能,这些内置方法我们称为魔术方法


bash 复制代码
class Student:
    def __init__(self,name,age):
        self.name=name
        self.age=age

    def __lt__(self,other):
        return self.age<other.age
stu1=Student("张三",20)
stu2=Student("李四",17)

print(stu1<stu2)
print(stu1>stu2)
bash 复制代码
class Student:
    def __init__(self,name,age):
        self.name=name
        self.age=age

    def __le__(self,other):
        return self.age <= other.age
stu1=Student("张三",20)
stu2=Student("李四",20)

print(stu1<=stu2)#True
print(stu1>=stu2)#True
bash 复制代码
class Student:
    def __init__(self,name,age):
        self.name=name
        self.age=age

    def __eq__(self,other):
        return self.age == other.age
stu1=Student("张三",20)
stu2=Student("李四",20)

print(stu1==stu2)#True

总结

五、封装

面向对象的三大特性:继承、封装、多态

封装


私有成员

私有成员无法被类对象使用,但是可以被其他的成员使用

继承


bash 复制代码
class 类名(父类名):
  内容体

pass的关键字

pass是一个普通的占位符,保证函数或者类定义的完整性,表示无内容、空的意思

复写

在子类中重新定义同名的属性或方法

六、类型注解语法


bash 复制代码
var_1: int=10
var_2: str="caicai"
var_3: bool=True

class Student:
    pass
stu:Student=Student()

my_list:list=[1,2,3]
my_tuple:tuple=(1,2,3)
my_dict:dict={"caicai":123}


my_list:list[int]=[1,23,3]
my_tuple:tuple[int,str,bool]=(1,"cai",True)


函数类型注解

七、多态

多态指的是:多种状态,即完成某个行为时,使用不同的对象会得到不同的状态


相关推荐
阿尔的代码屋3 小时前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
AI探索者20 小时前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者20 小时前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh1 天前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅1 天前
Python函数入门详解(定义+调用+参数)
python
曲幽1 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
两万五千个小时1 天前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿1 天前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户8356290780512 天前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng82 天前
Python+Django+H5+MySQL项目搭建
python·django