Error message here!

Hide Error message here!

Error message here!

Hide Error message here!

Error message here!

Close

# 多态和封装

Sunzz 2019-01-26 11:55:00 阅读数:249 评论数:0 点赞数:0 收藏数:0

### 一、多态

('apple',4)

def get_price(object):
if isinstance(object,tuple):
return object[1]
else:
return magic_network(object)

# 这里使用isinstance 来执行类型检查旨在说明：使用类型价差通常是馊主意，应尽可能避免。

def get_price(object):
if isinstance(object,tuple):
return object[1]
elif isinstance(object,dict):
return int(object['price'])
else:
return magic_network(object)

#### 多态和方法

print(object.get_price())
4

str1= “abcd"
str1.count("a")
len(str1)
list1 = [1,2,3,4]
len(list1)

#### 多态形式多样

print(1 + 2)
3
price("hello" + "world")
helloworld

### 二 、封装

class OpenObject():
def __init__(self):
pass
def set_name(self,name):
self.name= name
def get_name(self):
return self.name

o=OpenObject()
o.set_name("Sir Lancelot")
o.get_name()
Sir Lancelot

class OpenObject():
name = "Sir Lancelot"
def __init__(self):
pass
def set_name(self, name):
OpenObject.name = name
def get_name(self):
return self.name

o = OpenObject()
o1 = OpenObject()
print(o1.get_name())
o.set_name("Sir Grub")
print(o.get_name())
print(o1.get_name())

Sir Lancelot
Sir Grub
Sir Grub

class OpenObject():
def __init__(self):
pass
def set_name(self, name):
self.name = name
def get_name(self):
return self.name
o = OpenObject()
o.set_name("Sir Grub")
print(o.get_name())

Sir Grub

o1 = OpenObject()
print(o1.get_name())

Sir Lancelot

print(o.get_name())
Sir Grub

https://www.cnblogs.com/Sunzz/p/10323042.html

30万现金开奖等你来领