zoukankan      html  css  js  c++  java
  • Python面向对象的三大特点:封装,继承和多态(示例)

    继承

    单继承:

    #类定义
    class people:
        #定义基本属性
        name = ''
        age = 0
        #定义私有属性,私有属性在类外部无法直接进行访问
        __weight = 0
        #定义构造方法
        def __init__(self,n,a,w):
            self.name = n
            self.age = a
            self.__weight = w
        def speak(self):
            print("%s 说: 我 %d 岁。" %(self.name,self.age))
     
    #单继承示例
    class student(people):
        grade = ''
        def __init__(self,n,a,w,g):
            #调用父类的构函
            people.__init__(self,n,a,w)
            self.grade = g
        #覆写父类的方法
        def speak(self):
            print("%s 说: 我 %d 岁了,我在读 %d 年级"%(self.name,self.age,self.grade))
     
     
     
    s = student('ken',10,60,3)
    s.speak()
    

     实现结果

    ken 说: 我 10 岁了,我在读 3 年级
    

     多继承:(虽然是可以的,但是不建议这么做,只需要了解继承时的顺序是由左至右的即可)

    #类定义
    class people:
        #定义基本属性
        name = ''
        age = 0
        #定义私有属性,私有属性在类外部无法直接进行访问
        __weight = 0
        #定义构造方法
        def __init__(self,n,a,w):
            self.name = n
            self.age = a
            self.__weight = w
        def speak(self):
            print("%s 说: 我 %d 岁。" %(self.name,self.age))
     
    #单继承示例
    class student(people):
        grade = ''
        def __init__(self,n,a,w,g):
            #调用父类的构函
            people.__init__(self,n,a,w)
            self.grade = g
        #覆写父类的方法
        def speak(self):
            print("%s 说: 我 %d 岁了,我在读 %d 年级"%(self.name,self.age,self.grade))
     
    #另一个类,多重继承之前的准备
    class speaker():
        topic = ''
        name = ''
        def __init__(self,n,t):
            self.name = n
            self.topic = t
        def speak(self):
            print("我叫 %s,我是一个演说家,我演讲的主题是 %s"%(self.name,self.topic))
     
    #多重继承
    class sample(speaker,student):
        a =''
        def __init__(self,n,a,w,g,t):
            student.__init__(self,n,a,w,g)
            speaker.__init__(self,n,t)
     
    test = sample("Tim",25,80,4,"Python")
    test.speak()   #方法名同,默认调用的是在括号中排前地父类的方法
    

     封装:

    class Student(object):
        def __init__(self, name, score):
            self.name = name
             self.score = score
    
    May = Student("May",90)                      # 须要提供两个属性
    Peter = Student("Peter",85)
    print(May.name, May.score)
    print(Peter.name, Peter.score)
    
    def print_score(Student):                    # 外部函数
        print_score(Student)
    # print("%s's score is: %d" %(Student.name,Student.score))             
    # 普通 print 写法
    print("{0}'s score is: {1}".format(Student.name,Student.score))        
    print_score(May)    
    print_score(Peter)
    

     多态:

    class Animal(object):
        def __init__(self, name):  # Constructor of the class
            self.name = name
        def talk(self):
            raise NotImplementedError("Subclass must implement abstract method")
    class Cat(Animal):
        def talk(self):
            print('%s: 喵喵喵!' %self.name)
    class Dog(Animal):
        def talk(self):
            print('%s: 汪!汪!汪!' %self.name)
    def func(obj): #一个接口,多种形态
        obj.talk()
    c1 = Cat('毛毛')
    d1 = Dog('灰灰')
    func(c1)
    func(d1)
    
  • 相关阅读:
    编程之美 2.3寻找发帖‘水王’ 扩展问题
    编程之美:1.12 扩展问题 解答与思考
    编程之美:1.9高效率安排见面会 图的m着色问题 回溯法
    研究生毕业课题怎么确定(转)
    图模型的统计推断 inference in graphical models(马尔科夫链的推断)
    微信js-sdk注意事项
    bootstrap-material-design-个人总结
    前端页面优化
    Material Design
    马克飞象
  • 原文地址:https://www.cnblogs.com/xinin0909/p/9575405.html
Copyright © 2011-2022 走看看