面向对象(4)–承袭和多态

day6面向对象–继承、多态,day6面向对象–多态

继承

   
继承:
就像遗传一样,继承就是拥有父类的所有方法和属性,并且能够定义自己独特的属性和方法,对上面的类进行扩展。

    可以什么都不写,直接继承父类,如下:

   

class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Man(People):
    pass

class Woman(People):
    pass

m1 = Man("chenronghua",16)
m1.eat()
运行结果如下:
chenronghua is eating......

   
上面代码可以看出,首先定义了一个People的类,还有一个Man(People)类直接继承People类。

    下面,我们在上面的Man()类中定义一个单独的方法,如下:

class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Man(People):
    def piao(self):
        print("%s is piaoing......20s......" %self.name)

class Woman(People):
    pass

m1 = Man("chenronghua",16)
m1.piao()
运行结果如下:
chenronghua is piaoing......20s......

    上面,我们给Man()增加了新的方法,可以看出,能够执行。

   
下面,我们来重写父类。扩展新功能,子类并且要具有父类的所有方法与属性。在子类中调用父类:

class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Man(People):
    def piao(self):
        print("%s is piaoing......20s......" %self.name)

    def sleep(self):
        People.sleep(self)
        print("man is sleeping......")

class Woman(People):
    pass

m1 = Man("chenronghua",16)
m1.sleep()

   
上面代码中,子类调用了父类中的方法,要明白本质,创建实例的本质是增加了一个新的内存变量。

    不同类中方法的调用:

class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Man(People):
    def piao(self):
        print("%s is piaoing......20s......" %self.name)

    def sleep(self):
        People.sleep(self)
        print("man is sleeping......")

class Woman(People):

    def get_birth(self):
        print("%s is borning a bady......" %self.name)

# m1 = Man("chenronghua",16)
# m1.sleep()

w1 = Woman("chenronghua",26)
w1.get_birth()
w1.piao()
运行如下:
chenronghua is borning a bady......
Traceback (most recent call last):
  File "/home/zhuzhu/第六天/inherit.py", line 34, in <module>
    w1.piao()
AttributeError: 'Woman' object has no attribute 'piao'

    从上面可以看出,继承同一个父类的子类是方法是不能相互调用的。

   

class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Man(People):
    def __init__(self,name,age,money):     #给男的单独增加属性
        People.__init__(self,name,age)
        self.money = money
        print("%s 刚出生就有%s元" %(self.name,self.money))    #构造函数的时候就会执行代码


    def piao(self):
        print("%s is piaoing......20s......" %self.name)

    def sleep(self):
        People.sleep(self)
        print("man is sleeping......")

class Woman(People):

    def get_birth(self):
        print("%s is borning a bady......" %self.name)

m1 = Man("chenronghua",16,100)
m1.sleep()
运行结果如下:
chenronghua 刚出生就有100元
chenronghua is sleeping.....
man is sleeping.....

   
从上面代码可以看出,__init__(self,name,age,money)类的初始化,People.__init__(self,name,age)继承,继承父类的属性,普通的继承。

    下面来看看新式类中的继承。

class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Man(People):
    def __init__(self,name,age,money):     #给男的单独增加属性
        super(Man,self).__init__(name,age)
        self.money = money
        print("%s 刚出生就有%s元" %(self.name,self.money))    #构造函数的时候就会执行代码


    def piao(self):
        print("%s is piaoing......20s......" %self.name)

    def sleep(self):
        People.sleep(self)
        print("man is sleeping......")

class Woman(People):

    def get_birth(self):
        print("%s is borning a bady......" %self.name)

m1 = Man("chenronghua",16,100)
m1.sleep()

 

   
新式类是用super()函数来实现继承的,super(Man,self).__init__(name,age)实现继承,新式类和旧式类的差别主要体现在多继承上面。

    下面看下类的多继承问题:

class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Relation(object):

    def make_friends(self,obj):
        print("%s is making friends with %s" %(self.name,obj.name))

class Man(People,Relation):
    def __init__(self,name,age,money):     #给男的单独增加属性
        super(Man,self).__init__(name,age)    #超级父类,Man继承父类name,age
        self.money = money
        print("%s 刚出生就有%s元" %(self.name,self.money))    #构造函数的时候就会执行代码


    def piao(self):
        print("%s is piaoing......20s......" %self.name)

    def sleep(self):
        People.sleep(self)
        print("man is sleeping......")

class Woman(People,Relation):

    def get_birth(self):
        print("%s is borning a bady......" %self.name)

w1 = Woman("ruhua",18)

m1 = Man("chenronghua",16,100)
m1.make_friends(w1)           #把w1实例当做参数传给make_friends,等价于obj》w1,obj.name》w1.name

   
上面代码中,当子类继承多个父类的时候,即便其中一个父类没有实例化,也能调用另外一个父类的方法和属性。通过子类把两个父类关联到一起。

    多态

    一个接口,多个重用

继承
继承:
就像遗传一样,继承就是拥有父类的所有方法和属性,并且能够定义自己独特的…

继承

继承

一、继承

一、继承

   
继承:
就像遗传一样,继承就是拥有父类的所有方法和属性,并且能够定义自己独特的属性和方法,对上面的类进行扩展。

   
继承:
就像遗传一样,继承就是拥有父类的所有方法和属性,并且能够定义自己独特的属性和方法,对上面的类进行扩展。

继承的类称为子类,被继承的类称为基类,父类,或超类。

1、类成员的访问

   
可以什么都不写,直接继承父类,如下:

   
可以什么都不写,直接继承父类,如下:

继承的优点:

使用场合

   

   

  • 简化了代码
  • 提高了代码的健壮性
  • 提高了代码的安全性
  • 多态的前提

C#2.0

class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Man(People):
    pass

class Woman(People):
    pass

m1 = Man("chenronghua",16)
m1.eat()
运行结果如下:
chenronghua is eating......
class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Man(People):
    pass

class Woman(People):
    pass

m1 = Man("chenronghua",16)
m1.eat()
运行结果如下:
chenronghua is eating......

缺点:耦合与内聚。耦合性越低,内聚性越高,代码越好。

Visual Basic 2005

   
上面代码可以看出,首先定义了一个People的类,还有一个Man(People)类直接继承People类。

   
上面代码可以看出,首先定义了一个People的类,还有一个Man(People)类直接继承People类。

比如,有一个Animal类,有个run方法:

说明

   
下面,我们在上面的Man()类中定义一个单独的方法,如下:

   
下面,我们在上面的Man()类中定义一个单独的方法,如下:

In [1]: class Animal:
   ...:     def run(self):
   ...:         print('我正在跑步!!!')
   ...:

Type

class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Man(People):
    def piao(self):
        print("%s is piaoing......20s......" %self.name)

class Woman(People):
    pass

m1 = Man("chenronghua",16)
m1.piao()
运行结果如下:
chenronghua is piaoing......20s......
class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Man(People):
    def piao(self):
        print("%s is piaoing......20s......" %self.name)

class Woman(People):
    pass

m1 = Man("chenronghua",16)
m1.piao()
运行结果如下:
chenronghua is piaoing......20s......

当我们需要创建Dog和Cat类时,可以直接继承Animal:

(指类,接口等类型)

   
上面,我们给Man()增加了新的方法,可以看出,能够执行。

   
上面,我们给Man()增加了新的方法,可以看出,能够执行。

In [3]: class Dog(Animal):
   ...:     pass
   ...:

In [4]: class Cat(Animal):
   ...:     pass

public

   
下面,我们来重写父类。扩展新功能,子类并且要具有父类的所有方法与属性。在子类中调用父类:

   
下面,我们来重写父类。扩展新功能,子类并且要具有父类的所有方法与属性。在子类中调用父类:

由于Animal有run方法,通过继承,Dog和Cat也自动拥有了run方法。

public

class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Man(People):
    def piao(self):
        print("%s is piaoing......20s......" %self.name)

    def sleep(self):
        People.sleep(self)
        print("man is sleeping......")

class Woman(People):
    pass

m1 = Man("chenronghua",16)
m1.sleep()
class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Man(People):
    def piao(self):
        print("%s is piaoing......20s......" %self.name)

    def sleep(self):
        People.sleep(self)
        print("man is sleeping......")

class Woman(People):
    pass

m1 = Man("chenronghua",16)
m1.sleep()
In [5]: dog=Dog()
In [6]: dog.run()
我正在跑步!!!

In [7]: xjm=Cat()
In [8]: xjm.run()
我正在跑步!!!

访问不受限制

   
上面代码中,子类调用了父类中的方法,要明白本质,创建实例的本质是增加了一个新的内存变量。

   
上面代码中,子类调用了父类中的方法,要明白本质,创建实例的本质是增加了一个新的内存变量。

在·此时你有没有想过,每个动物的run方法都不一样,有飞的,有游的。有跳的,有爬的。

internal

   
不同类中方法的调用:

   
不同类中方法的调用:

所以我们可以对子类的方法进行重写,而重写后,子类的run方法会覆盖父类的run方法。这样就体现了继承的一个好处:多态。继承正是多态的前提。

Friend

class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Man(People):
    def piao(self):
        print("%s is piaoing......20s......" %self.name)

    def sleep(self):
        People.sleep(self)
        print("man is sleeping......")

class Woman(People):

    def get_birth(self):
        print("%s is borning a bady......" %self.name)

# m1 = Man("chenronghua",16)
# m1.sleep()

w1 = Woman("chenronghua",26)
w1.get_birth()
w1.piao()
运行如下:
chenronghua is borning a bady......
Traceback (most recent call last):
  File "/home/zhuzhu/第六天/inherit.py", line 34, in <module>
    w1.piao()
AttributeError: 'Woman' object has no attribute 'piao'
class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Man(People):
    def piao(self):
        print("%s is piaoing......20s......" %self.name)

    def sleep(self):
        People.sleep(self)
        print("man is sleeping......")

class Woman(People):

    def get_birth(self):
        print("%s is borning a bady......" %self.name)

# m1 = Man("chenronghua",16)
# m1.sleep()

w1 = Woman("chenronghua",26)
w1.get_birth()
w1.piao()
运行如下:
chenronghua is borning a bady......
Traceback (most recent call last):
  File "/home/zhuzhu/第六天/inherit.py", line 34, in <module>
    w1.piao()
AttributeError: 'Woman' object has no attribute 'piao'

 

访问范围仅限于同一程序集

   
从上面可以看出,继承同一个父类的子类是方法是不能相互调用的。

   
从上面可以看出,继承同一个父类的子类是方法是不能相互调用的。

In [9]: class Cat(Animal):
   ...:     def run(self):
   ...:         print('xjm正在爬呀爬呀!!!')
   ...:

In [10]: xjm=Cat()

In [11]: xjm.run()
xjm正在爬呀爬呀!!!

Member

   

   

二、多继承

(指类型中的成员,比如类中的字段)

class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Man(People):
    def __init__(self,name,age,money):     #给男的单独增加属性
        People.__init__(self,name,age)
        self.money = money
        print("%s 刚出生就有%s元" %(self.name,self.money))    #构造函数的时候就会执行代码


    def piao(self):
        print("%s is piaoing......20s......" %self.name)

    def sleep(self):
        People.sleep(self)
        print("man is sleeping......")

class Woman(People):

    def get_birth(self):
        print("%s is borning a bady......" %self.name)

m1 = Man("chenronghua",16,100)
m1.sleep()
运行结果如下:
chenronghua 刚出生就有100元
chenronghua is sleeping.....
man is sleeping.....
class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Man(People):
    def __init__(self,name,age,money):     #给男的单独增加属性
        People.__init__(self,name,age)
        self.money = money
        print("%s 刚出生就有%s元" %(self.name,self.money))    #构造函数的时候就会执行代码


    def piao(self):
        print("%s is piaoing......20s......" %self.name)

    def sleep(self):
        People.sleep(self)
        print("man is sleeping......")

class Woman(People):

    def get_birth(self):
        print("%s is borning a bady......" %self.name)

m1 = Man("chenronghua",16,100)
m1.sleep()
运行结果如下:
chenronghua 刚出生就有100元
chenronghua is sleeping.....
man is sleeping.....

每个孩子都有自己爸爸妈妈,类是否一样呢?多继承:

public

   
从上面代码可以看出,__init__(self,name,age,money)类的初始化,People.__init__(self,name,age)继承,继承父类的属性,普通的继承。

   
从上面代码可以看出,__init__(self,name,age,money)类的初始化,People.__init__(self,name,age)继承,继承父类的属性,普通的继承。

首先创建一个Father类:

public

   
下面来看看新式类中的继承。

   
下面来看看新式类中的继承。

In [18]: class Father():
    ...:     def __init__(self,money):
    ...:         self.money=money
    ...:     def play(self):
    ...:         print('我很皮')
    ...:     def func(self):
    ...:         print('父亲的方法')

访问不受限制

class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Man(People):
    def __init__(self,name,age,money):     #给男的单独增加属性
        super(Man,self).__init__(name,age)
        self.money = money
        print("%s 刚出生就有%s元" %(self.name,self.money))    #构造函数的时候就会执行代码


    def piao(self):
        print("%s is piaoing......20s......" %self.name)

    def sleep(self):
        People.sleep(self)
        print("man is sleeping......")

class Woman(People):

    def get_birth(self):
        print("%s is borning a bady......" %self.name)

m1 = Man("chenronghua",16,100)
m1.sleep()
class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Man(People):
    def __init__(self,name,age,money):     #给男的单独增加属性
        super(Man,self).__init__(name,age)
        self.money = money
        print("%s 刚出生就有%s元" %(self.name,self.money))    #构造函数的时候就会执行代码


    def piao(self):
        print("%s is piaoing......20s......" %self.name)

    def sleep(self):
        People.sleep(self)
        print("man is sleeping......")

class Woman(People):

    def get_birth(self):
        print("%s is borning a bady......" %self.name)

m1 = Man("chenronghua",16,100)
m1.sleep()

 

internal

 

 

再创建一个Mother类:

Friend

   
新式类是用super()函数来实现继承的,super(Man,self).__init__(name,age)实现继承,新式类和旧式类的差别主要体现在多继承上面。

   
新式类是用super()函数来实现继承的,super(Man,self).__init__(name,age)实现继承,新式类和旧式类的差别主要体现在多继承上面。

In [19]: class Mother():
    ...:     def __init__(self,faceValue):
    ...:         self.faceValue=faceValue
    ...:     def eat(self):
    ...:         print('我是吃货')
    ...:     def func(self):
    ...:         print('母亲的方法')

访问范围仅限于同一程序集

   
下面看下类的多继承问题:

   
下面看下类的多继承问题:

 

protected

class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Relation(object):

    def make_friends(self,obj):
        print("%s is making friends with %s" %(self.name,obj.name))

class Man(People,Relation):
    def __init__(self,name,age,money):     #给男的单独增加属性
        super(Man,self).__init__(name,age)    #超级父类,Man继承父类name,age
        self.money = money
        print("%s 刚出生就有%s元" %(self.name,self.money))    #构造函数的时候就会执行代码


    def piao(self):
        print("%s is piaoing......20s......" %self.name)

    def sleep(self):
        People.sleep(self)
        print("man is sleeping......")

class Woman(People,Relation):

    def get_birth(self):
        print("%s is borning a bady......" %self.name)

w1 = Woman("ruhua",18)

m1 = Man("chenronghua",16,100)
m1.make_friends(w1)           #把w1实例当做参数传给make_friends,等价于obj》w1,obj.name》w1.name
class People(object):

    def __init__(self,name,age):
        self.name = name
        self.age = age

    def eat(self):
        print("%s is eating......" %self.name)

    def sleep(self):
        print("%s is sleeping....." %self.name)

    def talk(self):
        print("%s is talking......" %self.name)

class Relation(object):

    def make_friends(self,obj):
        print("%s is making friends with %s" %(self.name,obj.name))

class Man(People,Relation):
    def __init__(self,name,age,money):     #给男的单独增加属性
        super(Man,self).__init__(name,age)    #超级父类,Man继承父类name,age
        self.money = money
        print("%s 刚出生就有%s元" %(self.name,self.money))    #构造函数的时候就会执行代码


    def piao(self):
        print("%s is piaoing......20s......" %self.name)

    def sleep(self):
        People.sleep(self)
        print("man is sleeping......")

class Woman(People,Relation):

    def get_birth(self):
        print("%s is borning a bady......" %self.name)

w1 = Woman("ruhua",18)

m1 = Man("chenronghua",16,100)
m1.make_friends(w1)           #把w1实例当做参数传给make_friends,等价于obj》w1,obj.name》w1.name

 先来满足这样一个需求,孩子继承Father的钱money。继承Mother的颜值faceValue

protected

   
上面代码中,当子类继承多个父类的时候,即便其中一个父类没有实例化,也能调用另外一个父类的方法和属性。通过子类把两个父类关联到一起。

   
上面代码中,当子类继承多个父类的时候,即便其中一个父类没有实例化,也能调用另外一个父类的方法和属性。通过子类把两个父类关联到一起。

再来创建子类来继承Father和Mother:

访问范围限于自己或派生出来的子类型

   
多态

   
多态

In [21]: class Child(Father,Mother):
    ...:     def __init__(self,money,faceValue):
    ...:         Father.__init__(self,money)
    ...:         Mother.__init__(self,faceValue)

Protected internal

   
一个接口,多个重用

   
一个接口,多个重用

 

Protected Friend

最后来实现一下:

在同一程序集内访问不受限制,在不同程序集中仅由此类型派生出来的子类型可访问

In [23]: c=Child(30000,99)

In [24]: c.money
Out[24]: 30000

In [25]: c.faceValue
Out[25]: 99

In [26]: c.play()
我很皮

In [27]: c.eat()
我是吃货

In [28]: c.func()
父亲的方法

private

 

private

说明:父类中方法名相同,默认调用的是括号中排前面的父类中的方法。

仅自己可以访问

三、多态

1)public、private和protected的区别:

概念:一种事物的多种形态

图片 1

要理解什么是多态,我们首先要对数据类型再作一点说明。当我们定义一个class的时候,我们实际上就定义了一种数据类型。我们定义的数据类型和Python自带的数据类型,比如str、list、dict没什么两样:

在子类中引用:

In [12]: a=list()

In [13]: b=Animal()

In [14]: c=Cat()

In [15]: isinstance(a,list)
Out[15]: True

In [16]: isinstance(c,Cat)
Out[16]: True

In [17]: isinstance(c,Animal)
Out[17]: True

图片 2

 

2)static: 对变量的静态设置(累加求和)

 从上面代码可以看出,c对象既属于Cat类,又属于Animal类。

在父类中的方法:
图片 3

那么多态有什么好处呢?通过一下一个事例来一一说明:

在子类中的方法:

class Person():
    def __init__(self,name,gender):
        self.name=name
        self.gender=gender
    def who(self):
        print('我是一个人,我的名字是%s'%self.name)

class Student(Person):
    def __init__(self,name,gender,score):
        super(Student,self).__init__(name,gender)
        self.score=score
    def who(self):
        print('我是一名学生,我的名字是%s'%self.name)
class Teacher(Person):
    def __init__(self,name,gender,course):
        super(Teacher,self).__init__(name,gender)
        self.course=course
    def who(self):
        print('我是一名老师,我的名字是%s'%self.name)
def who(x):
    x.who()
a=Person('小狗','女')
b=Student('小猫','男',99)
c=Teacher('小鸡','不男不女','python')
who(a)
who(b)
who(c)

图片 4

我是一个人,我的名字是小狗
我是一名学生,我的名字是小猫
我是一名老师,我的名字是小鸡

3)internal

 

图片 5

 super函数

//在某个类中没有任何可访问性关键字在他前面,它就是internal的。

In [8]: class A():
   ...:     def __init__(self):
   ...:         print('A')

In [13]: class B(A):
    ...:     def __init__(self):
    ...:         print('B')
    ...:         super().__init__()

In [14]: class C(A):
    ...:     def __init__(self):
    ...:         print('C')
    ...:         super().__init__()

In [15]: class D(B,C):
    ...:     def __init__(self):
    ...:         print('D')
    ...:         super().__init__()

In [16]: d=D()
D
B
C
A

internal class A     

 

     {

        internal int InternalI = 0;

    }

它完全等同于:

 class A   //在某个类中没有任何可访问性关键字在他前面,它就是internal的。

    {

        internal int InternalI = 0;

    }

图片 6

它完全等同于:

图片 7

2、多态:当出现继承关系,就会伴随出现多态;简单的理解就是,子类不仅继承了父类的所有属性和方法,每一个子类又会有属于自己的个性的属性和方法。

图片 8

3、方法重载、隐藏与虚方法调用

由于子类对象汇集了父类和子类的所有公共方法,有些方法父类和子类有重复的方法,所以调用时会有许多问题出现。

子类方法与父类方法之间的关系有三种:

1)扩充:子类方法,父类没有;

2)重载:子类有父类的同名函数,但参数类型或数目不一样;

3)完全相同:子类方法与父类方法从方法名称到参数类型完全一样。

图片 9

在主函数中调用:

图片 10

【练习1】方法重载:

图片 11

【练习2】方法重写:在子类同名方法前加override,表明对父类同名方法进行了重写。父类

子类默认的是new;

父类默认的是override:

1)加override:(只有当子类转化成父类时起作用)执行子类的方法(子类方法覆盖了父类方法)

2)不加override:变量名后.(点)哪个类就执行该类的方法。

图片 12

图片 13

图片 14

 

 

 

相关文章