面向对象编程--python基础

面向对象编程:

  • 封装
  • 继承
  • 多态

构造函数:

#调用未关联的超类构造函数
class SongBird(Bird): 
   def __init__(self): 
        Bird.__init__(self) 
        self.sound = 'Squawk!' 
   def sing(self): 
     print(self.sound)
#使用supper._init_()
class SongBird(Bird): 
    def __init__(self): 
        super().__init__() 
        self.sound = 'Squawk!' 
    def sing(self): 
        print(self.sound)
 
隐藏方法(相当于类的私有方法):
def __ inaccessible(self):
    print("隐藏属性")

(要让方法或属性成为私有的(不能从外部访问),只需让其名称以两个下划线打头即可)

发布了39 篇原创文章 · 获赞 1 · 访问量 1125

猜你喜欢

转载自blog.csdn.net/qq_39421693/article/details/104903164
今日推荐