欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

【Python基础知识-pycharm版】第八节-面向对象编程/类

发布时间:2024/7/5 python 36 豆豆
生活随笔 收集整理的这篇文章主要介绍了 【Python基础知识-pycharm版】第八节-面向对象编程/类 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

面向对象编程

  • 面向对象和面向过程的区别_执行者思维_设计者思维
  • 类的定义
    • 构造函数__init__
    • 实例属性_内存分析
    • 实例方法_内存分析
    • 类对象
    • 类属性_内存分析创建类和对象的底层
    • 类方法_静态方法_内存分析图示
  • __del__()析构方法和垃圾回收机制
  • __call__()方法和可调用对象

面向对象和面向过程的区别_执行者思维_设计者思维






类的定义



class Student:#company= "SXT" #类属性#count =0 #类属性def __init__(self,name,score): #构造方法的第一个参数必须为selfself.name=name #实例属性self.score=score#Student.count=Student.count+1def say_score(self): #实例方法#print('我的公司是:',Student.company)print(self.name,'的分数是',self.score) s1 = Student('张三',80) s1.say_score() 张三 的分数是 80Process finished with exit code 0

构造函数__init__


实例属性_内存分析

实例方法_内存分析




class Student:#company= "SXT" #类属性#count =0 #类属性def __init__(self,name,score): #构造方法的第一个参数必须为selfself.name=name #实例属性self.score=score#Student.count=Student.count+1def say_score(self): #实例方法#print('我的公司是:',Student.company)print(self.name,'的分数是',self.score) s1 = Student('张三',80) s1.say_score() s1.age=32 s1.salary=3000 #del sl print(s1.salary)s2=Student('高希希',100) s2.say_score() Student.say_score(s2)print(s2.__dict__)print(isinstance(s2,Student))class Man:pass 张三 的分数是 80 3000 高希希 的分数是 100 高希希 的分数是 100 {'name': '高希希', 'score': 100} True

类对象


class Student:def __init__(self,name,score): #构造方法的第一个参数必须为selfself.name=name #实例属性self.score=score#Student.count=Student.count+1def say_score(self): #实例方法#print('我的公司是:',Student.company)print(self.name,'的分数是',self.score)s1 = Student('高琪',80) s2=Student('高希希',100) s1.say_score() s2.say_score() 高琪 的分数是 80 高希希 的分数是 100

类属性_内存分析创建类和对象的底层





类方法_静态方法_内存分析图示



del()析构方法和垃圾回收机制

call()方法和可调用对象


class SalaryAccount:def __call__(self,salary):print("算工资啦")yearSalary=salary*12daySalary=salary//22.5hourSalary=daySalary//8return dict(yearSalary=yearSalary,daySalary=daySalary,hourSalary=hourSalary) s=SalaryAccount() print(s(30000)) 算工资啦 {'yearSalary': 360000, 'daySalary': 1333.0, 'hourSalary': 166.0}

总结

以上是生活随笔为你收集整理的【Python基础知识-pycharm版】第八节-面向对象编程/类的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。