python 面向对象的初识

class Student:
    '''
    此类是构建学生类
    '''
    daily = '学习'
    examination = '考试'

    def __init__(self,n,a,h):
        self.name = n
        self.age = a
        self.hobby = h

        # self.n = 'yz'
        # self.sex = 'n'

        # print(self)
        # print(666)

    def work(self,c):
        self.color = c
        print(f'{self.name}每天上课')

    def homeworl(self):
        print('家庭作业')

# obj = Student()  #类名()过程就叫做实例化过程,实例化一个对象
#

#
# obj = Student()
# print(obj)  #对象,实例
#  实例化一个对象时发生了三件事:
# 1、在内存中创建一个对象空间
# 2、自动执行__init__方法,并且将对象空间传给self参数。
# 3、执行__init__方法里面的代码,给对象空间封装其属性。

# 从对象的角度研究类:
# 对象操作对象里边的属性。
# obj = Student()
# del obj.n #删
# obj.age = '24' #增加了
# obj.sex = 'shuai'
# print(obj.sex)
# print(obj.__dict__)

# yz = Student('yz',24,'学习')
# print(yz.__dict__)
# yyc = Student('yyc',25,'学习' )
# print(yyc.__dict__)

# 对象产看类中的属性:
# yyc = Student('yyc',25,'钱' )
# print(yyc.daily)


# 对象调用类中的方法

yz = Student('yz',24,'学习')
# print(yz.__dict__)
yz.work('红色')
# print(yz.__dict__)

# self 类中方法的第一个位置参数,如果通过对象执行此方法,解释器就自动将此对象空间当成实参传给self
# 约定俗成:类中的方法一般第一个参数设置为self

class Student:
    '''
    此类是构建学生类
    '''
    daily = '学习'
    examination = '考试'

    def work(self):
        print('每天上课')

    def homeworl(self):
        print('家庭作业')

# 类名的角度去调用类中的属性
# 1。查看类中所有的内容。
# print(Student.__dict__)
# print(Student.__dict__['daily'])

# 万能的.点。
# print(Student.daily)  #查
# Student.cloth = '校服'# 增
# print(Student.__dict__)
# Student.examination = '不考试'# 改
# print(Student.examination)
#
# del Student.daily
# print(Student.__dict__)

#一般类中的属性都是通过类名.的方式去操控的。

# 类名的角度调用类中的方法。(一般类中的方法(除去类方法,静态方法)不通过类名调用)

Student.work(self=111)

class Student:
    '''
    此类是构建学生类
    '''
    daily = '学习'
    examination = '考试'

    def __init__(self,n,a,h):
        self.name = n
        self.age = a
        self.hobby = h

        # self.n = 'yz'
        # self.sex = 'n'

        # print(self)
        # print(666)

    def work(self,c):
        self.color = c
        print(f'{self.name}每天上课')

    def homeworl(self):
        print('家庭作业')

# obj = Student()  #类名()过程就叫做实例化过程,实例化一个对象
#

#
# obj = Student()
# print(obj)  #对象,实例
#  实例化一个对象时发生了三件事:
# 1、在内存中创建一个对象空间
# 2、自动执行__init__方法,并且将对象空间传给self参数。
# 3、执行__init__方法里面的代码,给对象空间封装其属性。

# 从对象的角度研究类:
# 对象操作对象里边的属性。
# obj = Student()
# del obj.n #删
# obj.age = '24' #增加了
# obj.sex = 'shuai'
# print(obj.sex)
# print(obj.__dict__)

# yz = Student('yz',24,'学习')
# print(yz.__dict__)
# yyc = Student('yyc',25,'学习' )
# print(yyc.__dict__)

# 对象产看类中的属性:
# yyc = Student('yyc',25,'学习' )
# print(yyc.daily)


# 对象调用类中的方法

yz = Student('yz',24,'学习')
# print(yz.__dict__)
yz.work('红色')
# print(yz.__dict__)

# self 类中方法的第一个位置参数,如果通过对象执行此方法,解释器就自动将此对象空间当成实参传给self
# 约定俗成:类中的方法一般第一个参数设置为self

--------------------------------------------------------记录笔记,如有侵权联系删除----------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值