Python学习

本文介绍了Python学习中的类和self概念。类定义了一个名为move的方法,用于表示移动,方法内self参数引用对象自身。self在方法调用时不必显式传递。此外,文章探讨了self的作用,通过self调用类属性,并提到了__init__特殊方法在对象初始化中的作用。
摘要由CSDN通过智能技术生成

学学Python,先看看类:

_metaclass_=type
class 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_=type
class 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_=type
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 posion
class Chicken(Bird):
    way_of_move = 'walk'
    possible_in_KFC = True

class Duck(Bird):
    way_of_move = 'fly'
    possible_in_KFC = False

mybird=Chicken()
print mybird.have_feather
print 'mybird\' posion is :',mybird.move(1,2)
print mybird.way_of_move
print mybird.possible_in_KFC
myDuck=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.__weight
class 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_=type
class 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()


3.__init__()方法

__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 posion
class happyBird(Bird):
    def __init__(self,more_words):
        print 'We are happy birds.',more_words
summer = 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值