zoukankan      html  css  js  c++  java
  • .NET程序设计——面向对象程序设计02

    一、任务

     编写一个控制台应用程序,输入正方形边长或者半径,计算其周长和面积并输出。

    (1) 编写两个接口,接口 IShape 包含三个方法:initialize, getPerimeter, getArea。分别进行初始化、获取边长和面积,其返回值均为 decimal。接口 IDisplayresult 显示计算结果。 

    (2) 编写两个类,Square(正方形)和 Circle(圆形),实现 IShape 和 IDisplayresult接口。 

    二、代码

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 
     5 namespace Tutorial2_2
     6 {
     7     public interface Ishape
     8     {
     9         void Initialize(); //初始化
    10         decimal GetPerimeter();//获取边长
    11         decimal GetArea();//获取面积
    12     }
    13 }
    Ishape.CS
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 
     5 namespace Tutorial2_2
     6 {
     7     public interface IDisplayresult
     8     {
     9         void Show();
    10     }
    11 }
    IDisplayresult.CS
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 
     5 namespace Tutorial2_2
     6 {
     7     class Square: Ishape, IDisplayresult
     8     {
     9         public decimal sidelength;//边长
    10        
    11         public void Initialize()
    12         {
    13             Console.WriteLine("请输入正方形边长:");
    14             do
    15             {
    16                 sidelength = decimal.Parse(Console.ReadLine());
    17                 if (sidelength <= 0)
    18                 {
    19                     Console.WriteLine("输入数据错误,请重新输入:");
    20                 }
    21             }
    22             while (sidelength <= 0);
    23         }
    24 
    25         public decimal GetArea()
    26         {
    27             return sidelength * sidelength;
    28         }
    29         public decimal GetPerimeter()
    30         {
    31             return 4 * sidelength;
    32         }      
    33 
    34         public void Show()
    35         {
    36             Console.WriteLine("正方形周长为:{0}", GetPerimeter());
    37             Console.WriteLine("正方形面积为:{0}", GetArea());
    38         }
    39     }
    40 }
    Square.CS
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 
     5 namespace Tutorial2_2
     6 {
     7     class Circle : Ishape, IDisplayresult
     8     {
     9         decimal radius;
    10         const decimal pai = 3.14M;   //常量pi
    11 
    12         public decimal GetArea()   //获得面积
    13         {
    14             return pai * radius * radius;
    15         }
    16 
    17         public decimal GetPerimeter()  //获得周长
    18         {
    19             return 2 * pai * radius;//圆形周长计算
    20         }
    21 
    22         public void Initialize()  //初始化
    23         {
    24             Console.WriteLine("请输入圆形半径:");
    25             do
    26             {
    27                 radius = decimal.Parse(Console.ReadLine());
    28                 if (radius <= 0)
    29                 {
    30                     Console.WriteLine("输入数据错误,请重新输入:");
    31                 }
    32             }
    33             while (radius <= 0);
    34         }
    35 
    36         public void Show()
    37         {
    38             Console.WriteLine("圆形周长为:{0}", GetPerimeter());
    39 
    40             Console.WriteLine("圆形面积为:{0}", GetArea());
    41         }
    42     }
    43 }
    Circle.CS

    三、结果截图

  • 相关阅读:
    沙雕玩意儿
    1558:聚会 ybt
    沙雕关于线段树的一点总结(滑稽)
    卑微
    沙雕题目 来自luogu
    甜茶好帅啊
    python 中字符串的格式化
    python的几个小程序
    python 第一课
    基于笔画宽度变换的自然场景文本检测方法
  • 原文地址:https://www.cnblogs.com/Lizhichengweidashen/p/14903676.html
Copyright © 2011-2022 走看看