Process oriented
class Stu: name="" sex = "" score = 0 def dayin(self): print(self.name,self.sex,self.score) zs=Stu() zs.name="Zhang San" zs.sex="male" zs.score=89 zs.dayin() ls=Stu() ls.name="Li Si" ls.sex="female" ls.score="99" ls.dayin()
Define a simple class:
class Dog(): def sleep(self): print("Puppy sleeping") def chifan(self): print("Puppy eating") zangao=Dog() zangao.sleep() zangao.chifan() xuehao=Dog() xuehao.sleep() xuehao.chifan()
Setting initial value with parameters
class People(): def __init__(self,name): priont(id(self)) self.name=name def dayin(self): print(id(self)) print("My name is",self.name) zs=People("Zhang San") print(id(zs)) zs.dayin() ls=People("Li Si") print(id(ls)) ls.dayin()
Add properties outside the class:
class Person(): def dayin(self): print("My name is",self.name) zs=Persom() zs.name="Zhang San" zs.dayin() ls=persom() ls.name="Li Si" ls.dayin()
Define a teacher's class. When initializing, assign a value to the name, age and length of service
Definition method: self introduction
Define a method: lecture (), write a joke
class teacher(): def __init__(self,name,age,gl): self.name=name self.age=age self.gl=gl def js(self): print("My name is",self.name) print("Age",self.age) print("Working years",self.gl) def jk(self): print("joke") zs=teacher("Wang Wu",48,10) zs.js() zs.jk()
External assignment, adding without assignment.
class A(): def __init__(self): pass def hsl(self,x): self.name=x def show(self): print(self.name) x=A() x.hsl("Zhang San") x.show() class X(): def hehe(self): print(self.name) z=X() z.name="pig" z.hehe()
Calculate the area and perimeter of a circle
class Yuan(): def __init__(self,r): self.r=r def zhouchang(self): return 2*3.1415*self.r def mianji(self): area=3.14*self.r*self.r return area yi=Yuan(2) print(y1.mianji()) print(y1.zhouchang()) y2=Yuan(3) print(y2.mianji()) print(y2.zhouchang())
Class properties and object properties
class Stu(): count=1999 #Class attribute, outside method def __init__(self,name): self.name=name#self attributes are all object attributes def showSelf(self):#self attributes are all object attributes print(self.name) print(Stu.count) a=Stu("Zhang San") print(a.name)
When a class attribute and an object attribute have the same name, (you can think of the class attribute as a global variable and the object attribute as a local variable), the object attribute is used first.
class Stu(): conut=0 def __init__(self,name): self.name=name Stu.count+=1 def showSelf(self): print(self.name) print(Stu.count) a=Stu("Zhang San") print(a.count) b=Stu("Li Si") print(Stu.count) print(a.count) **Delete function error:** class X(): def __init__(self,name,age): self.name=name self.age=age zs=X('Zhang San',18) print(zs.name) del zs.name #Report errors print(zs.age) print(zs.name) **A,Design a student class with object attributes, name,id B,Design an operation class, in which there is an empty list and a method to add students. Input student number, name, age and gender to the list id There is a method to delete students. Students in the list can't be deleted by student number** class Student(): def __init__(self,name,id) self.name=name self.id=id class Oper(): def __init__(self): self.stu_lb=[] def addStudent(self): id=input("Please input ID") for stu in self.stu_lb: if stu.id==id: print("repeat ID") break else: name=input("Please enter a name") stu=Student(name,id) self.stu_lb.append(stu) self.dayin() def delStudent(self): id=input("Please enter the ID") for sty in self.stu_lb: if stu.id==id: print("delete",stu.name) self.stu_lb.remove(stu) break else: print("No one else") self.dayin() def dayin(self): for stu in self.stu_lb: print(stu.name,stu.id) o=Oper() o.addStudent() o.addStudent() o.delStudent()
Class properties and instance properties
class Student(): count=9 def __init__(self,name,age): self.name=name self.age=age def say(self): print(self.name,self.age) @classmethod #Class method, related to class def cM(cls): print(id(cls)) print(id(Student)) print(cls.count,'------') @staticmethod def hehe(a,b) print(a+b) print("I have nothing to do with you, but it's not appropriate to write it elsewhere") zs=Student("Zhang San",18) Student.cM()
Teachers' salaries and taxes
class Teacher(): sl=0 tax=0.002 def __init__(self,name,salary): self.name=name self.salary=salary Teacher.sl+=1 @classmethod def cM(cls): print(cls.sl) @staticmethod def gz(a,b): c=(a+b)*Teacher.tax print(c) zs=Teacher("Zhang San",10000) ls=Teacher("Li Si",2000) Teacher.dayin() Teacher.jisuan(zs.salary,ls.salary)
Return to format
class Stu(): def __init__(self,name,age): self.name=name self.age=age def __str__(self): return"My name is{},Age{}".format(self.name,self.age) zs=Stu("Zhang San",19) b=str(zs) print(b)