Python(7)(面向对象程序设计)类对象多态继承重载

- 类和对象:
class person:
#静态变量或者类变量:
#调用方法:类名.变量名/对象名.方法名
var1 = "string"#静态变量或者类变量
#私有变量,只能在类的内部调用
__str = "str"
#静态方法,不能访问self实例
@staticmethod
def staticmd():
print(‘I am static method!’)
2.类的继承
class C:
def __init__(self,id,name,age):
self.id = id
self.name = name
self.age = age
def __del__(self):#析构函数
print('bye')
def show(self):
print(self.id,self.name,self.age)
class S(C):#学生类继承公民类,同时有自己的属性比如学号
def __init__(self,id,name,age,stdno):
super(S,self).__init__(id,name,age)#调用父类!!注意写法
self.stdno = stdno
def show(self):#类中的所有方法都要传入self
super(S,self).show()
print(self.stdno)
class T(C):
def __init__(self,id,name,age,tehno):
super(T,self).__init__(id,name,age)
self.tehno = tehno
def show(self):
super(T,self).show()
print(self.tehno)
if __name__=='__main__':
c=C('01','张',65)#创建公民类对象,进行类的实例化。对象名=类名()
c.show()
del c
s = S('001','李','18','2101003')
s.show()
del s
t = T('010','王','54','2101'
t.show()
3.多态与重载
定义抽象类:
from abc import ABCMeta,abstractmethod
class myAbc(object):
__metaclass__=ABMeta
@abstractmethod
def abstractmethod(self):pass
4.重载

浙公网安备 33010602011771号