一、变量
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)
1 class Person:
2 def __init__(self,age,name):
3 self.name = name #实例变量
4 self.age = age #实例变量
5 p1 = Person(18,"iboy")
6 print(p1.name) #通过对象名访问实例变量
7
8 p1.hobby = "打游戏" #这是在当前类中添加一个实例变量
9 print(p1.hobby)
2、类变量
直接写在类中的变量,给类赋值
形式:
变量名 = 值
访问:
类名/对象名.xxx 类名和对象名都能访问,但是只能通过类名来修改变量值。通过对象名修改,相当于在当前对象中增加了一个实例变量
一般把对象中的共性抽出来作为类变量
class Person:
country = "中国" #类变量
def __init__(self,age,name):
self.name = name #实例变量
self.age = age #实例变量p1 = Person("18","iboy") #对象p1
print(p1.country) #中国
p2 = Person("17","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 class Person:
2 country = "中国" #类变量
3 def __init__(self,age,name):
4 self.name = name #实例变量
5 self.age = age #实例变量
6
7 p1 = Person("18","iboy") #对象p1
8 print(p1.country) #中国
9 p2 = Person("17","jacklove") #对象p2
10 print(p2.country) #中国
11 print("--------")
12 Person.country = "中华" #通过类名 修改了类变量country
13 print(p1.country) #中华
14 print(p2.country) #中华
15 print("--------")
16 p1.country = "大清" #通过对象名 是在p1中创建了实例变量country, 并没有修改类变量country
17 print(p1.country) #大清
18 print(p2.country) #中华
二、方法
1、实例方法
直接写在类中的方法,只能由对象调用
形式:
def 方法名(self,参数):
pass
访问:
对象名.方法名(参数)
1 class Car:
2 def run(self):
3 print("车会跑")
4 def cul(self,a,b):
5 print(a+b)
6 def jump(self):
7 print("you jump,i push")
8 #Car.run() #TypeError: run() missing 1 required positional argument: 'self'
9 c = Car() #创建对象c
10 c.run()
11 c.cul(521,1314)
12 c.jump()
13
14 结果:
15 车会跑
16 1835
17 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
静态方法
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,"19252862163")
#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 是不行的
四、私有
在变量名或方法名前面加__作为前缀就表示这是私有的
私有的东西只能类自己内部访问
1 class Person:
2 def __init__(self, name): # 构造, 创建对象的时候自动调用
3 self.__name = name # 私有的
4
5 def __chi(self): # 私有的
6 print("我要吃. 疯狂的吃")
7
8 def he(self):
9 self.__chi() # 内部调用
10 print("我是喝", self.__name)
11
12 # Person.__chi #类访问私有方法 报错 AttributeError: type object 'Person' has no attribute '__chi'
13 p = Person("哈哈哈")
14 # p.__chi() #对象访问私有方法 报错 AttributeError: 'Person' object has no attribute '__chi'
15 #print(p.__name) #对象访问私有实例变量 报错 AttributeError: 'Person' object has no attribute '__name'
16 p.he() #内部访问 可以
17
18 结果:
19 我要吃. 疯狂的吃
20 我是喝 哈哈哈