一、定义类、子类、类的实例化、子类的实例化、继承、实例属性和实例方法

示例:

class Fruit():
'''
定义一个水果类,包含水果的一些属性和一些方法。
'''
def __init__(self,name,color,shape,taste):
self.name = name
self.color = color
self.shape = shape
self.taste = taste
print(self.name+"的小时候是这样的,它的颜色是:"+self.color+",它的形状是:"+self.shape+",它的味道是:"+self.taste)
def colorChange(self,new_color):
self.color = new_color
print(self.name + "的颜色变成了:"+self.color)
def sizeChange(self,new_size):
self.shape = new_size
print(self.name + '的大小变成了:'+self.shape)
def tasteChange(self,new_taste):
self.taste = new_taste
print(self.name + '的味道变成了:'+self.taste)
def growUp(self):
print("慢慢的它长大了...") class waterFruit(Fruit):
'''
定义一个水分多的水果类,包含多水分的属性和一些方法。
'''
def __init__(self,name,color,shape,taste,water_pencent):
# Fruit.__init__(self,name,color,shape,taste)
self.name = name
self.color = color
self.shape = shape
self.taste = taste
self.water_pencent = water_pencent
print(self.name+"的小时候是这样的,它的颜色是:"+self.color+",它的形状是:"+self.shape+",它的味道是:"+self.taste+",它的水分是:"+self.water_pencent)
def waterChange(self,new_water):
self.water_pencent = new_water
print(self.name + "的水分变成了:" + self.water_pencent)
banana = Fruit('香蕉','绿色','长条形','微甜')
banana.growUp()
banana.colorChange('黄色')
banana.sizeChange('椭圆形')
banana.tasteChange('很甜')
watermelon = waterFruit('西瓜','绿色','圆形','甜的','90%')
watermelon.growUp()
watermelon.waterChange("95%")
watermelon.tasteChange("超级甜")

二、类属性

1、类的数据属性:它是静态的类属性,直接绑定在类上而不是某个实例上,在使用时通过类+"."操作符+属性来调用。如下例:

>>> class foo():
foo = 100
>>>
>>> print(foo.foo)
100
>>> foo.foo += 1
>>> print(foo.foo)
101
>>>

2、方法(也是类的属性):必须通过实例去调用,类不能直接调用。

>>> class foo():
foo = 100
def doNothing(self):
print('Do nothing!')
>>>
# 必须先实例化对象:
>>> fooAction = foo()
>>> fooAction.doNothing()
Do nothing!
# 直接用类调用方法时报错:
>>> foo.doNothing()
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
foo.doNothing()
TypeError: doNothing() missing 1 required positional argument: 'self'

3、查看类的属性:

# 1:通过内建函数dir()查看类的内部属性,返回的是一个属性列表
>>> dir(foo)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'doNothing', 'foo']
# 2:通过类的__dict__属性查看,返回的是一个字典,key是属性名,value是具体的值。
>>> foo.__dict__
mappingproxy({'foo': 100, 'doNothing': <function foo.doNothing at 0x0000029E039C7F28>, '__weakref__': <attribute '__weakref__' of 'foo' objects>, '__dict__': <attribute '__dict__' of 'foo' objects>, '__doc__': None, '__module__': '__main__'})
>>> print(foo.__dict__)
{'foo': 100, 'doNothing': <function foo.doNothing at 0x0000029E039C7F28>, '__weakref__': <attribute '__weakref__' of 'foo' objects>, '__dict__': <attribute '__dict__' of 'foo' objects>, '__doc__': None, '__module__': '__main__'}

4、一些类的特殊属性

# 类的名字:
>>> print(foo.__name__)
foo
# 类说明
>>> foo.__doc__
>>> print(foo.__doc__)
None
>>>
>>> class fooo(foo):
pass
# 类的所有父类构成的元组
>>> print(foo.__bases__)
(<class 'object'>,)
>>> print(fooo.__bases__)
(<class '__main__.foo'>,)
>>>
# 类属性的字典查看方法
>>> print(foo.__dict__)
{'foo': 100, 'doNothing': <function foo.doNothing at 0x0000029E039C7F28>, '__weakref__': <attribute '__weakref__' of 'foo' objects>, '__dict__': <attribute '__dict__' of 'foo' objects>, '__doc__': None, '__module__': '__main__'}
# 定义类foo所在的模块:
>>> print(foo.__module__)
__main__
>>>
# 实例foo1所对应的类:
>>> print(foo1.__class__)
<class '__main__.foo'>
>>>

初始Python类的更多相关文章

  1. python类的定义和使用

    python中类的声明使用关键词class,可以提供一个可选的父类或者说基类,如果没有合适的基类,那就用object作为基类. 定义格式: class 类名(object): "类的说明文档 ...

  2. python类:magic魔术方法

    http://blog.csdn.net/pipisorry/article/details/50708812 魔术方法是面向对象Python语言中的一切.它们是你可以自定义并添加"魔法&q ...

  3. python 类编程相关内容(更新)

    python作为面向对象的编程语言,类和对象相关的编程当然是少不了的! python类: class 类名 : 变量名 [ = 初始值 ] …… def 函数名 ( self [ , 其余参数列表 ] ...

  4. (转)python类:magic魔术方法

    原文:https://blog.csdn.net/pipisorry/article/details/50708812 版权声明:本文为博主皮皮http://blog.csdn.net/pipisor ...

  5. Python类中super()和__init__()的关系

    Python类中super()和__init__()的关系 1.单继承时super()和__init__()实现的功能是类似的 class Base(object): def __init__(sel ...

  6. python基础之初始python

    初始python之基础一 一.Python 介绍 1.python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发 ...

  7. LightMysql:为方便操作MySQL而封装的Python类

    原文链接:http://www.danfengcao.info/python/2015/12/26/lightweight-python-mysql-class.html mysqldb是Python ...

  8. python 类属性与方法

    Python 类属性与方法 标签(空格分隔): Python Python的访问限制 Python支持面向对象,其对属性的权限控制通过属性名来实现,如果一个属性有双下划线开头(__),该属性就无法被外 ...

  9. python 类以及单例模式

    python 也有面向对象的思想,则一切皆对象 python 中定义一个类: class student: count = 0         books = [] def __init__(self ...

随机推荐

  1. MySQL性能优化的最佳经验

    今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序员需要去关注的事情.当我们去设计数据库表结构,对操作数据 ...

  2. Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. Notice The minimu ...

  3. 对 Linux 新手非常有用的 20 个命令

    参考:http://www.oschina.net/translate/useful-linux-commands-for-newbies 英文原文:http://www.tecmint.com/us ...

  4. Scanner 和 String 类的常用方法

    Scanner类是在jdk1.5 之后有了这个: 常用格式是: Scanner sc = new Scanner(System.in); 从以下版本开始: 1.5 构造方法摘要 Scanner(Fil ...

  5. 2.16 最长递增子序列 LIS

    [本文链接] http://www.cnblogs.com/hellogiser/p/dp-of-LIS.html [分析] 思路一:设序列为A,对序列进行排序后得到B,那么A的最长递增子序列LIS就 ...

  6. Java for LeetCode 057 Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  7. Greedy:Protecting the Flowers(POJ 3262)

    保护花朵 题目大意:就是农夫有很多头牛在践踏花朵,这些牛每分钟破坏D朵花,农夫需要把这些牛一只一只运回去,这些牛各自离牛棚都有T的路程(有往返,而且往返的时候这只牛不会再破坏花),问怎么运才能使被践踏 ...

  8. Java数据类型和运算符

    一,数据类型分类(2种) 1. 基本数据类型(3种) 数值型: 整数类型(4种): byte(1字节):范围(-128~127): short(2字节):范围(-32768~32767): int(4 ...

  9. Android学习笔记——文件路径(/mnt/sdcard/...)、Uri(content://media/external/...)学习

    一.URI 通用资源标志符(Universal Resource Identifier, 简称"URI"). Uri代表要操作的数据,Android上可用的每种资源 - 图像.视频 ...

  10. C++C++中构造函数与析构函数的调用顺序

    http://blog.csdn.net/xw13106209/article/details/6899370 1.参考文献 参考1: C++继承中构造函数.析构函数调用顺序及虚函数的动态绑定 参考2 ...