对象属性和类属性
class Person(object):
type = '人类' # 这个属性定义在类里,函数之外我们称之为类属性
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person('张三', 18)
p2 = Person('李四', 19)
x = p1
# p1和p2称呼他们为实例对象
# 只要创建一个实例对象,这个实例对象就有自己的属性
# 对象属性:每个实例对象都单独保存的属性
# 每个实例对象之间的属性没有关联,互不影响
print(p1.name)
print(p2.age)
print(x.name, x.age)
# 类属性可以通过类对象和实例对象获取
# print(Person.type) # 可以通过类对象获取
# print(p1.type) # 可以通过实例对象获取
# print(p2.type)
# 类属性只能通过类对象来修改,实例对象不能修改
# Person.type = 'human'
# print(Person.type)
print(p1.type)# 人类
p1.type = 'human'
print(p1.type)# human
print(Person.type)# 人类
print(p2.type)# 人类
私有属性
(了解)
import datetime
class Person(object):
def __init__(self, name, age):
self.name = name
self.age = age
self.__money = 1000 # 以两个下划线开始的变量是私有变量
def add(self):
self.__money += 1000 #在类里可以直接访问私有变量
def get_money(self):
print('{}查询了余额'.format(datetime.datetime.now()))
return self.__money
def set_money(self,qian):
if type(qian) !=int:
print('输入的钱的格式不对')
return
self.__money = qian
def __demo(self):# 以两个下划线开头的函数是私有函数
print('我是私有函数我被调用了')
def text(self):
self.__demo()
p = Person('张三', 18)
print(p.name, p.age) # 可以直接获取
# print(p.__money) # 不能直接获取到私有变量
# p.__demo()# 不能直接调用
# 获取私有变量的方式:
# 1.使用对象._类名__私有变量名获取和改变
p._Person__money = 500
print(p._Person__money)# 500
p._Person__demo()# 我是私有函数我被调用了
# 2.也可以通过定义方法来获取和改变私有属性
print(p.get_money())
# 2021-01-20 13:10:38.110169查询了余额
# 500
p.set_money(100)
print(p.get_money())
# 2021-01-20 13:10:38.110169查询了余额
# 100
# 定义函数来返回私有函数
p.text()# 我是私有函数我被调用了
类方法和静态方法
(和函数差不多,无非就是无参函数和有参函数,理解起来还是比较简单的)
class Person(object):
type='人类'
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food):
print(self.name + '正在吃' + food)
#如果方法里没有用到实例对象的任何属性,可以将这个方法定义成为一个静态方法
@staticmethod
def demo():
print('hello')
@classmethod
def text(cls):# 如果这个方法只用到了类属性,可以将这个方法定义为类方法
# 类方法有一个参数cls,不需要手动传递参数,会自动传参
# cls 指的是类对象, cls == Person ==True
print(cls.type)
p = Person('张三', 18)
p1 = Person('李四',19)
# 实例对象在调用方法时,不需要给形参self传递参数
# 会自动的把实例对象传给self
p.eat('屎') # 直接使用实例对象调用方法
# 对象方法还可以使用类对象来调用 格式:类名.方法名
# 这种方法不会自动的给self传递参数,需要手动的给self传参
Person.eat(p1,'奥利给')
# 静态方法:没有用到实例对象的任何属性
Person.demo()
p.demo()
# 类方法:
# 可以使用实例对象和类对象调用
p.text()
Person.text()