OJ Problem 3485 接口实例(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.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;
namespace ConsoleApplication1
{
    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());
        }
    }
} 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值