YTU OJ 3485 问题 D: 接口实例(C#,IShape)
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());
}
}
}
double A;
double B;
public Rectangle(double a,double b)
{
if (a > 0 && b > 0)
{
this.A = a;
this.B = b;
}
else
{
this.A = 0;
this.B = 0;
}
}
public double Area()
{
return A * B;
}
public double Perimeter()
{
return (2 * A + 2 * B);
}