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 ...
随机推荐
- [Oracle] 11G自己主动收集统计信息
在11g中,默认自己主动收集统计信息的时间为晚上10点(周一到周五,4个小时),早上6点(周六,周日,20个小时),例如以下所看到的: select a.window_name, a.repeat_i ...
- poj1742 多重背包的可行性问题
http://poj.org/problem? id=1742 Description People in Silverland use coins.They have coins of value ...
- 安装帝国CMS遇到“修改php.ini,将:short_open_tag 设为 On”的解决方法+“建立目录不成功!请检查目录权限”问题
想用安装个帝国CMS来做个网站,于是下载了程序,上传到服务器上,但是在输入安装路径的时候却给出了如下图示: 您的PHP配置文件php.ini配置有问题,请按下面操作即可解决: 1.修改php.ini, ...
- ThinkPHP验证码类
//ThinkPHP验证码类使用$config = array( 'fontSize' => 30, // 验证码字体大小 'length' => 3, // 验证码位数 'useNois ...
- Mockito--完整功能介绍(转)
public interface DBAccess { int delete(String moi,String moc) throws Exception; void create(String m ...
- iOS开发网络数据之AFNetworking使用1
链接地址:http://blog.csdn.net/daiyelang/article/details/38421341 如何选择AFNetworking版本 官网下载2.5版本:http://afn ...
- 快捷键accesskey
<!DOCTYPE html> <html> <body> <a href="http://www.w3school.com.cn/html/&qu ...
- [转]php连接postgresql
首先推荐一下postgres数据库,免费,强大,甚至某些方面比商业数据库还要好,大家可以试试. 安装: 附安装图解(网上找的):http://blog.sina.com.cn/s/blog_5edb7 ...
- Ubuntu下安装和配置mysql
一.检查 1.检查是否已经安装mysql whereis mysql 2.检查mysql服务是否已经启动 sudo netstat -tap | grep mysql 如果没有安装,下面就进行安装. ...
- SQL2005、2008、2000 清空删除日志
SQL2005清空删除日志: 代码如下: Backup Log DNName with no_log '这里的DNName是你要收缩的数据库名,自己注意修改下面的数据库名,我就不再注释了. go d ...