首页 > 编程语言 > 详细

Python基础课:类的特殊方法

时间:2017-07-01 09:39:04      阅读:256      评论:0      收藏:0      [点我收藏+]
class Rectangle:
    这是一个矩形类
    def __init__(self,length,width):
        if isinstance(length,(int,float)) and isinstance(width,(int,float)):
            self.length = length
            self.width  = width
        else:
            raise TypeError
            #return None
    def area(self):
        areas = self.length * self.width
        return areas
 
    def __str__(self):
        return 宽为%s,高为%s%(self.width,self.length)
    def __repr__(self):
        return 面积为:%s%(self.area())
    def __call__(self):
        return 这是一个call方法
    def __add__(self,other):
        add_length = self.length + other.length
        add_width = self.width + other.width
        return add_length,add_width
 
b = Rectangle(3,5)
b.__dict__
b.__doc__
 
#print(b)
b
 
c = Rectangle(4,6)

 

Python基础课:类的特殊方法

原文:http://www.cnblogs.com/yuebei/p/7101192.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!