类的属性及方法

时间:2020-01-15 00:09:41   收藏:0   阅读:87
# _*_ coding:utf-8 _*_
# @Time :2020/1/14 22:24
# @Author :dery
# @File :object_attribute.py
# @Software :PyCharm


class Student: # 定义类
# 类属性
name = ‘dery‘
age = ‘2‘


# 使用类创建对象:对象 = 类名()
dery = Student()
print(dery.name)
dery.age = 32 # 动态创建对象属性 赋值操作 对象属性
print(dery.age) # 先找自己空间的属性,再去类空间找对应属性

yupeng = Student()
yupeng.name = ‘xiaoyupeng‘
print(yupeng.name)
yupeng.age = 1
print(yupeng.age)

Student.name = ‘zhangsan‘

ruirui = Student()
print(ruirui.name)

# _*_ coding:utf-8 _*_
# @Time :2020/1/14 22:51
# @Author :dery
# @File :object_method.py
# @Software :PyCharm

# 类中的方法:动作
# 种类:普通方法、类方法、静态方法、魔术方法


class Phone:
brand = ‘xiaomi‘
price = 4999
type = ‘mate 802‘
note = ‘‘

def call(self): # 类里面的方法:call
print(self)
print(‘打电话‘)
print(‘留言:‘, self.note)


phone1 = Phone()
phone1.note = ‘我是phone1的note‘
print(phone1, ‘----------1-------‘)
# print(phone1.type)
phone1.call()
print(‘*‘ * 30)
phone2 = Phone()
print(phone2, ‘--------2-----‘)
phone2.call()
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!