Day18作业及默写

图形的面积与周长

#!/usr/bin/env python
# encoding: utf-8
# Author: MeiMeiLong <[email protected]>
# Create Date: 2019-03-25 12:03:57
# Last Modified: 2019-03-25 16:11:13
# Description:
'''
1、长方形的面积=长×宽 ,正方形的面积=边长×边长
2、三角形的面积=底×高÷2
3、圆的面积=圆周率×半径×半径
'''
from math import pi
class Rectangle:
    def __init__(self,length,width):
        self.length = length
        self.width = width
    def area(self):
        return self.length * self.width
    def girth(self):
        return ( self.length + self.width ) * 2
class Triangle:
    def __init__(self,base,height):
        self.base = base
        self.height = height
    def area(self):
        return self.base * self.height / 2
class Circular:
    def __init__(self,radius):
        self.radius = radius
    def area(self):
        return self.radius ** 2 * 3.14
    def girth(self):
        return self.radius * pi * 2

Rec = Rectangle(10,20)
print(f'图形为{Rec.__class__.__name__},长为{Rec.length},宽为{Rec.width},面积为{Rec.area()},周长为{Rec.girth()}')
Tri = Triangle(10,20)
print(f'图形为{Tri.__class__.__name__},底为{Tri.base},高为{Tri.height},面积为{Tri.area()}')
Cir = Circular(10)
print(f'图形为{Cir.__class__.__name__},半径为{Cir.radius},面积为{Cir.area()},周长为{Cir.girth()}')

猜你喜欢

转载自www.cnblogs.com/meilong/p/Day18zuo-ye-ji-mo-xie.html