Python Object Oriented
1. Creating class
class className:
'Optional class documentation string'
class_suite
The class has a documentation string, which can be accessed via ClassName.__doc__.
Example:
class Employee:
'Common base class for all employees'
empCount = 0 def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1
#class constructor or initialization method
#It's used to make a new class instance #the first argument to each method is self
#When we use this method, we do not need to add it. Python will adds the self argument to the list for you.
def displayCount(self):
print("Total Employee %d" % Employee.empCount) def displayEmployee(self):
print("Name : ", self.name, ", Salary: ", self.salary) #create an instance
e = Employee('Ben', 1000)
2. access an attribute
just like C++
#We can add, remove, or modify attributes of classes and objects at any time.
emp1.age = 7 # Add an 'age' attribute.
emp1.age = 8 # Modify 'age' attribute.
del emp1.age # Delete 'age' attribute.
hasattr(emp1, 'age') # Returns true if 'age' attribute exists
getattr(emp1, 'age') # Returns value of 'age' attribute
setattr(emp1, 'age', 8) # Set attribute 'age' at 8
delattr(empl, 'age') # Delete attribute 'age'
3. Built-in class attribute
ictionary containing the class's namespace.
__name__ #Class name.
__module__ #Module name in which the class is defined. This attribute is "__main__" in interactive mode.
__bases__ #A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list.
4. destroy
#Python deletes unneeded objects (built-in types or class instances) automatically to free the memory space.
'''
An object's reference count increases when it is assigned a new name or placed in a container (list, tuple, or dictionary).
The object's reference count decreases when it's deleted with del, its reference is reassigned, or its reference goes out of scope.
When an object's reference count reaches zero, Python collects it automatically.
'''
class Point:
def __init( self, x=0, y=0):
self.x = x
self.y = y
def __del__(self):
class_name = self.__class__.__name__
print class_name, "destroyed" pt1 = Point()
pt2 = pt1
pt3 = pt1
print id(pt1), id(pt2), id(pt3) # prints the ids of the obejcts
del pt1
del pt2
del pt3 #result
3083401324 3083401324 3083401324
Point destroyed
5. inheritance
class SubClassName (ParentClass1[, ParentClass2, ...]):
'Optional class documentation string'
class_suite
Python Object Oriented的更多相关文章
- Object Oriented Programming python
Object Oriented Programming python new concepts of the object oriented programming : class encapsula ...
- 《Using Databases with Python》Week1 Object Oriented Python 课堂笔记
Coursera课程<Using Databases with Python> 密歇根大学 Charles Severance Week1 Object Oriented Python U ...
- Python学习札记(三十) 面向对象编程 Object Oriented Program 1
参考:OOP NOTE 1.面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. ...
- CSharpGL - Object Oriented OpenGL in C#
Object Oriented OpenGL in C#
- What is Object Oriented Design? (OOD)
Object Oriented Design is the concept that forces programmers to plan out their code in order to hav ...
- Python Object Graphs — objgraph 1.7.2 documentation
Python Object Graphs - objgraph 1.7.2 documentation Python Object Graphs¶ objgraph is a module that ...
- Java - 面向对象(object oriented)计划 详细解释
面向对象(object oriented)计划 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24058107 程序包括 ...
- OO开发思想:面向对象的开发方法(Object oriented,OO)
面向对象的开发方法(Object oriented,OO)认为是好文章吧,拿来分享一下(转载) 面向对象的开发方法(Object oriented,OO) 从事软件开发的工程 师们常常有这样 的体会: ...
- JavaScript: Constructor and Object Oriented Programming
Constructor : Grammar: object.constructor Example: Javascript code: 1 function obj1() { this.number ...
随机推荐
- ORA-06512: at "SYS.XMLTYPE" 问题记录
执行SQL报错: SQL> SELECT COUNT(cl.enable_flg) FROM cont_ledger cl INNER JOIN project_project pp ON cl ...
- 使用pipework将Docker容器配置到本地网络环境中
使用pipework将Docker容器配置到本地网络环境中 需求 在使用Docker的过程中,有时候我们会有将Docker容器配置到和主机同一网段的需求.要实现这个需求,我们只要将Docker容器和主 ...
- String.prototype.getParm
String.prototype.getParms=function(name){ var reg = new RegExp('(^|&)' + name + '=([^&]*)(&a ...
- java List 学习
要学习List<E>接口,首先,我知道它还有一个父接口Collection<E>.而Collection<E>又有一个超级接口Iterable<T>. ...
- js之数码时钟加随机变色
HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- php自动添加相关文章
{pc:content action="relation" relation="$relation" id="$id" catid=&quo ...
- 大规模向量相似度计算方法(Google在07年发表的文章)
转载请注明出处:http://www.cnblogs.com/zz-boy/p/3648878.html 更多精彩文章在:http://www.cnblogs.com/zz-boy/ 最近看了Goog ...
- get、post接口测试-java
public class HttpClient { //get请求方法 public String sendGet(String url, String data) { Date date = new ...
- BZOJ5142: [Usaco2017 Dec]Haybale Feast(双指针&set)(可线段树优化)
5142: [Usaco2017 Dec]Haybale Feast Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 182 Solved: 131[ ...
- xml的读取(曾删改)
获取XML 得到 需要查询的字段名 private string GetXml(string TableName) { try { string TbName = TableName.Split('_ ...