5.5号python课堂笔记

面向过程

class	Stu:
	name=""
	sex = ""
	score = 0
	def dayin(self):
			print(self.name,self.sex,self.score)
zs=Stu()
zs.name="张三"
zs.sex="男"
zs.score=89
zs.dayin()

ls=Stu()
ls.name="李四"
ls.sex="女"
ls.score="99"
ls.dayin()

定义一个简单的类:

class Dog():
	def sleep(self):
			print("小狗睡觉")
	def chifan(self):
			print("小狗吃饭")
zangao=Dog()
zangao.sleep()
zangao.chifan()

xuehao=Dog()
xuehao.sleep()
xuehao.chifan()

利用参数设置初始值

class People():
	def __init__(self,name):
			priont(id(self))
			self.name=name
	def dayin(self):
			print(id(self))
			print("我叫",self.name)
zs=People("张三")
print(id(zs))
zs.dayin()
ls=People("李四")
print(id(ls))
ls.dayin()

在类的外部增加属性:

class Person():
	def dayin(self):
			print("我叫",self.name)
zs=Persom()
zs.name="张三"
zs.dayin()

ls=persom()
ls.name="李四"
ls.dayin()

定义一个老师的类,初始化的时候,给姓名、年龄、工龄赋值
定义方法:自我介绍
定义一个方法:讲课(),写个笑话出来

class teacher():
	def __init__(self,name,age,gl):
				self.name=name
				self.age=age
				self.gl=gl
	def  js(self):
				print("我叫",self.name)
				print("年龄",self.age)
				print("工龄",self.gl)
	def jk(self):
				print("笑话")
zs=teacher("王五",48,10)
zs.js()
zs.jk()

外部赋值,没有赋值的就添加。

class A():
		def  __init__(self):
					pass
		def  hsl(self,x):
				self.name=x
		def show(self):
				print(self.name)
	x=A()
	x.hsl("张三")
    	x.show()

class X():
		def hehe(self):
				print(self.name)
z=X()
z.name="猪"
z.hehe()

计算圆的面积和周长

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 Stu():
		count=1999	#类属性,放在方法外面
		def __init__(self,name):
				self.name=name#self属性都是对象属性
		def showSelf(self):#self属性都是对象属性
				print(self.name)
print(Stu.count)
a=Stu("张三")
print(a.name)

当类属性和对象属性重名的时候,(可以把类属性认为是全局变量 ,对象属性为局部变量)优先使用对象属性。

    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("张三")
    print(a.count)
    b=Stu("李四")
    print(Stu.count)
    print(a.count)

**删除函数报错:**

    class X():
    		def  __init__(self,name,age):
    					self.name=name
    					self.age=age
    zs=X('张三',18)
    print(zs.name)
    del zs.name		#报错
    print(zs.age)
    print(zs.name)
**A、设计一个学生类,类里面有对象属性,name,id
B、设计一个操作类,类里面有个列表空列表、有个 添加学生方法,输入学号、姓名、年龄,性别添加到列表里,其中id不能重复、有个删除学生方法,通过学号删除在列表中的学生**
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("请输入ID")
		for stu in self.stu_lb:
			if stu.id==id:
					print("重复ID")
					break
		else:
				name=input("请输入姓名")
				stu=Student(name,id)
				self.stu_lb.append(stu)
		self.dayin()
	def delStudent(self):
		id=input("请输入要删除的ID")
		for sty in self.stu_lb:
			if stu.id==id:
					print("删除",stu.name)
					self.stu_lb.remove(stu)
					break
		else:
				print("无此人")
		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 Student():
		count=9
		def __init__(self,name,age):
				self.name=name
				self.age=age
		def  say(self):
				print(self.name,self.age)
		@classmethod  #类方法,跟类有关系
		def cM(cls):
				print(id(cls))
				print(id(Student))
				print(cls.count,'------')
		@staticmethod
		def hehe(a,b)
				print(a+b)
				print("我跟你们没关系,只不过写在其他地方不合适")
zs=Student("张三",18)
Student.cM()

教师工资和纳税钱

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("张三",10000)
ls=Teacher("李四",2000)
Teacher.dayin()
Teacher.jisuan(zs.salary,ls.salary)		

返回format形式

class Stu():
	def __init__(self,name,age):
			self.name=name
			self.age=age
	def  __str__(self):
			return"我叫{},年龄{}".format(self.name,self.age)
zs=Stu("张三",19)
b=str(zs)
print(b)
上一篇:Python MongoDB 查询


下一篇:C/C++ typedef的理解 typedef void (*pfunc)(int);