python的对象
来源:互联网 发布:linux 发送arp包 编辑:程序博客网 时间:2024/06/06 09:32
对象:模拟真实世界,把数据代码都封装在一起。所谓对象,就是属性+方法
1.封装
例子:
创建对象
tt = Turtle()
tt.run()
2.继承
例(MyList 继承了list类这个方法与属性):
注:如果子类中定义与父类同名的方法或者属性,则会自动覆盖父类对应的方法和属性(包括__init__()方法)。
支持多重继承(容易出现代码混乱,尽量少用)
class ClassName(Base1,Base2,Base3...)
3.多态:不同对象对同一方法响应不同的行动
class A:
def fun(self):
print("我是A")
class B:
def fun(self):
print("我是B")
self是什么东西?self相当于c++的this指针
例:
>>> a = Ball()
>>> a.setName('球A')
>>> b= Ball()
>>> b.setName
<bound method Ball.setName of <__main__.Ball instance at 0x0201ADF0>>
>>> b.setName("球B")
>>> a.kick()
我叫球A,谁踢我!
>>> b.kick()
我叫球B,谁踢我!
>>>
python的特殊方法:
__init__(self):构造函数,对象被创建的时候自动调用;可以自定义,实现对象的初始化操作。
例:
>>> class Ball:
def __init__(self,name):
self.name = name
def kick(self):
print("我叫%s,谁踢我"%self.name)
>>> c = Ball("土豆")
>>> c.kick()
我叫土豆,谁踢我
4.公有和私
>>> class Person:
name = "heres"
>>> p = Person()
>>> p.name
'heres'有
为了实现类似私有对象的特征,python内部采用了一种name mangling (名字改编\重造)技术
在Python中定义私有变量只需要在变量名或函数名前加上“__”两个下滑线,那么这个函数或者变量就会为私有的了。
所谓的name mangling ,Python只不过把原来的名字改成了_类名__变量名,所以是伪私有。
>>> class Person:
__name = "heres"
def getName(self):
return self.__name
>>> o = Person()
>>> o.name
Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
o.name
AttributeError: Person instance has no attribute 'name'
>>> o.getName()
'heres'
>>> o._Person__name
'heres'
5.组合(不同物种,横向关系):
6.类,类对象,示例对象
>>> a= C()
>>> b = C()
>>> c = C()
>>> a.count
0
>>> b.count
0
>>> c.count
0
>>> c.count +=100
>>> c.count
100
>>> b.count
0
>>> a.count
0
>>> C.count
0
>>> C.count +=99
>>> a.count
99
>>> b.count
99
>>> c.count
100
如果属性和方法名相同,属性会覆盖方法
>>> class C:
def x(self):
print("x-man!")
>>> c = C()
>>> c.x()
x-man!
>>> c.x=1
>>> c.x
1
>>> c.x()
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
c.x()
TypeError: 'int' object is not callable
6.什么是绑定?
Python严格要求方法需要有实例才能被调用,这种限制其实就是Python所谓的绑定概念。
需要self来接收对象,实现绑定。
当执行dd.setXY(5,6),其实是dd.setXY(self,5,6)
>>> class CC:
def setXY(self,x,y):
self.x = x
self.y = y
def printXY(self):
print(self.x,self.y)
>>> dd = CC()
>>> dd.__dict__
{}
>>> CC.__dict__
{'__module__': '__main__', 'printXY': <function printXY at 0x02C4C730>, 'setXY': <function setXY at 0x02C4C6F0>, '__doc__': None}
AttributeError: CC instance has no attribute 'se'
>>> dd.setXY(5,6)
>>> dd.__dict__
{'y': 6, 'x': 5}
>>> CC.__dict__
{'__module__': '__main__', 'printXY': <function printXY at 0x02C4C730>, 'setXY': <fun
当我们删除类对象时,再创建CC的实例时,就会报错;
但是我们依然可以执行之前创建的dd实例对象的方法,这是因为类中定义的属性和方法是静态的,即使类对象被删除了,他们依然存放在内存中的。
>>> del CC
>>> ee = CC()
Traceback (most recent call last):
File "<pyshell#43>", line 1, in <module>
ee = CC()
NameError: name 'CC' is not defined
>>> dd.printXY()
5,6
7.一些相关的BIF
issubclass(class,classinfo) class 是 classinfo的一个子类,那么就返回true。
注意:类自身也是自己的子类;classinfo可以是一个元组
isinstance(object,classinfo) 检查是否是实例对象
hasattr(object,name) object对象是否有name(要用字符串给括起来)的属性
getattr(object,name[,default]) 返回对象指定的属性值,如果指定属性值不存在,那么就返回default(如果设置的话)
setattr(object,name,value)
1.封装
例子:
class Turtle:#Python 中的类名约定以大写字母开头 """ 关于类的一个简单的例子""" #属性 color = 'green' weight = 10 shell = True #方法 def climb(self): print("我正很努力的向前爬。。。。") def run(self): print("我正在飞快的跑。。。。")
创建对象
tt = Turtle()
tt.run()
2.继承
例(MyList 继承了list类这个方法与属性):
class MyList(list):#表示什么操作都不做passlist1 = MyList()list1.appand(9)
注:如果子类中定义与父类同名的方法或者属性,则会自动覆盖父类对应的方法和属性(包括__init__()方法)。
支持多重继承(容易出现代码混乱,尽量少用)
class ClassName(Base1,Base2,Base3...)
3.多态:不同对象对同一方法响应不同的行动
class A:
def fun(self):
print("我是A")
class B:
def fun(self):
print("我是B")
self是什么东西?self相当于c++的this指针
例:
>>> a = Ball()
>>> a.setName('球A')
>>> b= Ball()
>>> b.setName
<bound method Ball.setName of <__main__.Ball instance at 0x0201ADF0>>
>>> b.setName("球B")
>>> a.kick()
我叫球A,谁踢我!
>>> b.kick()
我叫球B,谁踢我!
>>>
python的特殊方法:
__init__(self):构造函数,对象被创建的时候自动调用;可以自定义,实现对象的初始化操作。
例:
>>> class Ball:
def __init__(self,name):
self.name = name
def kick(self):
print("我叫%s,谁踢我"%self.name)
>>> c = Ball("土豆")
>>> c.kick()
我叫土豆,谁踢我
4.公有和私
>>> class Person:
name = "heres"
>>> p = Person()
>>> p.name
'heres'有
为了实现类似私有对象的特征,python内部采用了一种name mangling (名字改编\重造)技术
在Python中定义私有变量只需要在变量名或函数名前加上“__”两个下滑线,那么这个函数或者变量就会为私有的了。
所谓的name mangling ,Python只不过把原来的名字改成了_类名__变量名,所以是伪私有。
>>> class Person:
__name = "heres"
def getName(self):
return self.__name
>>> o = Person()
>>> o.name
Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
o.name
AttributeError: Person instance has no attribute 'name'
>>> o.getName()
'heres'
>>> o._Person__name
'heres'
5.组合(不同物种,横向关系):
class Turtle: def __init__(self,x): self.num = xclass Fish1: def __init_(self,x): self.num = xclass Pool: def __init__(self,x,y): self.turtle = Turtle(x) self.fish = Fish1(y) def print_num(self): print("水池中有%d 条乌龟,有%d 条鱼"%(self.turtle.num,self.fish.num))
6.类,类对象,示例对象
>>> a= C()
>>> b = C()
>>> c = C()
>>> a.count
0
>>> b.count
0
>>> c.count
0
>>> c.count +=100
>>> c.count
100
>>> b.count
0
>>> a.count
0
>>> C.count
0
>>> C.count +=99
>>> a.count
99
>>> b.count
99
>>> c.count
100
如果属性和方法名相同,属性会覆盖方法
>>> class C:
def x(self):
print("x-man!")
>>> c = C()
>>> c.x()
x-man!
>>> c.x=1
>>> c.x
1
>>> c.x()
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
c.x()
TypeError: 'int' object is not callable
6.什么是绑定?
Python严格要求方法需要有实例才能被调用,这种限制其实就是Python所谓的绑定概念。
需要self来接收对象,实现绑定。
当执行dd.setXY(5,6),其实是dd.setXY(self,5,6)
>>> class CC:
def setXY(self,x,y):
self.x = x
self.y = y
def printXY(self):
print(self.x,self.y)
>>> dd = CC()
>>> dd.__dict__
{}
>>> CC.__dict__
{'__module__': '__main__', 'printXY': <function printXY at 0x02C4C730>, 'setXY': <function setXY at 0x02C4C6F0>, '__doc__': None}
AttributeError: CC instance has no attribute 'se'
>>> dd.setXY(5,6)
>>> dd.__dict__
{'y': 6, 'x': 5}
>>> CC.__dict__
{'__module__': '__main__', 'printXY': <function printXY at 0x02C4C730>, 'setXY': <fun
当我们删除类对象时,再创建CC的实例时,就会报错;
但是我们依然可以执行之前创建的dd实例对象的方法,这是因为类中定义的属性和方法是静态的,即使类对象被删除了,他们依然存放在内存中的。
>>> del CC
>>> ee = CC()
Traceback (most recent call last):
File "<pyshell#43>", line 1, in <module>
ee = CC()
NameError: name 'CC' is not defined
>>> dd.printXY()
5,6
7.一些相关的BIF
issubclass(class,classinfo) class 是 classinfo的一个子类,那么就返回true。
注意:类自身也是自己的子类;classinfo可以是一个元组
isinstance(object,classinfo) 检查是否是实例对象
hasattr(object,name) object对象是否有name(要用字符串给括起来)的属性
getattr(object,name[,default]) 返回对象指定的属性值,如果指定属性值不存在,那么就返回default(如果设置的话)
setattr(object,name,value)
阅读全文
0 0
- python对象的相等
- Python对象的拷贝
- Python对象的拷贝
- python的内置对象
- python的对象引用
- python的对象探寻.
- python的面向对象
- python的对象
- Python的对象类型
- python的对象
- Python基础:Python的变量和对象
- Python基础:Python的变量和对象
- 【Python】Python的面向对象机制
- Python基础:Python的变量和对象
- python 符合Python风格的对象
- python 符合Python风格的对象
- 【Python】Python的类、对象、属性、方法
- 面向对象的python(一)
- 快速幂取模
- 对OpenGLES中VBO与VAO的理解
- Codeforces 193B
- xcode编译代码到非测试机
- HashMap简单分析
- python的对象
- WebSocket实现Android客户端之间的简单通讯
- @Table
- qml与C++的交互
- IntelliJ IDEA设置自动导入包
- Redis-发布订阅
- Unity Build exe 设置默认分辨率
- HashMap,LinkedHashMap,TreeMap的区别
- App:transformClassesWithDexForDebug