一、变量
1、实例变量(又叫字段、属性)
创建对象时给对象赋值
形式:
self.xxx = xxx
访问:
对象名.xxx 只能由对象访问
class Person:
def __init__(self,age,name):
self.name = name #实例变量
self.age = age #实例变量
p1 = Person(18,"iboy")
print(p1.name) #通过对象名访问实例变量 p1.hobby = "打游戏" #这是在当前类中添加一个实例变量
print(p1.hobby)
2、类变量
直接写在类中的变量,给类赋值
形式:
变量名 = 值
访问:
类名/对象名.xxx 类名和对象名都能访问,但是只能通过类名来修改变量值。通过对象名修改,相当于在当前对象中增加了一个实例变量
一般把对象中的共性抽出来作为类变量
class Person:
country = "中国" #类变量
def __init__(self,age,name):
self.name = name #实例变量
self.age = age #实例变量 p1 = Person("","iboy") #对象p1
print(p1.country) #中国
p2 = Person("","jacklove") #对象p2
print(p2.country) #中国
print("--------")
Person.country = "中华" #通过类名 修改了类变量country
print(p1.country) #中华
print(p2.country) #中华
print("--------")
p1.country = "大清" #通过对象名 是在p1中创建了实例变量country, 并没有修改类变量country
print(p1.country) #大清
print(p2.country) #中华
二、方法
1、实例方法
直接写在类中的方法,只能由对象调用
形式:
def 方法名(self,参数):
pass
访问:
对象名.方法名(参数)
class Car:
def run(self):
print("车会跑")
def cul(self,a,b):
print(a+b)
def jump(self):
print("you jump,i push")
#Car.run() #TypeError: run() missing 1 required positional argument: 'self'
c = Car() #创建对象c
c.run()
c.cul(521,1314)
c.jump() 结果:
车会跑
1835
you jump,i push
2、类方法
在声明时加上@classmethod装饰的方法
形式:
@classmethod
def 方法名(cls):
pass
访问:
类名/对象名.方法名()
class Person:
def chi(self): #实例方法
print("人要吃饭")
@classmethod
def he(cls): # 这是类方法,可以通过类和对象名访问
print(cls)
print("人要喝水") Person.he()
p = Person()
p.he() 结果:
<class '__main__.Person'>
人要喝水
<class '__main__.Person'>
人要喝水
3、静态方法
声明时加@staticmethod 装饰的方法,相当于在类中定义的一个普通函数
形式:
@staticmethod
def 方法名():
pass
访问:
类名/对象名.方法名()
class Person: def chi(self): # 实例方法
print("人在吃") # 类方法
@classmethod # 类方法
def he(cls): # cls 类
print(cls)
print("我是喝") @staticmethod
def sleep(): # 在类中定义的一个普通函数,不带参
print("和你睡不等于睡你 -- 姜文") @staticmethod
def fly(height): # 在类中定义的一个普通函数,带参
print("上天%s" % height)
Person.sleep()
Person.fly(500)
p = Person
p.sleep()
p.fly(500) 结果:
和你睡不等于睡你 -- 姜文
上天500
和你睡不等于睡你 -- 姜文
上天500
静态方法
三、属性方法
通过@property 把一个方法变成一个实例变量来使用,我自称为属性方法,就是本来是一个方法,但是有属性的效果。
形式:
@property
def 方法名(self):
return 值
访问:
对象名.方法名
class Person:
def __init__(self,name,birthday,qq):
self.name = name
self.birthday = birthday
self.qq = qq @property
def age(self):
return 2018-self.birthday
p1 = Person("王三",1995,"")
#print(p1.age()) # TypeError: 'int' object is not callable age是不可调用的
print(p1.age) # 23 可以像属性一样用
print(Person.age) # 通过类名访问访问不到 <property object at 0x0000000001E18EF8>
!!!注意:
函数只能有一个self 参数
函数必须有返回值
不能给该属性赋值 像p1.age = 10 是不行的
四、私有
在变量名或方法名前面加__作为前缀就表示这是私有的
私有的东西只能类自己内部访问
class Person:
def __init__(self, name): # 构造, 创建对象的时候自动调用
self.__name = name # 私有的 def __chi(self): # 私有的
print("我要吃. 疯狂的吃") def he(self):
self.__chi() # 内部调用
print("我是喝", self.__name) # Person.__chi #类访问私有方法 报错 AttributeError: type object 'Person' has no attribute '__chi'
p = Person("哈哈哈")
# p.__chi() #对象访问私有方法 报错 AttributeError: 'Person' object has no attribute '__chi'
#print(p.__name) #对象访问私有实例变量 报错 AttributeError: 'Person' object has no attribute '__name'
p.he() #内部访问 可以 结果:
我要吃. 疯狂的吃
我是喝 哈哈哈
需要注意的是, 对于私有的内容,子类是无法继承的。