2021-09-01 python基础知识学习:类

1.nonlocal关键字

在这里插入图片描述

2. LEGB原则

在这里插入图片描述

3. 类

(1)对象

将不同类型的数据、方法(即函数)放到一起,就是对象。
def init(self,name,score)
类:


#创建类
class Student:#类名首字母要大写
    def __init__(self,name,score):#self指当前对象本身
        self.name = name
        self.score = score

    def say_score(self):#self必须位于第一个参数
        print('{0}的分数是:{1}'.format(self.name,self.score))
        
s1 = Student("bai",18)#通过类名调用构造函数
s1.say_score()
(2)实例属性

init()构造方法:初始化创建好的对象,就是给实例属性赋值
第一个参数固定,必须是self
new():用于创建对象,但是一般不用声明
实例属性从属于实例对象

#接着上个代码
s1.salary = 3000#可以继续添加属性,但是不会改变原来的类属性(模具)
print(s1.salary)
s2 = Student("selene",20)#这里的s2没有新加的属性,是按原来的模具创建的
(3)实例方法

实例方法从属于实例对象
在这里插入图片描述

a = Student("selene",18)
a.say_score()
Student.say_score(a)
'''结果
selene的分数是:18
selene的分数是:18
'''

其他操作:

print(dir(a))#打印a的所有属性
print(a.__dict__)#打印对象的属性字典
class Man:
    pass         #定义了一个对象但是没有想好写啥
'''
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name', 'say_score', 'score']
{'name': 'selene', 'score': 18}
'''
(4)类对象
class Student:#类名首字母要大写
    def __init__(self,name,score):#self指当前对象本身
        self.name = name
        self.score = score

    def say_score(self):#self必须位于第一个参数
        print('{0}的分数是:{1}'.format(self.name,self.score))

print(type(Student))   #结果 type:模具类
print(id(Student))

Stu2 = Student#将Student类对象的id给Stu2
s1 = Student('ran',25)
s2 = Stu2('ran',25)

s1.say_score()
s2.say_score()
'''结果
<class 'type'>
2084072984136
ran的分数是:25
ran的分数是:25
'''
(5)类属性
class Student:
    company = 'SXT'   #类属性
    count = 0    #类属性

    def __init__(self,name,score):#self指当前对象本身
        self.name = name    #实例属性
        self.score = score   #实例属性
        Student.count = Student.count+1#使用的是类属性

    def say_score(self):#实例方法
        print('公司:',Student.company,end='\t')
        print('分数:',Student.count)
s1 = Student('高淇',80)#s1是实例对象,自动调用_init_()方法
s2 = Student('ran',60)#s2是实例对象,自动调用_init_()方法
s1.say_score()

print('共创建了{0}个Student对象'.format(Student.count))

在这里插入图片描述

(6)类方法

在这里插入图片描述

class Student:
    company = 'SWJTU'
    def __init__(self,name,age):#实例属性
        self.name = name
        self.age = age

    @classmethod
    def printCompany(cls):
        print(cls.company)
        #print(self.name),类方法不能调用实例属性

    @staticmethod
    def add(a,b):#静态方法,与对象的属性无关的方法
        print('{0}+{1}={2}'.format(a,b,a+b))
        return a+b

Student.printCompany()
Student.add(20,30)
(7)析构方法

_del_方法:析构方法,实现对象销毁
python中,当对象没有被引用时(引用计算为0),由垃圾回收器调用_del_方法,一般情况不需要写这个方法

class Person:
     def __del__(self):
         print('销毁对象{0}'.format(self))

p1 = Person()
p2 = Person()
print('over')
'''结果
over
销毁对象<__main__.Person object at 0x000002C48C9E0808>
销毁对象<__main__.Person object at 0x000002C48C9E08C8>
'''
(8)_call_方法

定义了_call_方法的对象,称为 可调用的对象,即该对象可像函数一样被调用

class SalaryAccount():
    def __call__(self, salary):
        print('计算工资')
        year = salary *12
        day = salary//22.5
        return dict(yearSalary = year,daySalary = day)#返回一个字典
s = SalaryAccount()
print(s(3000))   #直接调用这个实例对象
'''结果
计算工资
{'yearSalary': 36000, 'daySalary': 133.0}
'''
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值