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 ...
随机推荐
- android linearlayout 把控件view置底部(放在页面最下方)
<LinearLayout android:id="@+id/recLayout" android:layout_width="fill_parent" ...
- 【充电器】小米手机2S电池座充——小米手机官网
ligh@local-host$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.0.3 [充电器]小米手机2S电池座充--小米手机官网 小米手机2S电池座 ...
- Android中通过进程注入技术改动广播接收器的优先级
前言 这个周末又没有吊事,在家研究了怎样通过进程的注入技术改动广播接收器的优先级.关于这个应用场景是非常多的.并且也非常重要.所以就非常急的去fixed了. Android中的四大组件中有一个广播:B ...
- linux命令--sysctl
sysctl sysctl被用来在执行时配置内核参数.这些参数都存储在/proc/sys/(以键-值对形式存储)中.你可以用sysctl来读和写数据 命令参数 variable 要读的键值的名字 ...
- 西门子PLC学习笔记二-(工作记录)
今天师傅给讲了讲做自己主动化控制的总体的思路,特进行一下记录,做个备忘. 1.需求分析 本次的项目是对楼宇循环供水的控制,整个项目须要完毕压力.压差.温度等的获取及显示.同一时候完毕电机的控制. 2. ...
- Android图像篇
Android的渲染分为2D渲染和3D渲染两种,当中2D渲染的引擎为Skia.3D渲染的引擎是OpenGL ES.眼下.Android支持OpenGL ES1.0和OpenGL ES 2.0两种标准. ...
- 使用client对象模型回写SharePoint列表
使用client对象模型回写SharePoint列表 client对象模型是一个有效的方式回写SharePoint列表. 1. 管理员身份打开VS,新建WPF应用程序SPWriteListApp,确保 ...
- Android Studio入门到精通
链接地址:http://blog.csdn.net/yanbober/article/details/45306483 目标:Android Studio新手–>下载安装配置–>零基础入门 ...
- Java--再次理解多态
Java中多态性(polymorphism)的实现 什么是多态 1. 面向对象的三大特性:封装.继承.多态.从一定角度来看,封装和继承几乎都是为多态而准备的.这是我们最后一个概念,也是最重要的知识点. ...
- 【转】介绍几个图论和复杂网络的程序库 —— BGL,QuickGraph,igraph和NetworkX
原文来自:http://blog.sciencenet.cn/blog-404069-297233.html 作复杂网络研究离不开对各种实际或模拟网络的统计.计算.绘图等工作.对于一般性的工作,我们可 ...