Seventeen, Python object-oriented inheritance of

In object-oriented inheritance is a very important feature
Child and parent class, the subclass is the parent class of an extension, for some extensions on the properties and methods of the parent class
Example: did not bring inheritance
 
# Define a door numbered class and status
class Door(object):
def __init__(self,num,status):
self.num num
self.status = status
def open(self):
self.status = 'open'
def close(self):
self.status = 'close'
 
# Define a class of the door can be locked
class Lockable(object):
def __init__(self,num,status,locked):
self.num num
self.status = status
self.locked = locked
def open(self):
if not self.locked: # If you do not lock, then I'll open it
self.status = 'open'
else:
print "the door is locked"
def close(self):
self.status = 'close'
======================================================================
======================================================================
======================================================================
Lockable the above example, we found that this class is the basis of the Door class adds a locked instance variable
And there is no way to lock, the rest is the same.
#inherit
class Door(object):
def __init__(self,num,status):
self.num num
self.status = status
def open(self):
self.status = 'open'
def close(self):
self.status = 'close'
 
# Inherited from the parent class Door
class Lockable(Door):
def __init__(self,num,status,locked):
super (Lockable, self) .__ init __ (num, status) # here super explain your constructors num inherited your father's class and status two instance variables, the wording of which is fixed (subclass name, self)
self.locked = locked # here because you inherit the parent class, so we will not need to self.num here and self.status assign values ​​to them, locked is new, it needs to assign
# Of the open method overloading
def open(self):
if not self.locked: # If you do not lock, then I'll open it
#调用父类的方法
super(Lockable,self).open()
else:
print "the door is locked"
 
 
class Unlockable(Door): #这里我定义了一个没有锁的门,并且我什么都不做,它继承了父类Door,即使他什么都不做,Door的方法它也都继承下来了
pass
 
 
u = Unlockable(1,'open')
print (u.status)
 
l = Lockable(1,'open',True)
l.open()
print (l.status)
 
l = Lockable(1,'open',False)
l.open()
print (l.status)
 
 
 
===========================================================================
===========================================================================
===========================================================================
静态方法:
 
@static_method #静态方法,类的工具包
def info() #不用加self,无需实例化,不能访问实例其他信息
 
 
@class_method #类方法,
def info(self): #能访问类变量,不能访问实例变量,不需要实例化,即可调用
 
 
@property #将函数变成静态属性,需实例化方可调用,如果不实例化则仅仅是打印内存地址,调用的时候不需要加()
def sayhi(self): #
print "-----say hi",self.name
return "test"
 
 
m = MyClass()
print (m.sayhi) #会打印出-----say hi
 
如果是这样:
print (MyClass.sayhi) #则仅仅只会打印出<property object at 0x0200F8A0>
 
如果是这样:
print (MyClass.sayhi()) #则会报错
 
 
 
=========================================================================
=========================================================================
=========================================================================
什么情况需要实例化才能调用,什么情况不需要实例化也能调用:
总结:
你只要看到有self的就代表需要实例化才能调用(仅仅在没有那三个特殊方法的情况下)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Guess you like

Origin www.cnblogs.com/steven9898/p/11329433.html