编译器:Visual Studio Code 环境:Python 3.9.6 记录学习过程,如有冒犯,不胜荣幸。
class Test:
num = 10
def fun(self):
print('Example')
@staticmethod
def means():
return "Static "
@classmethod
def way(cls):
print("正在使用")
def __init__(self,name,age) :
self.name = name
self.age = age
def fun1(self):
print(self.name+"正在调用")
num_1 = Test("Name",-1)
'''
num_1.fun() #调用方法。对象名.方法()
Test.fun(num_1) #用类名调用方法,传入类的对象
'''
'''
print (num_1.means())
print(Test.means()) #通过类名直接调用静态方法
print(Test.num) #调用类属性。 类名.类属性
Test.num = 100 #修改属性值
print(Test.num)
'''
'''
print(id(Test)) #分配内存空间
print(type(Test)) #<class 'type'>
print(Test) #<class '__main__.Test'> 创建了类对象Test
'''
stu_1 = Test("张三",20)
stu_2 = Test("李四",18)
'''
print(stu_1.name,stu_1.age) # 张三 20
print(stu_2.name,stu_2.age) # 李四 18
stu_1.fun1() # 张三正在调用
stu_2.fun1() # 李四正在调用
'''
class Test_1(Test):
def __init__(self, name, age,grade):
super().__init__(name, age)
self.grade = grade
def fun1(self):
super().fun1()
print("方法重写")
a = Test_1("王五",18,99)
'''
a.fun1() #调用重写方法fun1()
print(a.__dict__) #特殊属性__dict__,获得对象的属性以字典形式返回
#{'name': '王五', 'age': 18, 'grade': 99}
'''
import math
print(math.pi)
from math import pi
print(pi)
if __name__ == "__main__":
pass
import Package_1.page_1
file_1 = open("Data.txt","r")
print(file_1.readlines())
file_1.close()
file_2 = open("Data_1.txt","w")
file_2.write("Hello World")
file_2.close()
file_3 = open("Data_1.txt","a")
file_3.write("\nPyhton")
file_3.close()
with open("Data.txt","r") as File:
print(File.read())
|