Python学习
来源:互联网 发布:网络主页版式规范 编辑:程序博客网 时间:2024/05/24 04:16
学学Python,先看看类:
_metaclass_=typeclass Person: name=raw_input('input name:') age=raw_input('input age: ') def _init_(self,Name,Age): self.name=Name self.age=Age def main(self): print(self.name) print(self.age) p=Person()#类实例化p.main() #调用方法
定义了鸟类的一个方法属性,就是表示移动的方法move。它的参数中有一个self,它是为了方便我们引用对象自身。方法的第一个参数必须是self,无论是否用到。有关self的内容会在下一讲展开)
另外两个参数,dx, dy表示在x、y两个方向移动的距离。move方法会最终返回运算过的position。
在最后调用move方法的时候,我们只传递了dx和dy两个参数,不需要传递self参数(因为self只是为了内部使用)。
_metaclass_=typeclass Bird(object): '\'this is python\' ' have_feather = True way_of_reproduction = 'egg' def move(self, dx, dy): position = [0,0] position[0] = position[0] + dx position[1] = position[1] + dy return position def showClassName(self): print 'class\' name:',self.__class__.__name__ def showClassDoc(self): print self.__class__.__doc__mybird = Bird()print 'mybird\' posion is :',mybird.move(1,2)mybird.showClassName()mybird.showClassDoc()
继承:
_metaclass_=typeclass Bird(object): 'inheritance of python ' have_feather=True way_of_reproduction='egg' def move(self,dx,dy): posion=[0,0] posion[0]=posion[0]+dx posion[1]=posion[1]+dy return posionclass Chicken(Bird): way_of_move = 'walk' possible_in_KFC = Trueclass Duck(Bird): way_of_move = 'fly' possible_in_KFC = Falsemybird=Chicken()print mybird.have_featherprint 'mybird\' posion is :',mybird.move(1,2)print mybird.way_of_moveprint mybird.possible_in_KFCmyDuck=Duck()print myDuck.possible_in_KFC
_metaclass_=type class people: #定义基本属性 name = '' age = 0 #定义私有属性,私有属性在类外部无法直接进行访问 __weight = 0 #定义构造方法 def __init__(self,n,a,w): self.name = n self.age = a self.__weight = w def speak(self): print"%s is speaking: I am %d years old" %(self.name,self.age) print 'he\'weight is:%d' %self.__weightclass student(people):###单继承 height=0 def __init__(self,n,a,w,g): #调用父类的构造函数 people.__init__(self,n,a,w) self.height=g #父类的方法 def speak(self): print"%s\'s age is:%d,and height is:%d" %(self.name,self.age,self.height) #print 'he\'s weight is:%d' %self.__weight #错误,私有属性不能继承#p = people('tom',10,30) #p.speak() #print p.__weight #私有属性,不能直接访问#s=student('bob',25,160,180)#print s.speak()class major():#定义一个新的类 def __init__(self,n,c,s): self.name=n self.cource=c self.score=s def majors(self): print '%s\'cource is %s,and he\'s score is %d' %(self.name,self.cource,self.score)#多重继承class sample(major,student): def __init__(self,n,a,w,g,c,s): student.__init__(self,n,a,w,g) major.__init__(self,n,c,s)test=sample('wang',25,160,180,'math',1000)test.majors()#方法名同,默认调用的是在括号中排前地父类的方法 test.speak()
2.探究self是什么
我们重新在定义方法时,必须有self这一参数。这个参数表示某个对象。对象拥有类的所有性质,那么我们可以通过self,调用类属性。
好像是自己调用自己吧!
_metaclass_=typeclass Human(object): laugh = 'hello' def show_laugh(self): print self.laugh def laugh_10th(self): for i in range(10): self.show_laugh() AA= Human() AA.laugh_10th()
__init__()是一个特殊方法(special method)。Python有一些特殊方法。Python会特殊的对待它们。特殊方法的特点是名字前后有两个下划线。
如果你在类中定义了__init__()这个方法,创建对象时,Python会自动调用这个方法。这个过程也叫初始化。
class Bird(object): 'inheritance of python ' have_feather=True way_of_reproduction='egg' def move(self,dx,dy): posion=[0,0] posion[0]=posion[0]+dx posion[1]=posion[1]+dy return posionclass happyBird(Bird): def __init__(self,more_words): print 'We are happy birds.',more_wordssummer = happyBird('Happy,Happy!')#'Happy,Happy!' 被传递给了__init__()的参数more_words尽管我们只是创建了summer对象,但__init__()方法被自动调用了。最后一行的语句(summer = happyBird...)先创建了对象.
在初始化中,将参数input_gender,赋值给对象的性质,即self.gender。
li_lei拥有了对象性质gender。gender不是一个类属性。Python在建立了li_lei这一对象之后,使用li_lei.gender这一对象性质,专门储存属于对象li_lei的特有信息。
对象的性质也可以被其它方法调用,调用方法与类属性的调用相似,正如在printGender()方法中的调用。
摘自:
http://www.jb51.net/article/54480.htm
http://blog.csdn.net/wklken/article/details/6313265
阅读全文
0 0
- 【Python学习】Python 关键字
- Python学习----Python基础
- 【python】:python学习笔记
- Python:Python学习总结
- Python学习-Python数据类型
- Python学习
- 学习python
- python学习
- 学习Python
- python学习
- python学习
- python学习
- python 学习
- 学习python
- Python学习
- Python 学习
- python学习
- Python学习
- Shiro入门—授权
- Linux学习笔记:权限管理
- 全栈工程师之路-中级篇之小程序开发-第二章第三节小程序豆瓣评分主页
- Spring+hibernate 配置文件
- 23个MySQL常用查询语句
- Python学习
- C++栈的基本操作和应用
- BaseService
- opencv3模块
- mosquitto源码分析
- Android 内存泄漏总结
- Python时间戳与时间字符串互相转换
- Android ListView Adapter notifyDataSetChanged 刷新没有效果
- 【WEB】实战之html与css扁平化风格博客