“介面實作計算矩形面積”,想知道這個代碼怎么改(要求輸出圖1,我的是圖2)
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 GetArea(); }
class Test:IShape
{
public int chang{get;set;}
public int kuan{ get; set;}
public int Length
{
get{return Length;}
set{Length=value;}
}
public int Width
{
get{return Width;}
set{Width=value;}
}
public void GetArea()
{
Console.WriteLine("矩形的面積:{0}",chang*kuan);
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("請輸入矩形的寬:");
var widthStr = Console.ReadLine(); Console.WriteLine("請輸入矩形的長:");
var lengthStr = Console.ReadLine();
Test getArea = new Test(); Console.WriteLine("矩形的面積:{0}",(getArea)); Console.ReadLine(); Convert.ToInt32(Console.ReadLine()); }
}
}
}

uj5u.com熱心網友回復:
Test getArea = new Test(){chang=int.Parse(lengthStr),kuan=int.Parse(widthStr)}};getArea .GetArea();
uj5u.com熱心網友回復:
這個不行啊,getArea報錯
uj5u.com熱心網友回復:
你的GetArea方法需要一個回傳值,不能是 void 型別的。比如:
interface IShape
{
int Length{get;set;}
int Width{get;set;}
int GetArea(); }
。。。
public int GetArea()
{
//Console.WriteLine("矩形的面積:{0}",chang*kuan);
return chang*kuan;
}
Console.WriteLine("矩形的面積:{0}",。。。
這里的{0}不能傳一個 void 型別的方法,需要一個有回傳型別和值的方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/39829.html
標籤:C#
上一篇:不會寫代碼
下一篇:c#介面實作矩形面積的計算
