关于接口的新手问题 [英] Newbie question about interfaces

查看:58
本文介绍了关于接口的新手问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然是使用接口的新手,我不确定我是否正在使用接口。如果我的接口中的属性不会被实现类使用,我该怎么办?如果我拥有通用的功能怎么办 - 我应该使用基类还是实现多个接口?我真的只是要求某人确认我是否在正确的轨道上,因为我觉得我错过了一些简单的东西。



An示例如下:

I'm still pretty new to using interfaces and I'm unsure if I'm using interfaces as they are intended. What should I do if I have properties in my interfaces that won't be used by the implementing class? What if I have I functionality that is generic - should I rather be using a base class or be implementing multiple interfaces? I'm really just asking for someone to confirm whether or not I'm on the right track here, because I feel like I'm missing something simple.

An example below:

interface IPrinter
{
	string IPAddress { get; set; }
	
	void Print();
}

public class EthernetPrinter : IPrinter
{
	public string IPAddress { get; set; } // IPAddress
	public void Print() { Console.WriteLine("Printing over ethernet"); }
}

public class SerialPrinter : IPrinter
{
	public string IPAddress { get; set; } // Not going to be used. 
	public int CommPort { get; set; } // this is the actual property I need.
	public void Print() { Console.WriteLine("Printing serially"); }
}





然后像这样使用它。但是我不应该放弃使用OO编程的if语句吗?





And then use it like this. But aren't I supposed to be getting away from if statements with OO programming?

IPrinter printer;

if (someSetting == "UseEthernet")
    printer = new EthernetPrinter() { IPAddress = "1.1.1.1" };
else
    printer = new SerialPrinter() { CommPort = 1 };

printer.Print();





我的尝试:



然后我看到我可以进一步提取接口以使用其他设备的连接,但这感觉不太好。



What I have tried:

But then I see that I could be extracting the interfaces a little bit further to use the connection for other devices, but this doesn't feel much better.

interface IDataSender
{
	void SendData();
}
interface ISerialDataSender : IDataSender
{
	int CommPort { get; set;}
}

interface IEthernetSender : IDataSender
{
	string IPAddress { get; set;}
}

interface IPrinter
{
	void Print();
}

public class EthernetPrinter : IPrinter, IEthernetSender
{
	// this looks good
	public string IPAddress { get; set; }
	public void Print() { Console.WriteLine("Printing over ethernet"); }
	
	// but this feels like this should be in some other class 
	//as this could be pretty generic to any ethernet device.
	public void SendData() 	{ }
}

public class SerialPrinter : IPrinter, ISerialDataSender
{
	// once again, this seems correct
	public int CommPort { get; set; }
	public void Print() { Console.WriteLine("Printing serially"); }

	// but this doesn't.
	public void SendData() {}

}

推荐答案

接口只是保证类在接口上实现properties \methods,但是没有什么能阻止使用其他属性和方法的类。所以我将你的IPrinter接口简化为Print方法。



An interface is just a guarantee that the class implements the properties\methods on the interface, there is nothing stopping the class using other properties and methods though. So I'd reduce your IPrinter interface to just the Print method.

interface IPrinter
{
	void Print();
}

public class EthernetPrinter : IPrinter
{
	public string IPAddress { get; set; }
	public void Print() { Console.WriteLine("Printing over ethernet"); }
}

public class SerialPrinter : IPrinter
{
	public int CommPort { get; set; }
	public void Print() { Console.WriteLine("Printing serially"); }
}





您现有的代码仍然可以使用





Your existing code will still work

IPrinter printer;

if (someSetting == "UseEthernet")
    printer = new EthernetPrinter() { IPAddress = "1.1.1.1" };
else
    printer = new SerialPrinter() { CommPort = 1 };

printer.Print(); // no matter what branch the "if" takes above you know that the object referenced by "printer" has a Print method and that's all you need to know





至于你的if方法,如果你谷歌工厂模式将告诉你如何处理它。你仍然会有if,但它会隐藏在一个工厂类中,这将使你的主代码看起来更清晰。



As for your "if" method, if you google the factory pattern that will show you how you can deal with that. You'll still have the "if" but it will be hidden inside a factory class which will leave your main code looking cleaner.


引用:

如果我的接口中的属性不会被实现类使用,我该怎么办?

What should I do if I have properties in my interfaces that won't be used by the implementing class?



你不能,那是整点。



接口根本不能声明任何具体对象:他们所能做的就是声明必须由继承接口的类实现的项目。如果您尝试在接口中声明属性并给它任何真实定义:


You can't, that's the whole point.

Interfaces can't declare any concrete objects at all: all they can do is declare items which must be implemented by the class that inherits the interface. If you try to declare a property in your interface and give it any "real" definition:

public class EthernetPrinter : IPrinter
    {
    public string IPAddress { get; set; } // IPAddress
    public void Print() { Console.WriteLine("Printing over ethernet"); }
    }

然后你会得到一个编译错误。

如果你没有在派生类中实现该属性,你将得到一个编译错误然后也是。



如果你需要具体的对象,那么你使用的是抽象类。

Then you will get a compilation error.
If you fail to implement the property in derived classes, you will get a compilation error then as well.

If you need concrete objects, then you use an Abstract class instead.


这篇关于关于接口的新手问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆