【发布时间】:2018-12-15 12:06:39
【问题描述】:
我开始学习如何创建 C# 以及使用接口和类。 有人可以教我如何将我的类和我的界面分开,并且仍然保持它们之间的连接吗?
原因:如果我要更新我的代码,我会知道在哪里添加它们,并且我的屏幕上的代码会更少。
namespace Car
{
class MainClass
{
//My Interface
interface ICar
{
int gas { get; set; }
//void refuel();
int getGasLeft();
}
//My Class
class Car : ICar
{
public int gas { get; set; }
public Car(string _name)
{
name = _name;
dist = 0;
gas = 40;
}
public void refuel(int lit)
{
gas += lit;
}
public int getGasLeft()
{
return gas;
}
}
//Main
public static void Main(string[] args)
{
Car toyota = new Car();
toyota.drive(100);
toyota.refuel(5);
Console.WriteLine("Name: " + toyota.name +
"\nGas Left: " + toyota.getGasLeft());
Console.ReadLine();
}
}
}
【问题讨论】: