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笔记之面向对象的更多相关文章

  1. 8.python笔记之面向对象基础

    title: 8.Python笔记之面向对象基础 date: 2016-02-21 15:10:35 tags: Python categories: Python --- 面向对象思维导图 (来自1 ...

  2. Python:笔记(3)——面向对象编程

    Python:笔记(3)——面向对象编程 类和面向对象编程 1.类的创建 说明:和Java不同的是,我们不需要显示的说明类的字段属性,并且可以在后面动态的添加. 2.构造函数 构造函数的功能毋庸置疑, ...

  3. 9.Python笔记之面向对象高级部分

    类的成员 类的成员可以分为三大类:字段.方法和属性 注:所有成员中,只有普通字段的内容保存对象中,即:根据此类创建了多少对象,在内存中就有多少个普通字段.而其他的成员,则都是保存在类中,即:无论对象的 ...

  4. python笔记 - day7-1 之面向对象编程

    python笔记 - day7-1 之面向对象编程 什么时候用面向对象: 多个函数的参数相同: 当某一些函数具有相同参数时,可以使用面向对象的方式,将参数值一次性的封装到对象,以后去对象中取值即可: ...

  5. python学习笔记(10):面向对象

    一.类和实例 1.类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 2.对象:通过类定义的数据结构实例.对象包括两个数据成员( ...

  6. python学习笔记(7): 面向对象

    class Foo: #类中的函数 def bar(self): #功能阐述 print('Bar') pass def hello(self,name): print('i am %s' %name ...

  7. python笔记 - day8

    python笔记 - day8 参考: http://www.cnblogs.com/wupeiqi/p/4766801.html http://www.cnblogs.com/wupeiqi/art ...

  8. python笔记 - day7

    python笔记 - day7 参考: http://www.cnblogs.com/wupeiqi/articles/5501365.html 面向对象,初级篇: http://www.cnblog ...

  9. s21day19 python笔记

    s21day19 python笔记 一.面向对象的基本知识 1.1 基本格式 # 定义类 class 类名: def 方法名(self,name): print(name) return 123 de ...

随机推荐

  1. 结构体struct和联合体union以及enum枚举体5的区别

    下面来自wikipedia: In computer science, a union is a value that may have any of several representations ...

  2. java学习之观察者设计模式

    package com.gh.observer; import java.util.Observable; /** * 被观察者对象 * 必须继承被观察者抽象类 * @author ganhang * ...

  3. hdu 1236 1.3.2排名

    排名 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission ...

  4. DIOR HOMME_百度百科

    DIOR HOMME_百度百科     DIOR HOMME    编辑    Dior Homme 男装品牌,中文名迪奥·桀傲,由迪奥 (Dior) 在2001年更名更来,品牌来源地法国.迪奥·桀傲 ...

  5. 一个轻client,多语言支持,去中心化,自己主动负载,可扩展的实时数据写服务的实现方案讨论

    背景 背景是设计一个实时数据接入的模块,负责接收client的实时数据写入(如日志流,点击流),数据支持直接下沉到HBase上(兴许提供HBase上的查询),或先持久化到Kafka里.方便兴许进行一些 ...

  6. “易信”今日正式更新至V1.1版

    热门移动通讯社交应用“易信”今日正式更新至V1.1版,目前用户已可在苹果AppStore和各大Android商店下载.新版本主要包括三大变化:开通公众平台.提供外部分享.强化社交安全,此外包含好友关系 ...

  7. setInterval定义与调用

    以下是一个倒计时,可以定义定时器为全局变量(ti2),或局部变量(ti). <script type="text/javascript" src="js/jquer ...

  8. iOS 类别和扩展(Categories和Extensions)

    分类(Category)   分类能够做到的事情主要是:即使在你不知道一个类的源码情况下,向这个类添加扩展的方法.   此外,分类能够保证你的实现类和其他的文件区分开.   1 #import “UI ...

  9. [POJ 2588]--Snakes(并查集)

    题目链接:http://poj.org/problem?id=2588 Snakes Time Limit: 1000MS   Memory Limit: 65536K   Description B ...

  10. mong 备份和恢复

    [root@hy-mrz01 bin]# ./mongofiles list -h114.55.5.57 -db pics 20160602152850deeabcb1bd2644afa0c3a9a8 ...