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. [移动网关]2G环境下资源下载有一定概率失败,客户端日志显示收到403错误

    2G环境下资源下载有一定概率失败,客户端日志显示收到403错误 问题现象: 测试同学在使用联通号码在移动网络环境下,访问连接得到的response_code出现是403,导致资源读取失败表情显示异常. ...

  2. iOS/Xcode异常:reason = “The model used to open the store is incompatible with the one used to create the store”

    reason=The model used to open the store is incompatible with the one used to create the store 出现上述异常 ...

  3. server配置学习 ---- 关闭防火墙

    iptables 一种网络防火墙,在LINUX下使用,RedHat9.0版本号以上自带. 它能够实现NAT转换.能够做上网代理. 首先对于server的配置第一步来说就是关闭防火墙.在没有图形化中的l ...

  4. ubuntu15.04更新软件源

    1,首先备份原来的源 sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup2,编辑软件源的文件 sudo vim /etc/apt/so ...

  5. Spring MVC整体处理流程

    一.spring整体结构 首先俯视一下spring mvc的整体结构 二.处理流程 1.请求处理的第一站就是DispatcherServlet.它是整个spring mvc的控制核心.与大多数的jav ...

  6. Deflater与Inflater的压缩与解压缩

    原文:Deflater与Inflater的压缩与解压缩 package util; import java.util.Arrays; import java.util.zip.Deflater; im ...

  7. This exception may occur if matchers are combined with raw values

    org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers!3 ma ...

  8. JavaScript的实现

    了解了JavaScript是干什么的< 对一些词的理解 >,下面该知道它是怎么实现的. 一个完整的JavaScript是由三部分组成的,如下图 ECMAScript 可以为不同种类的宿主环 ...

  9. 斯坦福大学IOS开发课程笔记(第七课第一部分)

    转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/31462099 作者:小马 这节课的内容太多,分两部分介绍.本节课主要是介绍怎样开发 ...

  10. Android 蓝牙( Bluetooth)耳机连接分析及实现

    Android 实现了对Headset 和Handsfree 两种profile 的支持.其实现核心是BluetoothHeadsetService,在PhoneApp 创建的时候会启动它. if ( ...