一、类与对象
类:是一群具有相同特征行为的事物的统称,类是抽象的不能直接使用
对象:由类创造出来的具体存在
类:属性(方法)和方法(你能完成的事情)
1、如何定义一个类?
2、如何将类转化为对象?实例化
实例化旨在面向对象编程中,使用类创建对象的过程称为实例化,是将一个抽象的概念具体化的过程
实例化一般由类名 对象名 = 类名(参数1,参数2,…)
例一:
需求:
小明今年18岁,身高175cm,每天在上要跑步,跑完去吃饭
小美今年16岁,身高165cm,每天不跑步,喜欢吃东西
class People():
name = 'laoli'
age = 20
def eat(self):
print('eat')
def sleep(self):
print('sleepping....')
p1 = People()
p2 = People()
print(p1.name)
print(p2.name)
p1.eat()
结果 :
laoli
laoli
eat
二、类的方法的构造,类的初始化自动执行
例二:
class People():
def __init__(self): #类的初始化自动执行,self是对象本身
print('self', self)
p1 = People()
print('p1',p1)
结果:
self <__main__.People object at 0x7fbfba000e50>
p1 <__main__.People object at 0x7fbfba000e50>
三、类的封装
定义
类的封装,写出类的模板
封装就是把同一类型的东西放在一起,通过self.xxx 来调用
例三:
class People:
def __init__(self, name, age):
self.name = name # self.name 对象和属性结合在一起; 后边的=name表示传进来的值
self.age = age # 同上
def __str__(self):
return 'I am %s' %self.name
def run(self):
print('%s is running' %self.name)
p1 = People('wang', 18)
print('p1:', p1.name)
p1.run()
print(p1)
p2 = People('fei', 24)
print('p2:', p1.name)
p2.run()
print(p2)
结果:
p1: wang
wang is running
I am wang
p2: wang
fei is running
I am fei
综合练习
练习1:
需求:1、小明体重75.0 公斤;
2、小明每次跑不会减肥0.5公斤;
3、小明每次吃东西体重会增加1.0公斤
需求:1、小明和小美都爱跑步
2、小美体重45.0公斤
3、小明体重75.0 公斤
4、每次跑步会减肥0.5公斤;
5、每次吃东西体重会增加1.0公斤
class People():
def __init__(self, name, weight):
self.name = name
self.weight = weight
def __str__(self):
return 'My name is %s,weight is %.2f' %(self.name, self.weight)
def run(self):
print('%s is running...' %self.name)
self.weight -= 0.5
def eat(self):
print('%s is eating...' %self.name)
self.weight += 1.0
xiaoming = People('xiaoming', 75.0)
xiaoming.run()
print(xiaoming)
xiaomei = People('xiaomei', 45.0)
xiaomei.eat()
print(xiaomei)
结果:
xiaoming is running...
My name is xiaoming,weight is 74.50
xiaomei is eating...
My name is xiaomei,weight is 46.00
练习二
栈类的封装
“”"
栈类的封装
“”"
class Stack(object):
def __init__(self):
self.stack = []
def __len__(self):
return len(self.stack)
# 最后写
def top(self):
if not self.is_empty():
return self.stack[-1]
raise Exception('Stack is empty')
def push(self, element):
self.stack.append(element)
def pop(self):
if self.is_empty():
raise Exception('Stack is empty')
else:
item = self.stack.pop()
return item
def length(self):
return len(self.stack)
def is_empty(self):
return len(self.stack) == 0
stack = Stack()
stack.push([1, 2])
stack.push(3)
print(stack.length())
item = stack.pop()
print('The pop item is: %s' %item)
print(stack.pop())
结果:
2
The pop item is: 3
[1, 2]
练习三
要求:
1,房子有户型,总面积和家具名称列表;
新房子没有任何家具
2, 家具有名字和占地面积,
其中床:占4平方;
衣柜:占2平方;
餐桌:占1.5平方
3, 将以上三件家具添加到房子中
4, 打印房子时,要求输出:户型,总面积,剩余面积,家具名称列表
“”"
class Furniture(object):
def __init__(self, name, area):
self.name = name
self.area = area
def __str__(self):
return '[%s] 占地 %.2f 平方' %(self.name, self.area)
class House(object):
def __init__(self, type, area):
self.type = type
self.area = area
self.free_area = area
self.fur_list = []
def __str__(self):
return (' 户 型: %s\n 总 面 积: %.2f\n 剩余面积: %.2f\n 家具名称: %s'
%(self.type, self.area, self.free_area, self.fur_list))
def add(self, item):
self.fur_list.append(item.name)
self.free_area -= item.area
bed = Furniture('bed', 10)
yigui = Furniture('yigui', 2)
table = Furniture('table', 1.5)
home = House('villa', 400)
home.add(bed)
home.add(yigui)
home.add(table)
print(home)
结果:
户 型: villa
总 面 积: 400.00
剩余面积: 386.50
家具名称: ['bed', 'yigui', 'table']