类的属性及方法

Posted python-beginner

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了类的属性及方法相关的知识,希望对你有一定的参考价值。

# _*_ 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()

以上是关于类的属性及方法的主要内容,如果未能解决你的问题,请参考以下文章

类的实例方法静态方法类方法及静态属性

Java中子类继承了父类的私有属性及方法吗?

类的属性及方法

Python中类的继承及类的属性和方法总结

View类的xml属性,相关方法及说明

Javascript String类的属性及方法