c#,介面實作面積計算(隱形式轉換),要求鍵盤輸入長和寬,得出面積
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 介面的實作
{
interface IShape
{
int Length{get;set;}
int Width{get;set;}
void Area();
}
class Test: IShape
{
public int Chang = 0;
public int Kuan = 0;
public int Length
{
get {return Length;}
set {Length = value; }
}
public int Width
{
get { return Width; }
set { Width = value; }
}
public void Area()
{ var Area = Length * Width; Console.WriteLine(Area);
}
}
class Program
{
static void Main(string [] args)
{
Test cm = new Test();
IShape x = cm;
Console.WriteLine("請輸入矩形的寬:"); cm.Kuan= Convert.ToInt32(Console.ReadLine()); Console.WriteLine("請輸入矩形的長:"); cm.Chang= Convert.ToInt32(Console.ReadLine()); Console.WriteLine("矩形的面積為:"); Console.WriteLine(x.GetArea()); Console.ReadLine();
}
}
}
uj5u.com熱心網友回復:
IShape類里面沒有定義GetArea的方法,你看看你那個x的類里面的定義uj5u.com熱心網友回復:
你的IShape介面里定義的是Area()方法,不是GetArea()方法。改一下就好了而且Console.WriteLine(x.GetArea()); 這句代碼也不對。
Area()方法不回傳任何資料,你直接寫x.Area()就行
uj5u.com熱心網友回復:
IShape介面最好重新設計一下,面積和周長應該是一個幾何圖形的固有屬性,而且是只讀的。using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 獲取長度和寬度
Console.WriteLine("請輸入長度:");
double length = double.Parse(Console.ReadLine());
Console.WriteLine("請輸入寬度:");
double width = double.Parse(Console.ReadLine());
// 計算面積和周長
IShape rectangle = new Rectangle(length, width);
Console.WriteLine("矩形面積:{0}", rectangle.Area);
Console.WriteLine("矩形周長:{0}", rectangle.Perimeter);
// 獲取半徑
Console.WriteLine("請輸入半徑:");
double radius = double.Parse(Console.ReadLine());
// 計算面積和周長
IShape circle = new Circle(radius);
Console.WriteLine("圓面積:{0}", circle.Area);
Console.WriteLine("圓周長:{0}", circle.Perimeter);
Console.ReadKey();
}
}
public interface IShape
{
/// <summary>
/// 面積
/// </summary>
double Area { get; }
/// <summary>
/// 周長
/// </summary>
double Perimeter { get; }
}
public class Rectangle : IShape
{
/// <summary>
/// 長度
/// </summary>
private double length;
public double Length
{
get { return length; }
set { length = value; }
}
/// <summary>
/// 寬度
/// </summary>
private double width;
public double Width
{
get { return width; }
set { width = value; }
}
/// <summary>
/// 面積
/// </summary>
public double Area
{
get { return length * width; }
}
/// <summary>
/// 周長
/// </summary>
public double Perimeter
{
get { return 2 * (length + width); }
}
/// <summary>
/// 建構式
/// </summary>
/// <param name="length"></param>
/// <param name="width"></param>
public Rectangle(double length, double width)
{
this.length = length;
this.width = width;
}
}
public class Circle : IShape
{
/// <summary>
/// 半徑
/// </summary>
private double radius;
public double Radius
{
get { return radius; }
set { radius = value; }
}
/// <summary>
/// 面積
/// </summary>
public double Area
{
get { return Math.PI * radius * radius; }
}
/// <summary>
/// 周長
/// </summary>
public double Perimeter
{
get { return 2 * Math.PI * radius; }
}
/// <summary>
/// 建構式
/// </summary>
/// <param name="radius"></param>
public Circle(double radius)
{
this.radius = radius;
}
}
}
uj5u.com熱心網友回復:
Console.WriteLine(x.GetArea()); 這里 改成 Console.WriteLine(x.Area);uj5u.com熱心網友回復:
可以改的地方挺多的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 介面的實作
{
interface IShape
{
int Length { get; set; }
int Width { get; set; }
int Area();
}
class Test : IShape
{
private int Chang = 0;
private int Kuan = 0;
public int Length
{
get { return Chang; }
set { Chang = value; }
}
public int Width
{
get { return Kuan; }
set { Kuan = value; }
}
public int Area()
{
var Area = Chang * Kuan;
return Area;
}
}
class Program
{
static void Main(string[] args)
{
Test cm = new Test();
IShape x = cm;
Console.WriteLine("請輸入矩形的寬:");
cm.Width = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請輸入矩形的長:");
cm.Length = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("矩形的面積為:");
Console.WriteLine(x.Area());
Console.ReadLine();
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/38106.html
標籤:C#
