Python学习笔记_类与对象Ⅰ


类是多个类似事物组成的群体的统称。能够帮助我们快速理解和判断事物的性质。
数据类型
不同的数据类型属于不同的类,使用内置函数type()查看数据类型
对象
100,99,520都是int类之下包含的相似的不同个例,这个个例专业术语称为实例或对象

类的创建

class Student:  # Student 为类的名称。类名由一个或多个单词组成。类名的规范:每个单词的首字母大写,其余小写
    pass  # pass占位
# Python中一切皆对象,对象有id type value


print(id(Student))  # id为2532855694208(每次执行的id都不同)
print(type(Student))  # type为 <class 'type'>
print(Student)  # value为<class '__main__.Student'>
class Student:  # Student 为类的名称。类名由一个或多个单词组成。类名的规范:每个单词的首字母大写,其余小写
    native_pace = '吉林'  # 直接写在类里的变量,称为类属性
    
    def __init__(self, name, age):  
        self.name = name  # self.name 称为实例属性,进行了一个赋值的操作,将局部变量name的值赋给实体属性
        self.age = age
        
    # 实例方法  在类之外定义的称为函数,在类之内定义的称为方法
    def eat(self):
        print('学生在吃饭...')
    
    # 静态方法
    @staticmethod
    def method():
        print('我使用了staticmethod进行修饰,所以我是静态方法')
        
    # 类方法
    @classmethod
    def cm(cls):
        print('我是类方法,因为我使用了classmethod进行修饰')
        

对象的创建
对象的创建又称为类的实例化,有了实例,就可以调用类中的内容
语法 实例名 = 类名()

class Student:  # Student 为类的名称。类名由一个或多个单词组成。类名的规范:每个单词的首字母大写,其余小写
    # 类属性
    native_pace = '吉林'  # 直接写在类里的变量,称为类属性

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

    # 实例方法  在类之外定义的称为函数,在类之内定义的称为方法
    def eat(self):
        print('学生在吃饭...')

    # 静态方法
    @staticmethod
    def method():
        print('我使用了staticmethod进行修饰,所以我是静态方法')

    # 类方法
    @classmethod
    def cm(cls):
        print('我是类方法,因为我使用了classmethod进行修饰')


# 创建Student类的对象
stu1 = Student('张三', 20)
stu1.eat()  # 学生在吃饭...
print(stu1.name)  # 张三
print(stu1.age)  # 20

类属性、类方法、静态方法
类属性:类中方法外的变量称为类属性,被该类的所有对象所共享
类方法: 使用@classmethod修饰的方法,使用类名直接访问的方法
静态方法:使用@staticmethod修饰的方法,使用类名直接访问的方法

class Student:  # Student 为类的名称。类名由一个或多个单词组成。类名的规范:每个单词的首字母大写,其余小写
    # 类属性
    native_pace = '吉林'  # 直接写在类里的变量,称为类属性

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

    # 实例方法  在类之外定义的称为函数,在类之内定义的称为方法
    def eat(self):  # 实例方法的默认参数为self
        print('学生在吃饭...')

    # 静态方法
    @staticmethod
    def method():  # 静态方法没有默认参数
        print('我使用了staticmethod进行修饰,所以我是静态方法')

    # 类方法
    @classmethod
    def cm(cls):  # 类方法的默认参数为cls
        print('我是类方法,因为我使用了classmethod进行修饰')


# 类属性的使用方式
print(Student.native_pace)  # 吉林
stu1 = Student('张三', 20)
stu2 = Student('李四', 30)
print(stu1.native_pace)  # 吉林
print(stu2.native_pace)  # 吉林
Student.native_pace = '天津'
print(stu1.native_pace)  # 天津
print(stu2.native_pace)  # 天津
# 类方法的使用方式
Student.cm()  # 我是类方法,因为我使用了classmethod进行修饰
# 静态方法的使用方式
Student.method()  # 我使用了staticmethod进行修饰,所以我是静态方法

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

# 动态绑定属性与方法
class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def eat(self):
        print(self.name+'在吃饭')


stu1 = Student('张三', 20)
stu2 = Student('李四', 30)
# 给stu2动态绑定属性,直接用   对象名.添加的属性 = 希望赋的值
print('-------------为stu2动态绑定性别属性--------------')
stu2.gender = '女'
print(stu1.name, stu1.age)
print(stu2.name, stu2.age, stu2.gender)  # 李四 30 女


# 给stu1动态绑定方法,
def show():
    print('将函数show绑定为方法')    # 现在show还只是一个函数


stu1.show = show  # 给stu1动态绑定方法show
stu1.show()  # 将函数show绑定为方法

# stu2.show()  # 报错,因为show没有与stu2绑定
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值