方法名 | 作用 |
---|---|
init | 构造函数,在生成对象时调用,即初始化该方法的属性 |
del | 析构函数,释放对象的时候使用 |
… | … |
参考
class person(object):
tall=180
hobbies=[]
def __init__(self,name,age,weight):
self.name=name
self.age=age
self.weight=weight
def information(self):
print("%s age is %d ,and weigth is %s"%(self.name,self.age,self.weight))
person.hobbies.extend(["football","woman"]) #类数据属性属于类本身,可以通过类名进行修改和访问,此次添加两个值
print("person hobbies list is:%s"%person.hobbies)
person.hobbies2=["reading","swimming","running"] #类在定义后,依然可以添加类的属性,新增的类属性也可以被类和所以实例共有
print("person hobbies2 list is:%s"%person.hobbies2)
print(dir(person)) #通过内建函数dir()或访问类的字典属性__dict__这两种方式来查看类有那些属性
jhon=person("jhon",24,66) #实例数据属性只能通过实例化访问
jhon.gender="man" #类实例化后可以动态添加属性,但是只能被实例化的jhon使用
print("%s gender is %s"%(jhon.name,jhon.gender))
print(dir(jhon))
jhon.hobbies.append("python") #修改实例化列表属性,在原基础上添加
infoma=jhon.information()
print(infoma)
print(jhon.hobbies)
jhon2=person("jhon2",25,68) #jhon2将没有属性gender
print(jhon2.__dir__())
属性名 | 含义 |
---|---|
name | 类的名字 |
doc | 类的文档字符串 |
bases | 类的所有父类组成的元组 |
dict | 类的属性组成的字典 |
module | 类所属的模块 |
class | 类对象的类型 |
对于可变类型的类属性(列表等),隐藏属性总结为:
虽然可以通过实例化来访问类的属性,但是不建议这样做,最好通过类名访问类的属性,从而避免属性隐藏带来的不必要的麻烦
class person(object):
tall=180
hobbies=[]
def __init__(self,name,age,weight): #__init__类的私有方法,构造方法,实例类的时候调用
self.name=name
self.age=age
self.weight=weight
def infoma(self): #实例方法,只能通过实例进行调用
print('%s is age %s weights %s'%(self.name,self.age,self.weight))
jhon=person("jhon",24,175)
jhon.infoma()
输出:
jhon is age 24 weights 175
class person(object):
tall = 180
hobbies = []
def __init__(self, name, age,weight):
self.name = name
self.age = age
self.weight = weight
@classmethod #类的装饰器
def infoma(cls): #cls表示类本身,使用类参数cls
print(cls.__name__)
print(dir(cls))
#cls表示类本身
person.infoma() #直接调用类的装饰器函数,通过cls可以访问类的相关属性
jhon=person("jhon",24,170) #也可以通过两个步骤来实现,第一步实例化person
jhon.infoma() #调用装饰器
class person(object):
tall = 180
hobbies = []
def __init__(self, name, age,weight):
self.name = name
self.age = age
self.weight = weight
@staticmethod #静态方法装饰器
def infoma(): #没有参数限制,既不要实例参数,也不用类参数
print(person.tall)
print(person.hobbies)
#person.infoma() #静态法可以通过类名访问
Bruce = person("Bruce", 25,180) #通过实例访问
Bruce.infoma()
_tall=180 #导入该包时会导入
def _call_for(): #导入该包时不会导入
print('_tall',——tall)
class person(object):
tall = 180
hobbies = []
def __init__(self, name, age,weight):
self.name = name
self.age = age
self.weight = weight
self.__Id = 430 #实现__id一定程度的私有化
@staticmethod
def infoma():
print(person.tall)
print(person.hobbies)
#person.infoma()
jhon = person("jhon", 25,180)
#print(Bruce.__Id)#出错
#jhon.infoma()
#通过内建函数dir()发现__Id属性在运行时,属性名被改成_person__Id
print(dir(jhon))
print(jhon._person__Id)
class A(object):
def __init__(self):
self.__private()#将执行时会变成self._A__private()
self.public()
def __private(self): #只能被本身类访问,连子类也不行
print('A.__private()')
def public(self):
print('A.public()')
class B(A):
def __private(self): #
print('B.__private()')
def public(self):
print('B.public()')
b=B() #当B实例化后,由于没有定义__init__函数,将会调用父类的__init__,但是由于双下划线的'混淆'效果,"self.__private()"将变成self._A__private()
输出:
A.__private()
B.public()
class A(object):
def __init__(self):
self.private()
self.public()
def private(self):
print('A.private()')
def public(self):
print('A.public()')
class B(A):
def private(self): #
print('B.private()')
def public(self):
print('B.public()')
b=B()
输出:
B.private()
B.public()
在Python中出现继承时需要注意点是:初始化函数__init__的行为:
#首先定义父类parent
class Parent(object):
def __init__(self,name):
self.name=name
print("create an instance of:",self.__class__.name__) #self.__class__.__name__用于显示执行该语句的类名
print("name attribute is:",self.name)
#define subChild class ,继承父类:
class Child(Parent):
pass
#子类实例化是,由于子类没有初始化,此时父类的初始化函数会被默认调用,且必须传入父类的参数那么
c=Child("init Child")
输出:
create an instance of: Child
name attribute is: init Child
class Parent(object):
def __init__(self,name):
self.name=name
print("create an instance of: ",self.__class__.__name__)
print("name attribute is:",self.name)
#子类继承父类
class Child(Parent):
#子类中没有显示调用父类的初始化函数
def __init__(self):
print("call__init__ form Child class")
#实例化子类
c=Child()
print(c.name) #将会出错,原因父类没有被初始化
class Parent(object):
def __init__(self, name):
self.name = name
print("create an instance of:", self.__class__.__name__)
print("name attribute is:", self.name)
class Child(Parent):
def __init__(self):
print("call__init__form Child class")
super(Child,self).__init__("data from Child") #要将子类child和self传递进去
d=Parent('jhon')
c=Child()
print(c.name)
输出结果:
create an instance of: Parent
name attribute is: jhon
call__init__form Child class
create an instance of: Child
name attribute is: data from Child
data from Child
class Parent(object):
Value="from parent value"
def fun(self):
print("function is from Parent")
#定义子类,继承父类
class Child(Parent):
Value ="from child value"
def fun(self):
print("funtion is from child")
Parent.fun(self) #调用父类的fun函数方法,将self显示的传递进入
c=Child()
c.fun()
print(Child.Value)
上面继承父类的缺点:需要经过父类名硬编码到子类中,然而super方法可以解决这一问题:
class Parent(object):
Value="from parent value"
def fun(self):
print("function is from Parent")
#定义子类,继承父类
class Child(Parent):
Value ="from child value"
def fun(self):
print("funtion is from child")
#Parent.fun(self) #调用父类的fun函数方法,将self显示的传递进入
super(Child,self).fun() #和上一句相同用于调用父类方法
c=Child()
c.fun()
print(Child.Value)
输出:
funtion is from child
function is from Parent
from child value
1.参考
2.参考