码迷,mamicode.com
首页 > 其他好文 > 详细

第4章 面向对象编程

时间:2017-08-17 16:27:53      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:show   面向   square   complex   get   面向对象   highlight   pre   lex   

实验一:复数类complex

class complex:
    def __init__(self,real_num,imaginary_num):
        self.__real_num = real_num
        self.__imaginary_num = imaginary_num
    def add(self,c2):
        print(‘结果为:\n实数:‘,self.__real_num+c2.__real_num,‘虚数:‘,self.__imaginary_num+c2.__imaginary_num)
    def show(self):
        print(‘实数:‘,self.__real_num,‘虚数:‘,self.__imaginary_num)

c1 = complex(4,-1)
c2 = complex(2,3)
c1.show()
c2.show()
c1.add(c2)

 实验二:抽象基类shape

import math

class shape(object):
    pass

class rectangle(shape):
    def __init__(self,length, width):
        self.__length = length
        self.__width = width
    def getCircumference(self):
        circumference = 2*(self.__length+self.__width)
        print(circumference)
    def getArea(self):
        area = self.__length*self.__width
        print(area)

class square(rectangle):
    def __init__(self, side, ):
        self.__side = side
    def getCircumference(self):
        circumference = 4*self.__side
        print(circumference)
    def getArea(self):
        area = self.__side*self.__side
        print(area)

class circle(shape):
    def __init__(self,diameter):
        self.__diameter = diameter
    def getCircumference(self):
        circumference = math.pi*self.__diameter
        print(circumference)
    def getArea(self):
        area = 0.25*math.pi*self.__diameter
        print(area)

a = rectangle(7,8)
a.getCircumference()
a.getArea()
b = square(4)
b.getCircumference()
b.getArea()
c = circle(5)
c.getCircumference()
c.getArea()

第4章 面向对象编程

标签:show   面向   square   complex   get   面向对象   highlight   pre   lex   

原文地址:http://www.cnblogs.com/Nick-Peggy/p/7382245.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!