zoukankan      html  css  js  c++  java
  • 问题 D: 接口实例(C#,IShape)

    题目描述

    接口实例。接口和类如下图所示,根据给出代码,补写缺失的代码,然后在Program类的静态Main方法中验证所实现的类。

    using System;
    namespace Myinterface
    {
        public interface IShape
        {
            double Perimeter();
            double Area();
        }
        class Circle : IShape
        {
            public double Radius { get; set; }
            public Circle(double r)
            {
                Radius = r;
            }
            public double Area()
            {
                return Math.PI * Radius * Radius;
            }
            public double Perimeter()
            {
                return 2 * Math.PI * Radius;
            }
        }
        class Rectangle : IShape
        {
                /////////////////////////////////////////////////////////////////
                
                //请填写代码,实现输出矩形的面积和周长

                /////////////////////////////////////////////////////////////////
            
        }

        class Program
        {
            static void Main(string[] args)
            {
                double w, h;
                double.TryParse(Console.ReadLine(), out w);
                double.TryParse(Console.ReadLine(), out h);
                Rectangle r = new Rectangle(w, h);
                Console.WriteLine("area={0},Perimeter={1}",r.Area(), r.Perimeter());
            }
        }
    }

    输入

    输入矩形长、高,如
    10
    3

    输出

    area=30,Perimeter=26

    样例输入

    10
    3

    样例输出

    area=30,Perimeter=26

    提示

    需要考虑输入非数字、负数等

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace 接口实例
    {
        public interface IShape
        {
            double Perimeter();
            double Area();
        }
        class Circle : IShape
        {
            public double Radius { get; set; }
            public Circle(double r)
            {
                Radius = r;
            }
            public double Area()
            {
                return Math.PI * Radius * Radius;
            }
            public double Perimeter()
            {
                return 2 * Math.PI * Radius;
            }
        }
        class Rectangle : IShape
        {
            public double Height { get; set; }
            public double Length { get; set; }
            public Rectangle(double l, double h)
            {
                Height = h;
                Length = l;
            }
            public double Area()
            {
                if (Height <= 0 || Length <= 0)
                {
                    return 0;
                }
                return Height * Length;
            }
            public double Perimeter()
            {
                if (Height <= 0 || Length <= 0)
                {
                    return 0;
                }
                return (Height + Length) * 2;
            }
    
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                double w, h;
                double.TryParse(Console.ReadLine(), out w);
                double.TryParse(Console.ReadLine(), out h);
                Rectangle r = new Rectangle(w, h);
                Console.WriteLine("area={0},Perimeter={1}", r.Area(), r.Perimeter());
            }
        }
    }
    

      

  • 相关阅读:
    C++ 使用老牌库xzip & unzip对文件进行压缩解压
    第一次玩蛇,有点紧张。
    fiddler 抓取手机http/https包
    disk或者Partition镜像的制作
    VS2013+phread.h环境配置
    C++ 浅谈 strlen 与 sizeof的区别
    Qt 显示网页的控件
    Qt error: C2236: 意外的标记“class”。是否忘记了“;”?
    初识MySQL——人生若如初相逢
    【学习笔记】HTML基础:列表、表格与媒体元素
  • 原文地址:https://www.cnblogs.com/mjn1/p/12619083.html
Copyright © 2011-2022 走看看