学习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
相关推荐
databook13 小时前
Manim实现闪光轨迹特效
后端·python·动效
Juchecar15 小时前
解惑:NumPy 中 ndarray.ndim 到底是什么?
python
用户83562907805115 小时前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_15 小时前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi
数据智能老司机21 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机1 天前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i1 天前
drf初步梳理
python·django
每日AI新事件1 天前
python的异步函数
python