面向对象和面向过程
类和对象
类的设计
类的创建
self:相当于当前对象;类似于Java中的this
类的创建和使用:
class Dog:
name = 'dog'
age = 11
def eat(self):
print('eat rice!')
dog = Dog()
print(dog.name,dog.age,sep='\n')
dog.eat()
class Dog1(object):
name = 'dog'
age = 11
def __init__(self, name, age):
self.name = name
self.age = age
def mood(self):
print('good')
dog1 = Dog1('dog1', 8)
print(dog1.name,dog1.age,sep='\n')
dog1.mood()
基本语法:self
基本语法:slots
案例:
class Choose(object):
__slots__ = ('name','age','position')
def __init__(self,name,age,postion):
self.name=name
self.age=age
self.position=postion
def fun(self):
print('just fun,don\'t think!')
def printName(self):
print('name={}'.format(self.name))
c=Choose('c',90,'position')
c.fun()
c.printName()
class Choose(object):
def __init__(self,name,age,postion):
self.name=name
self.age=age
self.position=postion
c=Choose('c',90,'position')
c.city='add'
print(c.city)
魔术方法
魔术方法:__init__方法 & __del__方法
魔术方法:__str__方法
魔术方法: __call__方法
魔术方法: __eq__方法
魔术方法案例
class Magic(object):
__slots__ = ('name', 'age')
def __init__(self, name, age):
self.name = name
self.age = age
print('init class!:' + self.name)
def __del__(self):
print('delete class!:' + self.name)
def __str__(self):
return 'this is str:' + self.name
def __call__(self, *args, **kwargs):
print('call class:' + self.name)
def __eq__(self, other):
return self.name == other.name and self.age == other.age
m1 = Magic('m1', 1)
m2 = Magic('m2', 2)
print(m1)
print(m2)
m1()
m2()
print(m1 is m2)
print(m1 == m2)
其他魔术方法总结
练习1-摆放家具
代码
class House(object):
__slots__ = ('style', 'allSzie', 'restSize', 'furnituresList')
def __init__(self, style, allSzie, restSize, furnitureList):
self.style = style
self.allSzie = allSzie
self.restSize = restSize
self.furnituresList = furnitureList
def __str__(self):
return 'style=' + self.style + '\t' + 'allSzie=' + str(self.allSzie) + '\t' + 'restSize=' + str(self.restSize)+'\t'+' '.join(self.furnituresList) + '\n'
def add_furniture(self, item):
if(item.area<=self.restSize):
self.restSize-=item.area
self.furnituresList.append(item.name)
print('可以添加'+item.name+'家具!\n')
else:
print('不可以添加'+item.name+'家具!\n')
class HouseItem(object):
__slots__ = ('name', 'area')
def __init__(self, name, area):
self.name = name
self.area = area
def __str__(self):
return 'name='+self.name+'\t'+'area='+str(self.area)+'\n'
bed=HouseItem('席梦思',4)
chest=HouseItem('衣柜',2)
table=HouseItem('餐桌',1.5)
print(bed)
print(chest)
print(table)
h=House('别墅',10,10,[])
h.add_furniture(bed)
h.add_furniture(chest)
h.add_furniture(table)
print(h)
练习2-计算点是否在矩形内
class Point(object):
__slots__ = ('x','y')
def __init__(self,x,y):
self.x=x
self.y=y
def __str__(self):
return 'x='+self.x+'\t'+'y='+self.y+'\n'
class Rectangle(object):
__slots__ = ('x1', 'y1','x2','y2')
def __init__(self, x1, y1,x2,y2):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
def __str__(self):
return 'area='+ str(self.area())
def area(self):
return (self.x2-self.x1)*(self.y2-self.y1)
def inside(self,point):
if((self.x1<point.x<self.x2) and(self.y1<point.y<self.y2)):
return True
else:
return False
point=Point(1,4)
r=Rectangle(0,0,2,7)
print(r)
print(r.inside(point))
练习3-计算器类
设计一个计算器类,可以进行加、减、乘、除计算。
class Count(object):
__slots__ = ('x', 'pos', 'y')
def __init__(self, x, pos, y):
self.x = x
self.pos = pos
self.y = y
def decide(self):
if (self.pos == '+'):
return self.add()
elif (self.pos == '-'):
return self.sub()
elif (self.pos == '*'):
return self.mul()
elif (self.pos == '/'):
return self.truediv()
def add(self):
return self.x + self.y
def sub(self):
return self.x - self.y
def mul(self):
return self.x * self.y
def truediv(self):
return self.x / self.y
def __str__(self):
return str(self.x) + self.pos + str(self.y) + "=" + str(self.decide())
x=int(input('请输入x:'))
pos=input('请输入pos:')
y=int(input('请输入y:'))
c = Count(x, pos, y)
print(c)
|