ass Cat(object):
# def __init__(self, name = '动物',color = "白色"):
# self.name = name
# self.color = color
# def test(self):
# print(self.__name)
# print(self.color)
# # def run(self):
# # print("%s————在跑步"%self.name)
# class bosi(Cat):
# def setNewName(self,newName):
# self.name = newName
# def eat(self):
# print("%s__在吃"%self.name)
#
# b = bosi("印度猫")
# print('b的名字是:%s'%b.name)
# print("b的颜色是%s"%b.color)
# b.eat()
# b.setNewName('波斯')
# b.run()
class Animal(object):
def __init__(self, name = '动物',color = "白色"):
self.name = name
self.color = color
def test(self):
print(self.__name)
print(self.color)
# def run(self):
# print("%s————在跑步"%self.name)
class Dog(Animal):
def dogTest1(self):
print(self.__name)
def dogTest2(self):
self._test()
# A = Animal()
# # print(A.__name)
# A.test()
print("分割线")
D = Dog(name = "小花狗",color = "黄色")
D.dogTest1()
D.dogTest2()
说明:
虽然子类没有定义__init__方法,但是父类有,所以在子类继承父类的时候这个方法就被继承了,所以只要创建Bosi的对象,就默认执行了那个继承过来的__init__方法
总结
1、子类在继承的时候,在定义类时,小括号()中为父类的名字
2、父类的属性、方法,会被继承给子类
1、私有的属性,不能通过对象直接访问,但是可以通过方法访问
2、私有的方法,不能通过对象直接访问
3、私有的属性、方法,不会被子类继承,也不能被访问
4、一般情况下,私有的属性、方法都是不对外公布的,往往用来做内部的事情,起到安全的作用