I want to build a nice API (C#) to make it easier for people to consume, I think I've seen this before and want to know how to do this:
我想建立一个很好的API(C#),让人们更容易消费,我想我以前见过这个并想知道如何做到这一点:
MyNamespace.Cars car = null;
if(someTestCondition)
car = new Honda();
else
car = new Toyota();
car.Drive(40);
Is this possible? If so, what needs to be done?
这可能吗?如果是这样,需要做什么?
7 个解决方案
#1
Interface Car
{
void Drive(int miles);
}
class Honda : Car
{
...
}
class Toyota : Car
{
...
}
#2
You could do this a couple of different ways. You could declare an abstract base class or you could have an interface that your object implement. I believe the "C#" preferred method would be to have an interface. Something like:
你可以用几种不同的方式做到这一点。您可以声明一个抽象基类,也可以使用对象实现的接口。我相信“C#”首选方法是拥有一个接口。就像是:
public interface ICar
{
public Color Color { get; set; }
void Drive(int speed);
void Stop();
}
public class Honda : ICar
{
#region ICar Members
public Color Color { get; set; }
public void Drive(int speed)
{
throw new NotImplementedException();
}
public void Stop()
{
throw new NotImplementedException();
}
#endregion
}
public class Toyota : ICar
{
#region ICar Members
public Color Color { get; set; }
public void Drive(int speed)
{
throw new NotImplementedException();
}
public void Stop()
{
throw new NotImplementedException();
}
#endregion
}
#3
I see everyone is pushing interface / abstract base class changes to you. The pseudocode you provided more or less implies you already have this in place.
我看到每个人都在推动界面/抽象基类更改。您提供的伪代码或多或少暗示您已经有了这个。
I'll pose something else:
我会做出别的东西:
You'll want to create a "CarFactory" that will return a specific implementation of your base class / interface. The Create method can take your test conditions as parameters so you create the correct car.
您将需要创建一个“CarFactory”,它将返回基类/接口的特定实现。 Create方法可以将您的测试条件作为参数,以便您创建正确的汽车。
EDIT: Here's a link from MSDN -- http://msdn.microsoft.com/en-us/library/ms954600.aspx
编辑:这是MSDN的链接 - http://msdn.microsoft.com/en-us/library/ms954600.aspx
EDIT: See the comments for another link.
编辑:请参阅其他链接的评论。
#4
Make a class named Cars. Give it the Drive method. Extend that base class in your Honda and Toyota classes.
创建一个名为Cars的课程。给它Drive方法。扩展本田和丰田课程中的基础课程。
#5
namespace MyNameSpace
{
public interface Cars
{
public void Drive(int miles);
}
public class Honda : Cars
{
public void Drive(int miles) { ... }
}
public class Toyota : Cars
{
public void Drive(int miles) { ... }
}
}
#6
Don't forget the namespace, also see my comment in the question about variable names
不要忘记命名空间,也请看关于变量名称的问题中的评论
namespace MyNamespace {
public interface Cars {
void Drive(int speed);
}
public class Honda : Cars {
public void Drive(int speed) {
}
}
public class Toyota : Cars {
public void Drive(int speed) {
}
}
}
#7
Make an abstract class called Cars with an abstract method called Drive(). Subclass it and add implementations.
使用名为Drive()的抽象方法创建一个名为Cars的抽象类。对它进行子类化并添加实现。
#1
Interface Car
{
void Drive(int miles);
}
class Honda : Car
{
...
}
class Toyota : Car
{
...
}
#2
You could do this a couple of different ways. You could declare an abstract base class or you could have an interface that your object implement. I believe the "C#" preferred method would be to have an interface. Something like:
你可以用几种不同的方式做到这一点。您可以声明一个抽象基类,也可以使用对象实现的接口。我相信“C#”首选方法是拥有一个接口。就像是:
public interface ICar
{
public Color Color { get; set; }
void Drive(int speed);
void Stop();
}
public class Honda : ICar
{
#region ICar Members
public Color Color { get; set; }
public void Drive(int speed)
{
throw new NotImplementedException();
}
public void Stop()
{
throw new NotImplementedException();
}
#endregion
}
public class Toyota : ICar
{
#region ICar Members
public Color Color { get; set; }
public void Drive(int speed)
{
throw new NotImplementedException();
}
public void Stop()
{
throw new NotImplementedException();
}
#endregion
}
#3
I see everyone is pushing interface / abstract base class changes to you. The pseudocode you provided more or less implies you already have this in place.
我看到每个人都在推动界面/抽象基类更改。您提供的伪代码或多或少暗示您已经有了这个。
I'll pose something else:
我会做出别的东西:
You'll want to create a "CarFactory" that will return a specific implementation of your base class / interface. The Create method can take your test conditions as parameters so you create the correct car.
您将需要创建一个“CarFactory”,它将返回基类/接口的特定实现。 Create方法可以将您的测试条件作为参数,以便您创建正确的汽车。
EDIT: Here's a link from MSDN -- http://msdn.microsoft.com/en-us/library/ms954600.aspx
编辑:这是MSDN的链接 - http://msdn.microsoft.com/en-us/library/ms954600.aspx
EDIT: See the comments for another link.
编辑:请参阅其他链接的评论。
#4
Make a class named Cars. Give it the Drive method. Extend that base class in your Honda and Toyota classes.
创建一个名为Cars的课程。给它Drive方法。扩展本田和丰田课程中的基础课程。
#5
namespace MyNameSpace
{
public interface Cars
{
public void Drive(int miles);
}
public class Honda : Cars
{
public void Drive(int miles) { ... }
}
public class Toyota : Cars
{
public void Drive(int miles) { ... }
}
}
#6
Don't forget the namespace, also see my comment in the question about variable names
不要忘记命名空间,也请看关于变量名称的问题中的评论
namespace MyNamespace {
public interface Cars {
void Drive(int speed);
}
public class Honda : Cars {
public void Drive(int speed) {
}
}
public class Toyota : Cars {
public void Drive(int speed) {
}
}
}
#7
Make an abstract class called Cars with an abstract method called Drive(). Subclass it and add implementations.
使用名为Drive()的抽象方法创建一个名为Cars的抽象类。对它进行子类化并添加实现。