学习python仅此一篇就够了(面向对象)

面向对象

使用对象组织数据

1.在程序中设计表格,我们称之为:设计类(class)

python 复制代码
class Student:
	name = None #记录学生姓名

2.在程序中打印生产表格,我们称之为:创建对象

python 复制代码
#基于类创建对象
stu_1 = Student()
stu_2 = Student()

3.在程序中填写表格,我们称之为:对象属性赋值

python 复制代码
stu_1.name = "周杰伦" #为学生1对象赋予名称属性值
stu_2.name = "wyx"   #为学生2对象赋予名称属性值
python 复制代码
#设计一个类
class studet:
    name = None
    gender = None
    age = None
    native_place = None


#创建一个对象(类比生活中:打印一张登记表)
stu_1 = studet()

#对象属性进行赋值
stu_1.name = "wyx"
stu_1.gender = "女"
stu_1.age = "22"
stu_1.native_place = "中国"

#获取对象中记录的信息
print(stu_1.name)
print(stu_1.age)
print(stu_1.gender)
print(stu_1.native_place)
wyx
22
女
中国

类的成员方法

使用语法:

python 复制代码
class 类名称:
	类的属性
	类的行为
  • class是关键字,表示要定义类

  • 类的属性,即定义在类中的变量

  • 类的行为,即定义在类中的函数

创建类对象的语法:

对象 = 类名称()

成员方法的定义语法

python 复制代码
def 方法名(self, 形参1,....形参N)
	方法体

self关键字是成员方法定义的时候,必须填写的。

  • 它用来表示类对象自身的意思

  • 当我们使用类对象调用方法的是,self会自动被python传入

  • 在方法内部,想要访问类的成员变量,必须使用self

python 复制代码
class student:
    name = None

    def stu_1(self):
        print("大家好")

    def stu_2(self,msg):
        print(f"很高兴认识那么,{msg}")

stu = student()
stu.name = "wyx"
print(stu.name)
stu.stu_1()
stu.stu_2("很高兴认识搭建")
wyx
大家好
很高兴认识那么,很高兴认识搭建

类和对象

python 复制代码
#设计一个闹钟类
class clock:
    id = None
    price = None

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

#构建2个闹钟对象并让他工作
clock1 = clock()
clock1.id = "001"
clock1.price = 10
print(f"闹钟ID为{clock1.id},价格为{clock1.price}")
clock1.ring()

构造方法

python类中可以使用:init()方法,称之为构造方法:

  • 在创建类对象(构造类)的时候,会自动执行

  • 在创建类对象(构造类)的时候,将传入的参数自动传递给init方法使用

python 复制代码
class stu:
    name = None
    age = None
    tel = None #可以省略


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


stu = stu("wyx", 20, "12311321")
print(stu.name)
print(stu.age)
print(stu.tel)
#student创建了一个类对象
wyx
20
12311321

魔术方法

1.str字符串方法

python 复制代码
class stu:

    def __init__(self, name, age, tel):
        self.name = name
        self.age = age
        self.tel = tel

    def __str__(self):
        return f"name:{self.name},age:{self.age}, tel:{self.tel}"

tu = stu("wyx", 20, "12311321")
print(tu) #name:wyx,age:20, tel:12311321
print(str(tu)) #name:wyx,age:20, tel:12311321

2.lt小于符号比较方法

  • 传入参数:other,另一个类对象

  • 返回值:True或False

python 复制代码
class stu:

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __lt__(self, other):
        return self.age < other.age
tu = stu("wyx", 20)
tu2 = stu("sxx", 10)
print(tu < tu2) #false
print(tu > tu2) #true

3.le小于等于,大于等于

python 复制代码
class stu:

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __le__(self, other):
        return self.age <= other.age

tu = stu("wyx", 20)
tu2 = stu("sxx", 10)
print(tu <= tu2) #false
print(tu >= tu2) #true

4.eq

python 复制代码
class stu:

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __le__(self, other):
        return self.age == other.age

tu = stu("wyx", 20)
tu2 = stu("sxx", 10)
print(tu == tu2) #false
相关推荐
蛋仔聊测试4 分钟前
Playwright 中特定的 Fixtures
python
GO兔9 分钟前
开篇:GORM入门——Go语言的ORM王者
开发语言·后端·golang·go
蹦蹦跳跳真可爱58912 分钟前
Python----大模型(使用api接口调用大模型)
人工智能·python·microsoft·语言模型
好开心啊没烦恼23 分钟前
Python 数据分析:numpy,抽提,整数数组索引与基本索引扩展(元组传参)。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy·pandas
清幽竹客28 分钟前
Day 3:Python模块化、异常处理与包管理实战案例
python
许白掰1 小时前
【stm32】HAL库开发——CubeMX配置RTC,单片机工作模式和看门狗
stm32·单片机·嵌入式硬件·学习·实时音视频
future14121 小时前
C#学习日记
开发语言·学习·c#
菜包eo1 小时前
二维码驱动的独立站视频集成方案
网络·python·音视频
Yo_Becky2 小时前
【PyTorch】PyTorch预训练模型缓存位置迁移,也可拓展应用于其他文件的迁移
人工智能·pytorch·经验分享·笔记·python·程序人生·其他
king_harry2 小时前
Java程序-OceanBase Connector/J 示例
开发语言