• 小白学Python面向对象01


      python里面处处皆是对象!那么到底什么是对象?官方答案:把一组数据结构和处理它们的方法组成对象(object),把相同行为的对象归纳为(class),通过类的封装(encapsulation)隐藏背部细节,通过继承(inheritance)实现类的特化(specialization)和泛华(generalization),通过多态(polymorphism)实现基于对象类型的动态分派。虽然听不太懂,但是我知道学了python我也会有对象了。

    类和对象

      类是对象的模板和集合,而对象是类的实例,前者是抽象概念,后者是个具体东西。对象都有静态特征(属性)和动态特征(行为),将一群具有共同特征的对象圈起来,就可以定义为“类”。总共分三步:

    1、定义类,使用class关键字,类名的每个单词首字母大写

    数据抽象—>抽取对象共同的静态特征(找名词)—>属性

    行为抽象—>抽取对象共同的动态特征(找名词)—>方法

    2、调用构造方法创建学生对象

    3、通过给对象发消息,让对象完成某些工作

    class Student(object):#定义类,object是类的爸爸
    
        def __init__(self, name, age):
            self.name = name
            self.age = age
        def study(self,course):#定义行为方法
            print('%s正在学习%s' % (self.name, course))
    
    
    def main():
        stu1 = Student('小明',18)
        stu1.study('python程序设计')
    
    
    if __name__ == '__main__':
        main()
    

     Python中对象的访问可见性

      一般的编程语言,都会把对象的属性设置为私有的或受保护的,但是对象的方法是公开的。在python里面实际上从语法上严格来讲没有保证对象属性和方法能够受私密保护,我们通常采用“隐喻”的方法:若希望属性是私有的,在属性名下用2个下划线作为开头。但其实如果你知道更换名字的规则,你也可以访问到该对象。以下2段代码可以对访问性做实例说明。

    class Test:
    def __init__(self, foo):   self.__foo = foo
    def __bar(self):#__bar将bar名字私有保护   print(self.__foo)   print('__bar') def main(): test = Test('hello') # AttributeError: 'Test' object has no attribute '__bar'出错 test.__bar() # AttributeError: 'Test' object has no attribute '__foo' print(test.__foo) if __name__ == "__main__": main()
    class Test:
        
        def __init__(self, foo):
            self.__foo = foo
            
        def __bar(self):
            print(self.__foo)#打印出hello
            print('__bar')#打印出__bar
            
    def main():
        test = Test('hello')
        test._Test__bar()
        print(test._Test__foo)#打印出hello
        
    if __name__ == "__main__":
        main()

    习题:定义一个类描述数字化的时钟如:00:00:00

    import time
    
    
    class Clock(object):
    
        def __init__(self, hour=0, minute=0, second=0):
            self._hour = hour
            self._minute = minute
            self._second = second
    
        def go(self):
            self._second += 1
            if self._second == 60:
                self._second = 0
                self._minute += 1
                if self._minute == 60:
                    self._hour += 1
                    self._minute = 0
                    if self._hour == 24:
                        self._hour = 0
    
        # 可以获得对象的字符串表示形式 当我们用print打印对象时会自动调用该方法
        def __str__(self):
            return '%02d:%02d:%02d' % (self._hour, self._minute, self._second)
    
       
    def main():
        #Clock参数有默认值,可以自己设置
        nowtime = Clock(12,25,56)
        while True:
            print(nowtime)
            nowtime.go()
            time.sleep(1)
    
    if __name__ == '__main__':
        main()

     习题:仿照上述习题,定一个倒计时器

    class Count_Down(object):
    
        def __init__(self,hour=1,minute=0,second=5):
            self._hour = hour
            self._minute = minute
            self._second = second
    
        def cut_time(self):
            if self._hour == 0 and self._minute==0 and self._second == 0:
                return False
            elif self._second == 0:
                if self._minute == 0:
                    self._hour -= 1
                    self._minute = 59
                    self._second =60
                else:
                    self._minute -= 1
                    self._second = 60
            self._second -= 1
            return True
    
        def show(self):
            return '%02d:%02d:%02d' % (self._hour, self._minute, self._second)
    
    def main():
        import time
        x = Count_Down()
        while x.cut_time():
            print(x.show())
            time.sleep(1)
        print('时间到了')
    if __name__ == '__main__':
        main()
  • 相关阅读:
    影视-纪录片:《梵净山》
    NuGet:ServiceStack
    资源-产品:ServiceStack
    Win7x64安装Oracle11201x64 解决PLSQL Developer无法找到oci问题
    Oracle11g环境设置-windows环境
    安装sql server提示挂起报错
    安装oracle11g未找到文件WFMLRSVCApp.ear文件
    配置linux中文
    卸载rpm包提示:error: specifies multiple packages
    FileZilla简单介绍及运用
  • 原文地址:https://www.cnblogs.com/bbszc520/p/8569900.html
Copyright © 2020-2023  润新知