Python self-study-class13-basic usage of classes

  1. Data encapsulation can be achieved by using classes. Data independence reuse
    class is the class identifier, followed by the class name
class namemoney:
    def __init__(self):  #__init__初始化  self自身
        print("create")
        self.name = "tmy"
        self.money = 100000
    def savemoney(self,num):
        print(self.name,"savemoney",num)
        self.money += num
    def packout(self,num):
        print(self.name,"packout",num)
        self.money -= num

tmy = namemoney()  #创建一个对象,namemoney(),调用init函数
print(type(tmy))   #类型就是对象namemoney
tmy.savemoney(100)   #调用内部函数方法
tmy.packout(1000)
print(tmy.money)   #调用类的属性
print(tmy.name)
  1. Class implementation reuse
#类更深入的基础用法
class namemoney:
    def __init__(self,name,money):  #__init__初始化  self自身
        print("create")
        self.name = name
        self.money = money
    def savemoney(self,num):
        print(self.name,"savemoney",num)
        self.money += num
    def packout(self,num):
        print(self.name,"packout",num)
        self.money -= num
tmy = namemoney("tmy",100000)  #即需要传递参数
tmy.savemoney(1000)
print(tmy.money)

txm = namemoney("txm",100000)   #实现重用
txm.packout(1000)
print(txm.money)

#1.self is essentially the address of the object after instantiation
#2.self can call the properties and methods of
the class #3. In the class, the properties are independent, the method is shared, and the method is called to pass self. The difference is who called self

#类的一般形式
class lazy:
    name = "txm"   #属性-变量,self在内部可以调用属性也可以调用行为
    def sleep(self):  #行为-函数
        print(self.name,"sleep")  #引用属性需要加self
        self.go()   #self只能在类的内部,不能在外部
    def go(self):   #self用于区分谁调用的
        print("lazy,d")

txm = lazy()
print(txm.name)
txm.sleep()

tmm = lazy()
print(id(txm.name),id(tmm.name))   #属性
print(id(txm.go),id(tmm.go))  #行为,函数地址一样
  1. Simple application of class-constructor
class namemoney:
    #a = 10
    #b = 100
    def __init__(self,x,y):   #__init__构造的时候调用
        print("构造了",id(self))
        self.a = x
        self.b = y
    def add(self):
        return self.a + self.b
    def __del__(self):    #__del__删除的时候调用
        print("删除了",id(self))


add1 = namemoney(10,100)  #构造函数,传递参数初始化
print(add1.add())
  1. Simple application of classes-UI design, basic tkinker usage (Tk(), title, geometry, mainloop)
import tkinter
class mywin:
    def __init__(self,text,height,width,x,y):
        self.mytk = tkinter.Tk()  # Tk是一个类,Tk()是构造函数
        self.mytk.title("hello")  # 调用类的行为
        self.height = height
        self.width = width
        self.x = x
        self.y = y
        lastr = "%dx%d+%d+%d"%(self.height,self.width,self.x,self.y)
        self.mytk.geometry(lastr)  # 设置窗口位置和大小,"x"是字母x
    def show(self):
        self.mytk.mainloop()  # 运行起来

win1 = mywin("hello1",300,400,0,0)
win1.show()
win2 = mywin("hello2",500,800,0,0)
win2.show()
win3 = mywin("hello3",600,700,0,0)
win3.show()
'''
mytk = tkinter.Tk()   #Tk是一个类,Tk()是构造函数
mytk.title("hello")   #调用类的行为
mytk.geometry("600x600+0+0")  #设置窗口位置和大小,"x"是字母x
mytk.mainloop()   #运行起来
'''
  1. For a class, its properties can also be dynamically added externally, but it should be noted that self cannot be used at this time
class tmm:
    pass
txm = tmm()
print(type(txm))
txm.name = "txm2"    #动态增加属性
print(txm.name)
txm.getup = lambda name:print(name,"lsp",txm.name)  #匿名函数,动态增加的方法
txm.getup("txm")
  1. Overloading of operators in a class, overloading is to reinterpret a certain behavior.
    At the same time, it is necessary to pay attention to the difference between deep copy and shallow copy: object assignment is the shared address of shallow copy, and deep copy will reopen the new address and transfer the parameter definition object again.
    #Function's parameters, the copy mechanism is shallow copy
    #Cannot change the address of the original data
    #String, number, as a parameter, the original data will not change
    #Object, list can change the address variable content of the original data
class complex:
    def __init__(self,x,y):
        self.x = x
        self.y = y
    def show(self):
        print(self.x,"+",self.y,"i")
    def __add__(self, other):   #重载的含义针对本类型,对于+号重新解释一种行为
        self.x += other.x
        self.y += other.y
c1 = complex(1,2)
c2 = complex(3,5)
c1.show()
c2.show()
c1+c2   #等价c1.__add__(c2)
c1.__add__(c2)
c1.show()
c2.show()
#对象的赋值是浅拷贝,共用地址
c2 = complex(c1.x,c1.y)   #深拷贝,重新传参定义对象
  1. Simple encapsulation of classes, definition of private variables
#私有变量
'''
class money:
    def __init__(self):
        self.money = 100
    def show(self):
        print(self.money)


tmymoney = money()
tmymoney.money = -1000  #python解释语言,变量存在,解释引用,不存在,解释动态绑定或者不可见
print(tmymoney.money)
tmymoney.show()
'''
class money:
    def __init__(self):
        self.__money = 100   #__表示是私有变量,不允许外面随便访问
    def show(self):
        print(self.__money)


tmymoney = money()
tmymoney.__money = -1000  #python解释语言,变量存在,解释引用,不存在,解释动态绑定
print(tmymoney.__money)
tmymoney.show()
#访问私有变量方法:_money__money,加一个_
  1. Data merging method-using class encapsulation
class filegetlines:
    def __init__(self,filepath):
        self.filepath = filepath
        self.file = open(filepath,"rb")
        self.Lines = -1
    def readlines(self):
        i = 0
        while True:
            linestr = self.file.readline()
            if linestr:
                print(linestr)
                i+=1
            else:
                break
        self.lines = i
        return i

f1 = filegetlines(r"D:\Python代码\class13\9.数据归并.txt")
print(f1.readlines())

Summary: understand the idea of ​​python object-oriented programming, understand the definition and application of simple classes

Guess you like

Origin blog.csdn.net/weixin_46837674/article/details/109962584