Python面向对象编程基础
来源:互联网 发布:linux命令行怎么打开 编辑:程序博客网 时间:2024/04/30 03:16
面向对象编程基础
定义类并创建实例
class Person(object): passxiaoming = Person()xiaohong = Person()print xiaomingprint xiaohongprint xiaoming == xiaohong
创建实例属性
class Person(object): passp1 = Person()p1.name = 'Bart'p2 = Person()p2.name = 'Adam'p3 = Person()p3.name = 'Lisa'L1 = [p1, p2, p3]L2 = sorted(L1,lambda x,y:cmp(x.name,y.name))print L2[0].nameprint L2[1].nameprint L2[2].name
初始化实例属性
class Person(object): def __init__(self,name,gender,birth,job): self.name = name self.gender = gender self.birth = birth self.job = jobxiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')print xiaoming.nameprint xiaoming.job
访问限制
class Person(object): def __init__(self, name, score): self.name = name self.__score = scorep = Person('Bob', 59)print p.nameprint p.__score
创建类属性
class Person(object): count = 0 def __init__(self,name): Person.count = Person.count + 1 self.name = name p1 = Person('Bob')print Person.countp2 = Person('Alice')print Person.countp3 = Person('Tim')print Person.count
定义实例方法
class Person(object): def __init__(self, name, score): self.name = name self.__score = score def get_grade(self): if(self.__score < 60): return "C" elif(self.__score >= 90): return "A" else: return "B" p1 = Person('Bob', 90)p2 = Person('Alice', 65)p3 = Person('Tim', 48)print p1.get_grade()print p2.get_grade()print p3.get_grade()
定义类方法
class Person(object): __count = 0 @classmethod def how_many(cls): return cls.__count def __init__(self,name): self.name = name Person.__count = Person.__count + 1print Person.how_many()p1 = Person('Bob')print Person.how_many()
0 0
- python面向对象编程基础
- Python面向对象编程基础
- Python面向对象编程基础
- python面向对象编程基础
- python 面向对象编程-基础
- Python面向对象编程基础
- python基础-----面向对象编程
- Python 面向对象编程(一)基础
- python基础学习笔记<面向对象编程>
- Python面向对象编程基础学习笔记
- python之面向对象编程基础
- Python基础之五面向对象编程
- python基础八_面向对象编程
- Python 之 基础面向对象编程
- Python基础学习--第十二篇(面向对象编程基础)
- Python基础进阶篇——面向对象编程基础
- Python编程基础之十二面向对象编程
- Python面向对象基础
- 数组应用实例(生成并打印随机数和统计随机数的分布)
- 《TCP/IP入门经典》——什么是TCP/IP
- 编写LXRT(RTAI-LXRT)用户空间程序详解
- LeetCode Count Complete Tree Nodes
- Java多线程<1>
- Python面向对象编程基础
- 进程的存储管理详细解释
- HDOJ 超级楼梯
- Android异步任务AsyncTask
- Android25闹钟项目——刷新时间
- 2015-06-07 开启自己的学习笔记
- 数据结构与算法分析-索引
- Percona ubuntu14 fresh install
- C#中易错的题和知识点