Python中类的定义与使用
目标:
1.类的定义
2.父类,子类定义,以及子类调用父类
3.类的组合使用
4.内置功能
1.类的定义
代码如下:
#!/usr/bin/env python
#coding:utf8 class Hotel(object):
"""docstring for Hotel"""
def __init__(self, room, cf=1.0, br=15):
self.room = room
self.cf = cf
self.br = br def cacl_all(self, days=1):
return (self.room * self.cf + self.br) * days if __name__ == '__main__':
stdroom = Hotel(200)
big_room = Hotel(230, 0.9)
print stdroom.cacl_all()
print stdroom.cacl_all(2)
print big_room.cacl_all()
print big_room.cacl_all(3)
2.父类、子类以及调用父类
代码如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*- # 父类
class AddBook(object):
def __init__(self, name, phone):
self.name = name
self.phone = phone def get_phone(self):
return self.phone # 子类,继承
class EmplEmail(AddBook):
def __init__(self, nm, ph, email):
# AddBook.__init__(self, nm, ph) # 调用父类方法一
super(EmplEmail, self).__init__(nm, ph) # 调用父类方法二
self.email = email def get_email(self):
return self.email # 调用
if __name__ == "__main__":
Detian = AddBook('handetian', '')
Meng = AddBook('shaomeng', '') print Detian.get_phone()
print AddBook.get_phone(Meng) alice = EmplEmail('alice', '', 'alice@xkops.com')
print alice.get_email(), alice.get_phone()
3.类的组合使用
代码如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*- '''
1.class类的组合使用
2.手机、邮箱、QQ等是可以变化的(定义在一起),姓名不可变(单独定义)。
3.在另一个类中引用
''' class Info(object):
def __init__(self, phone, email, qq):
self.phone = phone
self.email = email
self.qq = qq def get_phone(self):
return self.phone def update_phone(self, newphone):
self.phone = newphone
print "手机号更改已更改" def get_email(self):
return self.email class AddrBook(object):
'''docstring for AddBook'''
def __init__(self, name, phone, email, qq):
self.name = name
self.info = Info(phone, email, qq) if __name__ == "__main__":
Detian = AddrBook('handetian', '', 'detian@xkops.com', '')
print Detian.info.get_phone()
Detian.info.update_phone(18210413002)
print Detian.info.get_phone()
print Detian.info.get_email()
4.内置功能(函数()加与不加的区别)
代码如下:
#!/usr/bin/env python
#coding:utf8 class Books(object):
def __init__(self, title, author):
self.title = title
self.author = author def __str__(self):
return self.title def __repr__(self):
return self.title def __call__(self):
print "%s is written by %s" %(self.title, self.author) if __name__ == '__main__':
pybook = Books('Core Python', 'Wesley')
print pybook
pybook()
----------------------------------------------------------------------------------------------------------------------------------------------------------------
#!/usr/bin/env python
#coding:utf8 class Number(object):
"""Custum object
add/radd -> +;
sub/rsub -> -;
mul/rmul -> *;
div/rdiv -> /;
"""
def __init__(self, number):
self.number = number def __add__(self, other):
return self.number + other def __radd__(self, other):
return self.number + other def __sub__(self, other):
return self.number - other def __rsub__(self, other):
return other - self.number def __gt__(self, other):
if self.number > other:
return True
return False if __name__ == '__main__':
num = Number(10)
print num + 20
print 30 + num
print num - 5
print 11 - num
print num > 20
Python中类的定义与使用的更多相关文章
- python中类的定义、实例化、封装以及私有变量/方法
1. 定义类 python中定义一个类的格式如下: class MyClass(object): def __init__(self,data1,data2): self.__data1=data1 ...
- Python中类的定义及使用
类是一些有共同特征和行为事务事物的抽象概念的总和. 从中可以看出,方法只能使用实例直接调用(无需传self参数),而使用类调用必须传入实例对象: 属性可以使用实例调用,也可以使用类直接调用,因此可以看 ...
- python中类的定义方法
# coding =utf-8 ## 类的定义 ##-------------------------------------------------- class Employee: empCoun ...
- Python中类的定义
class Student(object): # 有点类似其它高级语言的构造函数 def __init__(self,name,score): self.name = name self.score ...
- python中类中属性和方法的具体定义方法和使用
1. Python中类中特性分成属性和方法 属性和方法都分为私有和公有的,私有的只可以在本类中使用外部是无法访问的 2. 定义属性(成员变量)的语法格式(公有属性/私有属性) class 类名: de ...
- python类的定义和使用
python中类的声明使用关键词class,可以提供一个可选的父类或者说基类,如果没有合适的基类,那就用object作为基类. 定义格式: class 类名(object): "类的说明文档 ...
- python中类的继承
python中类的继承 在python中面向对象编程中实现继承,以下面一个实例进行说明. class SchoolMenber(): # __init__类似于c++中的构造函数 # __init__ ...
- python中类的属性(class attribute)的解释
python中的类叫 class object,类的实例叫instance object. 类 Class Objects 类拥有两种操作,1.类属性 attribute references 2.实 ...
- Python中类的__init__继承
Python中类的__init__继承 概念: 定义父类 In [10]: class Person: ....: def __init__(self,name,age,sex): ....: sel ...
随机推荐
- Java 默认事务级别read committed对binlog_format的需求
转载: java.sql.SQLException: Cannot execute statement: impossible to write to binary log since BINLOG_ ...
- [UE4]运行时创建Actor
- Linux下apache2及模块mod_deflate等安装和配置
安装apache 1.wget http://archive.apache.org/dist/httpd/httpd-2.2.13.tar.gz 2.在安装目录 先让大家看看实际效果,请看下图10点中 ...
- Chrome 鼠标左键-新标签打开
改chrome设置 1.打开google搜索主页2.打开右下角Settings选项->Search Settings3.找到where results open选项4.把Open each se ...
- 跳表(skiplist)Python实现
# coding=utf-8 # 跳表的Python实现 import random # 最高层数设置为4 MAX_LEVEL = 4 def randomLevel(): ""& ...
- ORM( ORM查询13种方法3. 单表的双下划线的使用 4. 外键的方法 5. 多对多的方法 ,聚合,分组,F查询,Q查询,事务 )
必知必会13条 <1> all(): 查询所有结果 <2> get(**kwargs): 返回与所给筛选条件相匹配的对象,返回结果有且只有一个,如果符合筛选条件的对象超过一个或 ...
- swagger配置
1.pom.xml <!--swagger2--> <dependency> <groupId>io.springfox</groupId> <a ...
- vb 导出excel生成图表统计
Private Sub btnExprot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnE ...
- 过滤器 ;spring拦截器 切片 小结
1. springMVc的拦截器 实现HandlerInterceptor接口,如下: public class HandlerInterceptor1 implements HandlerInter ...
- VisualSVN:允许修改svn提交日志(pre-revpro-change hook)
有时候需要对之前版本提交的错误的日志信息进行修改或者进行补充描述: 1.在windows 7( 64位 )下使用TortoiseSVN客户端,选中代码目录,点击右键,选择<显示日志> 在出 ...