#python类中的公有方法和私有方法
#公有:function()
#私有方法: __function() #两个下划线
class Car:
def start(self):#公有方法
self.__startEngine()
print('start car')
def __startEngine(self):#私有方法
print("start car by engine!")
myCar=Car()
myCar.start()
#myCar._startEngine() #报错,因为是私有函数
#myCar._Car_startEngine() 不推荐的做法,对私有方法进行重命名,会破坏封装性
#继承
#python类 继承
class A:
def __init__(self,name,salary):
self.name=name
self.salary=salary
def work(self):
print('A work')
def addsalary(self,number):
self.salary=self.salary+number
def printinfo(self):
print("name is "+self.name+' salary is '+str(self.salary))
class A1(A):#继承父类A,继承父类中的公有方法,不含私有属性和私有方法
def work(self):
print(" learn python")
def learn(self):
print(self.name+'learn python')
#实例化对象A11
A11=A1('jiang_ji_ming',12000)
A11.printinfo()
A11.learn()
A11.addsalary(5000)
A11.printinfo()
A11.work()#根据继承覆盖原则,这里是调用子类里面的