python学习类方法以及重载

#-*- encoding: utf-8 -*-
'''
Created on 2013-1-7

@author: 
'''
#定义类
class Point:
    pass
#实例
pt1= Point()
#为类添加属性
pt1.x=1;
pt1.y=2;
print pt1,type(pt1),pt1.x #<__main__.Point instance at 0x01BDE030> <type 'instance'> 1

def printPoint(pt):
    print "(" + str(pt.x) + ","+ str(pt.y) + ")"
printPoint(pt1)   

class Rectange:
    pass
rec = Rectange();
rec.width = 20;
rec.high =10 
#定义矩形的左下角为一个点
rec.cor= Point();
rec.cor.x = 0;
rec.cor.y = 0;
#copy与deepcopy
import copy
pt2 = copy.copy(pt1)
print pt1,pt2,pt1.x,pt1.y,pt2.x,pt2.y #1 2 1 2
print id(pt1),id(pt2)
pt3 = copy.deepcopy(pt1)
print pt1,pt3,pt1.x,pt1.y,pt3.x,pt3.y #1 2 1 2
print id(pt1),id(pt3)
#类的方法
#定义在类的内部
class time:
    #构造函数
    def __init__(self,hour=0,minutes=0,seconds=0):
        self.hour = hour
        self.minutes= minutes
        self.seconds = seconds
    #函数的第一个参数是self,如果没有第二个参数,具体就用self.hour
    def printTime(self,t):
        print str(t.hour) + ":" + \
            str(t.minutes) + ":" + \
            str(t.seconds)
    #定义增加秒数的函数,并设置缺省参数为30
    def increseconds(self,sec=30):
        self.seconds += sec
        if (self.seconds>60):
            self.seconds = self.seconds -60
            self.minutes += 1
        if(self.minutes>60):
                self.minutes = self.minutes -60
                self.hour += 1
                    
          
t1 = time()   
t1.hour=10
t1.minutes=8
t1.seconds=50

#第一个参数self就是类的对象t1
t1.printTime(t1) #10:8:50
t1.increseconds(20);
t1.printTime(t1) #10:9:10

#有的参数之所以可以省略,是因为函数中已经给出了缺省的参数
t1.increseconds();
t1.increseconds();
t1.printTime(t1) #10:10:10

#构造函数是任何类都有的特殊方法。当要创建一个类时,就调用构造函数。它的名字是: __init__ 。
t2 = time(10,44,45)
t2.printTime(t2)

class Num:
    def __init__(self,num=0):
        self.num = num
    def __str__(self):
      return   str(self.num)    
   #加法重载
    def __add__(self,other):
        return Num(self.num+other.num)   
    #减法重载
    def __sub__(self,other):
        return Num(self.num-other.num) 
    def __print__(self):
        print "Num:" + self.__str__()
a1 = Num(50)
a2 = Num(40)
a3 = a1-a2
a4 = a1 + a2
print a3,a4

#乘法重定义有2个
#如果乘法操作符“*”的左右操作数都是用户定义的数据类型,那么
#调用 __mul__ 。若左边的操作数是原始数据类型,而右边是用户定义数据类
#型,则调用 __rmul__ 。
class Line:
    def __init__(self,length):
        self.length = length
    def __str__(self):
        return self.length
    #乘法重定义1,两个参数都是用户自定义数据类型,返回一个矩形
    def __mul__(self,other):
        return Rect(self.length,other.length) 
    #乘法重定义2,左边是原始数据类型,右边是用户自定义数据类型
    def __rmul__(self,other):
        return Line(self.length*other)
    def printline(self):
        print self.length
    
class  Rect:
    def __init__(self,width,heigh):
        self.width = width
        self.heigh = heigh 
    def printrect(self):
        print self.width ,self.heigh    
 
line1 = Line(20)
#调用乘法重载2,左边是原始类型,右边是用户自定义类型
line2 =3*line1 
#交换调用顺序会出错
#lint3 = lin1*4 #it is error
line2.printline()  
#调用line的乘法重载1
rec1 = line1 * line2
rec1.printrect() #20,60
#类的继承
class Person:
    def __init__(self,name,age,sex):
        self.name=name
        self.age= age
        self.sex=sex
    def disPlay(self):
        print "name   :      %-20s" % self.name
        print "age    :      %-20d" % self.age
        print "sex    :      %-20s" % self.sex
#Student继承Person        
class Studen(Person):
    def __init__(self,name,age,sex,gardon):
        Person.__init__(self,name, age, sex)
        self.gardon = gardon
    def disPlay(self):
        Person.disPlay(self)
        print "gardon :      %-20s" % self.gardon
        #私有方法只能在内部调用
        self.__output()
#在Python 中,类的私有方法和私有属性,不能够从类的外面调用。类
#的方法和属性是公有,还是私有,可以从它的名字判断。如果名字是以两
#个下划线开始,但并不是以两个下划线结束,则是私有的。其余的都是公
#有的
    def __output(self):
        print "testing"        
 
stu1 = Studen("zhangsan",30,"man","yanyi") 
stu1.disPlay()  
#stu1.output()    #私有方法不能在外部调用 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值