36.面向对象-封装

  1. 封装
  2. 身份运算符

封装

  1. 封装时面向对象编程的一大特点
  2. 面向对象编程的第一步——将属性和方法封装到一个抽象的类中
  3. 外界使用类创建对象,然后让对象调用方法
  4. 对象方法的细节都被封装在类的内部

案例提示:

  1. 在对象方法内部,是可以可以直接访问对象的属性的
  2. 同一个类创建的多个对象之间,属性互不干预
  3. 一个对象的属性,可以是另一个类创建的对象

被使用的类,通常应该先开发
定义没有初始值的属性
在定义属性时,如果不知道设置什么初始值,可以设置为None

  • None关键字表示什么都没有
  • 表示一个空对象,没有方法和属性,是一个特殊的常量
  • 可以将None赋值给任何一个变量

身份运算符

身份运算符用于比较两个对象的内存地址是否一致——是否是同一个对象的引用

  • 在Python中针对None比较时,建议使用is判断
运算符 描述 实例
is 用于判断两个标识符是不是引用同一个对象 x is y, 类似 id(x) == id(y)
is not is not 是判断两个标识符是不是引用不同对象 x is not y, 类似 id(x) !=id(y)

is 与==区别:

  • is 用于判断两个变量引用对象是否为同一个
  • ==用于判断引用对象的值是否相等

例子1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Person:
def __init__(self, name, weight):
self.name = name
self.weight = weight

def __str__(self):
return "我的名字是%s,体重是 %.2f公斤" % (self.name, self.weight)

def run(self):
print("%s跑步" % self.name)
self.weight -= 0.5

def eat(self):
print("吃东西%s" % self.name)
self.weight += 1


xiaoming = Person("小明", 74.44)
xiaoming.run()
xiaoming.eat()

xiaomei = Person("小美", 45)
xiaomei.eat()
xiaomei.run()

print(xiaoming)
print(xiaomei)
print(xiaoming)

例子2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class HouseItem:
def __init__(self, name, area):
self.name = name
self.area = area

def __str__(self):
return "[%s]占地 %.2f" % (self.name, self. area)


class House:
def __init__(self, house_type, area):
self.house_type = house_type
self.area = area
# 剩余面积
self.free_area = area

# 家具名词
self.item_list = []

def __str__(self):
# python 能够自动将一对括号内部的代码连接在一起
return ("户型: %s\n总面积: %.2f[剩余:%.2f]\n家具:%s"
% (self.house_type, self.area,
self.free_area, self.item_list))

def add_item(self, item):
print("要添加%s" % item)
"""if里面变大于号的话,可以用return直接结束,然后不要else"""
if item.area < self.free_area:
self.free_area = self.free_area - item.area
self.item_list.append(item.name)
else:
print("空间不足,无法添加:%s" % item.name)


bed = HouseItem("床", 3)
chest = HouseItem("衣柜", 2)
table = HouseItem("餐桌", 1.3)
ball = HouseItem("巨无霸", 60)

print(bed)
print(chest)
print(table)

# 创建房子对象
my_home = House("两室一厅", 60)
my_home.add_item(bed)
my_home.add_item(chest)
my_home.add_item(table)
my_home.add_item(ball)

print(my_home)

例子3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Gun:
def __init__(self, model):
self.model = model
self.bullet_count = 0

def add_buller(self, count):
self.bullet_count += count

def shoot(self):
if self.bullet_count <= 0:
print("[%s]没有子弹了" % self.model)
return

self.bullet_count -= 1
print("%s 突突 [%d]" % (self.model, self.bullet_count))


class Solder:
def __init__(self, name):
self.name = name
# 新兵没有枪
self.gun = None

def fire(self):
# 身份运算符
# if self.gun == None:
if self.gun is None:
print("%s没有枪。。。" % self.name)
return

print("冲%s" % self.name)
self.gun.add_buller(50)
self.gun.shoot()


ak47 = Gun("ak47")

xusanduo = Solder("许三多")
xusanduo.gun = ak47
xusanduo.fire()

转载请注明来源,欢迎指出任何有错误或不够清晰的表达。可以邮件至gxnucgb@qq.com

文章标题:36.面向对象-封装

文章字数:810

本文作者:陈桂彬

发布时间:2019-08-05, 17:33:45

最后更新:2019-08-05, 18:08:35

原始链接:https://github.com/gxnucgb/gxnucgb.github.io/2019/08/05/36-面向对象-封装/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

喜欢就点赞,疼爱就打赏

github