面向对象——课堂内容

一、定义类(class)
1、最简单的类

#标识      类名    object:   python3可写可不写,python2必须写
class Class_name(object):
         print('hello')
    if __name__ == "__main__":
         LL =Class_name()

" " "
#__init__是一个特殊方法用于在创建对象时进行初始化操作

" " "

class class_name(object):
	#重写init函数,并不是新的定义,但必须写(当用某些公共变量时在此定义)
    def __init__(self):
    	#初始化,打标记,以便于重复利用
        self.a = 1000 
        #打上self标记表示调用的是自己是“class_name”类的不然不能引用,self是印记的作用,不代表一个参数
    def A(self):  
        print(self.a)
        print("sdf dgh htd")

if __name__ == "__main__":
    Joker = class_name  #相当于走了初始化__init__
    Joker.A()  #在类中调用使用.
    print(Joker.a)

" " "
2.进度条
" " "

  **#常规进度**
   


  

 

      mport time
            class ll(object):
                def __init__(self):
                    pass
                def jindutiao(self):
                    for i in range(1,101):
                        time.sleep(0.5)
                        print('#'*(i),'%d %%'%i)
            if __name__  == "__main__":
                LL = ll()
                LL.jindutiao()

" " "
" " "
#20个#,下载进度还是100%

import time
class ll(object):
    def __init__(self):
        self.count = 1
    def jindutiao(self):
        for i in range(0,100):
            time.sleep(0.1)
            if i % 5 ==0:
                res = '#'*self.count
                print('%s %d%%'%(res,(i+5)))
                self.count += 1
if __name__  == "__main__":
    LL = ll()
    LL.jindutiao()
" " "

" " "
#FLUSH刷新,传参,且在一行上出#
" " "

 import time
    class ll(object):
        def jindutiao(self,count):
            for i in range(0,100):
                time.sle d: str
                print('%d%%\r'%(i+1),end="",flush=True)
                if i % 5 == 0:
                   res = '#'*count
                 print('%s %d%%\r'%(res,(i+5)),end)
                 count += 1
                 print()
    if __name__  == "__main__":
        LL = ll()
        LL.jindutiao(count=1)

" " "
3.#设置私有变量

class LL(object):
    def __init__(self,path):
        self.path = pathself.count =0
    def read(self):
        with open(self.path,modelines()
        for line in lines:
            new_line = line.strip('\n')
            for i in new_line:
                if i.isupper():
                    self.count += 1
                     print('The Upper word is %d'%self.count)
 if __name__ == "__main__":
     path = 'users/LL/Desktop/test.txt'
    ll = LL(path)
    ll.read()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值