当前位置: 代码迷 >> 综合 >> Byte-of-python笔记代码3:Object.py
  详细解决方案

Byte-of-python笔记代码3:Object.py

热度:2   发布时间:2023-10-20 03:34:27.0

#-*-coding:utf-8-*-###创建Person类
# class Person:
#     pass
#
#
# p=Person();#创建Person类的一个对象
# print(p)# ###方法
# class Person:
#     def SayHello(self):
#         print("Hello My Lord !")
# d=Person();
# d.SayHello()
#
# Person().SayHello()# ###:__init__,类在实例化时,会被首先执行__init__中的东西,亦可称为:初始化方法
#
# class Person:
#     def __init__(self,name):
#         self.name=name;
#     def SayHello(self):
#         print("__init__方法初始化的name:{0}".format(self.name))
# Person("xiaoxie").SayHello()# ###  类变量,和对象变量
# class Robot:
#     population=0
#     def __init__(self,name):
#         self.name=name
#         print("正在创造机器人,名字为:{0}".format(name))
#         Robot.population+=1;
#
#     def die(self):
#         print("{0}挂了!".format(self.name))
#         Robot.population-=1;
#
#         if Robot.population==0:
#             print("{0}是最后的一个机器人".format(self.name))
#         else:
#             print("仍然还有{0}个机器人".format(Robot.population))
#
#     def sayHello(self):
#         print("MyLord,我是机器人:{0},请指教".format(self.name))
#
#     def sayCount(self):
#         print("报告大人,现在共有机器人:{0}台".format(Robot.population))
#
# b1=Robot("number1")
# b1.sayHello()
# b1.sayCount()
#
# b2=Robot("number2")
# b2.sayCount()
# b2.sayHello()
#
# b1.die()
# b2.die()# ###输入、输出
# def reverse(text):
#     return text[::-1]
#
# def is_palindrome(text):
#     return text==reverse(text)
#
# someing=input("请输入文本:")
#
# if(is_palindrome(someing)):
#     print("文本为回文!")
# else:
#     print("文本不是回文!")# ###文件操作
# import os
# text="这是python创建的文件\nhahahha"
# f=open('tt.txt','w')
# f.write(text)
# f.close()
#
# print(os.path.abspath('tt.txt'))
#
# f=open('tt.txt')
# while True:
#     line=f.readline()
#     if len(line)==0:
#         break
#     else:
#         print(line,end='')
# f.close()###Unicode
# import io
#
# txt=io.open("tt.txt","wt",encoding='utf-8')
# txt.write("哈哈哈,滴滴滴,大大大,嘿嘿嘿,qwert67")
# txt.close()
#
# read=io.open("tt.txt",encoding="utf-8").read()
# print(read)###异常try:......except Exception:......
# try:
#     s=input("input Something ")
# except EOFError:
#     print("格式有错")
# else:
#     print("没有错误操作")# ##自定义异常类
# class shortInputException(Exception):
#     def __init__(self,length,atleast):
#         Exception.__init__(self)
#         self.length=length
#         self.atleast=atleast
#
# try:
#     s=input("input something:")
#     if(len(s)<3):
#         raise shortInputException(len(s),3)
# except shortInputException as ex:
#     print("输入长度为{0},最少输入长度要求为{1}".format(ex.length,ex.atleast))
# else:
#     print("输入长度为:{0}".format(len(s)))####   try......finally# import sys,time
#
# f=None
# try:
#     f=open("tt.txt",encoding="utf-8")
#     while True:
#         line=f.readline()
#         if len(line)==0:
#             break
#         print(line)
#         sys.stdout.flush()#为了让打印的字符立马显示。
#         time.sleep(2)
# except IOError:
#     print("找不到文件!")
# except KeyboardInterrupt:
#     print("中断执行(通常是输入^C)")
# finally:
#     if f:
#         f.close()
#     print("close f")# with open("tt.txt",encoding="utf-8") as f:
#     for line in f:
#         print(line)



这里给出下一篇的链接:

  相关解决方案