为什么不接口[Serializable接口]?接口、Serializable

由网友(时光凉春衫薄)分享简介:我倒是觉得并称属性界面将有助于确保你不要创建使用接口类,却忘了让他们序列化。I would think that adding that attribute to an interface would be helpful make sure you do not create classes that use th...

我倒是觉得并称属性界面将有助于确保你不要创建使用接口类,却忘了让他们序列化。

I would think that adding that attribute to an interface would be helpful make sure you do not create classes that use the interface and forget to make them serializable.

这可能是一个很基本的问题,但我想请教专家。

This could be a very fundamental question, but I wanted to ask the experts.

推荐答案

接口定义一个合同,没有自己的任何状态。

Interfaces define a contract and do not have any state of their own.

序列化是关于保存和加载的状态的流入和流出的对象模型。

Serialization is about saving and loading state into and out of an object model.

没有多大意义,以序列化的东西,没有保持状态。

Not much point to serializing something that holds no state.

要回答迫使接口的实现是序列化的实际问题 - 这就是为什么ISerializable存在界面。

To answer the practical question of forcing an implementation of an interface to be Serializable - this is why the ISerializable interface exists.

在.NET中你可以定义应该实现其他接口的接口:

In .NET you can declare an interface that should implement other interfaces:

interface MustBeSerializable : ISerializable {}

请参阅一些更多的信息here.

See some more information here.

阅读全文

相关推荐

最新文章