Python笔记之面向对象
1。类和对象
#create a class
class fruit:
def say(self):
print "hello, python" if __name__ == "__main__":
f = fruit() #不同于Java,不用new
f.say()
2,属性和方法
#create a class
class fruit:
price = 0<span style="white-space:pre"> </span>#类属性
def __init__(self):
self.color = "red"
zone = "china" # def getColor(self):
print self.color @ staticmethod #covert ordinary method to static method
def getPrice():
print fruit.price def say(self):
print "hello, python" if __name__ == "__main__":
f = fruit()
f.say()
apple = fruit()
apple.getColor()
构造函数。__init__()方法,可选,不提供有默认的
析构函数用语释放对象占用的资源。__del__()
垃圾回收机制,Python採用引用计数方式。
gc.collect() #显式调用垃圾回收器
3。继承
class Fruit:
def __init__(self, color):
self.color = color
print "fruit's color is %s" % self.color def sayname(self):
print "Fruit name" class Apple(Fruit):
def __init(self, color):
Fruit.__init__(self, color)
print "Apple's color is %s" % self.color
def sayname(self):
print "My name is Apple" class Banana(Fruit):
def __init__(self, color):
Fruit.__init__(self, color)
print "Banana's color is %s" % self.color
def sayname(self):
print "My name is banana" if __name__ == "__main__":
apple = Apple("red")
apple.sayname() banana = Banana("yelloe")
banana.sayname()
#抽象类模拟
def abstract():
raise NotImplementError(“abstract”) class Fruit:
def __init__(self):
if self.__class__ is Fruit:
abstract()
print “Fruit” class Apple(Fruit):
def __init(self):
Fruit.__init__(self)
print "Apple"
def sayname(self):
print "My name is Apple"
#多态。多重继承 略
Python笔记之面向对象的更多相关文章
- 8.python笔记之面向对象基础
title: 8.Python笔记之面向对象基础 date: 2016-02-21 15:10:35 tags: Python categories: Python --- 面向对象思维导图 (来自1 ...
- Python:笔记(3)——面向对象编程
Python:笔记(3)——面向对象编程 类和面向对象编程 1.类的创建 说明:和Java不同的是,我们不需要显示的说明类的字段属性,并且可以在后面动态的添加. 2.构造函数 构造函数的功能毋庸置疑, ...
- 9.Python笔记之面向对象高级部分
类的成员 类的成员可以分为三大类:字段.方法和属性 注:所有成员中,只有普通字段的内容保存对象中,即:根据此类创建了多少对象,在内存中就有多少个普通字段.而其他的成员,则都是保存在类中,即:无论对象的 ...
- python笔记 - day7-1 之面向对象编程
python笔记 - day7-1 之面向对象编程 什么时候用面向对象: 多个函数的参数相同: 当某一些函数具有相同参数时,可以使用面向对象的方式,将参数值一次性的封装到对象,以后去对象中取值即可: ...
- python学习笔记(10):面向对象
一.类和实例 1.类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 2.对象:通过类定义的数据结构实例.对象包括两个数据成员( ...
- python学习笔记(7): 面向对象
class Foo: #类中的函数 def bar(self): #功能阐述 print('Bar') pass def hello(self,name): print('i am %s' %name ...
- python笔记 - day8
python笔记 - day8 参考: http://www.cnblogs.com/wupeiqi/p/4766801.html http://www.cnblogs.com/wupeiqi/art ...
- python笔记 - day7
python笔记 - day7 参考: http://www.cnblogs.com/wupeiqi/articles/5501365.html 面向对象,初级篇: http://www.cnblog ...
- s21day19 python笔记
s21day19 python笔记 一.面向对象的基本知识 1.1 基本格式 # 定义类 class 类名: def 方法名(self,name): print(name) return 123 de ...
随机推荐
- QT VS检测内存泄漏
测试程序:http://download.csdn.net/detail/ajaxhe/4085447 vld-2.2.3: http://vld.codeplex.com/releases/view ...
- 顺为资本CEO许达来:为什么说中国创业者很幸福?(附PPT)
顺为资本创始合伙人许达来 编者按:许达来,顺为资本创始合伙人及CEO,代表性投资项目包括小米科技.丁香园.一起作业.加一联创.金山软件及兴达国际等. 本文为许达来在新浪创业举办的新创课活动上的内容分享 ...
- nginx 配置日志
http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $r ...
- 费用流&网络流模版
费用流模版: #include<cstdio> #include<cstring> #include<queue> using namespace std; ;// ...
- (收藏)KMP算法的前缀next数组最通俗的解释
我们在一个母字符串中查找一个子字符串有很多方法.KMP是一种最常见的改进算法,它可以在匹配过程中失配的情况下,有效地多往后面跳几个字符,加快匹配速度. 当然我们可以看到这个算法针对的是子串有对称属性, ...
- ceph install
Ceph : performance, reliability and scalability storage solution Contents 1 Introduction 1.1 Testing ...
- Automatic Preferred Max Layout Width is not available on iOS versions prior to 8.0
今天在真机调试低版本系统的时候出现如题类似Layout Max Width在ios 8 之前不适用的问题, 初步估计是autolayout 所导致的 查找资料解决方法如下: 将label下Preffe ...
- 我的Python成长之路---第三天---Python基础(13)---2016年1月16日(雾霾)
五.Python的常用的内置函数 Python为我们准备了大量的内置函数,如下图所示 这里我们只讨论红框内的内置函数 abs(x) 返回一个数的绝对值(模),参数可以是真说或浮点数 >>& ...
- POJ1054 枚举【STL__binary_search()_的应用】
①使用binary_search前要先保证有序 ②binary_search函数仅返回true或false ③binary_search(first element, laste lment + 1, ...
- POJ2395 最小生成树 - Prime算法
题目: Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description The ...