python面向对象编程

python面向对象编程

python面向对象编程_第1张图片

python面向对象编程_第2张图片 

1111111111111111111111
 class Cylinder:
     def __init__(self,r=0,h=0,pi=0):
         self.r=r
         self.h=h
         self.pi=math.pi
     def setfi(self,r,h):
         self.r=r
         self.h=h
     def show(self):
         print(self.r*self.pi*self.h*2+self.r*self.r*self.pi*2)

 r=float(input("r:"))
 h=float(input("h:"))
 c=Cylinder(r,h)
 c.show()
222222222222222222
 class Person:
     def __init__(self,name,age,sex):
         self.name=name
         self.age=age
         self.sex=sex
     def setname(self,name):
         self.name=name
     def setage(self,age):
         self.age=age
     def setsex(self,sex):
         self.sex=sex
     def show(self):
         print("name:",self.name,"\nage:",self.age,"\nsex",self.sex)

 p1=Person("张三","18","男")
 p1.show()
 3333333333333333333333
 class Person:
     def __init__(self,name,age,sex):
         self.name=name
         self.age=age
         self.sex=sex
     def setname(self,name):
         self.name=name
     def setage(self,age):
         self.age=age
     def setsex(self,sex):
         self.sex=sex
     def show(self):
         print("name:",self.name,"\nage:",self.age,"\nsex",self.sex)

 class teacher(Person):
     def __init__(self,name,age,sex,course,salar):
         super(teacher, self).__init__(name,age,sex)
         self.course=course
         self.salar=salar
     def setSalar(self,salar):
         self.salar=salar
     def setCourse(self,course):
         self.course=course
     def show(self):
         print("name:",self.name,"\nage:",self.age,"\nsex",self.sex,"\ncourst:",self.course,"\nsalar:",self.salar)
 tt=teacher("李华","20","男","物理","10000")
 tt.show()
 444444444444444444444444
 class static:
     def __init__(self):
         self.s=[]
     def push(self,item):
         self.item=item
         self.s.append(item)
     def pop(self):
         self.s.pop()
     def is_empty(self):
         return self.s==[]
     def show(self):
         for i in self.s:
             print(i,end='')
 st=static()
 st.push(1)
 print(st.s)
 st.push(2)
 print(st.s)
 print(st.is_empty())
 st.pop()
 print(st.s)
 st.pop()
 print(st.is_empty())
 print(st.s)
 st.show()
 555555555555555555555555
 class Vector:
     def __init__(self,a,b,c):          #初始化方法 进行传参
         self.x=a
         self.y=b
         self.z=c
     def plus(self,m,n):                #加法
         self.x=m.x+n.x
         self.y=m.y+n.y
         self.z=m.z+n.z
         print((self.x,self.y,self.z))
     def sub(self,m,n):                 #减法
         self.x=m.x-n.x
         self.y=m.y-n.y
         self.z=m.z-n.z
         print((self.x,self.y,self.z))
     def mul(self,m,i):                 #乘法
         self.x=m.x*i
         self.y=m.y*i
         self.z=m.z*i
         print((self.x,self.y,self.z))
     def div(self,m,i):                 #除法
         self.x = m.x//i
         self.y = m.y//i
         self.z = m.z//i
         print((self.x,self.y,self.z))
 vector1=Vector(1,2,3)
 vector2=Vector(3,2,1)
 vector3=Vector(3,5,6)
 vector3.plus(vector1,vector2)
 vector3.sub(vector2,vector1)
 vector3.mul(vector1,12)
 vector3.div(vector2,1)

 

你可能感兴趣的:(python,python,开发语言)