- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
今天我遇到了 C# 接口(interface)的概念,我有一个希望很简单的问题,看看我是否理解它们......它们与 C++ 头文件非常相似吗?我的意思是,据我所知,您定义了一个类的主干,但实际上没有定义它的作用,这有点类似于标题,对吗?我阅读了整个 MSDN 定义,它并没有真正让我 100% 清楚。我相信我有这个想法(编写并附上了一个非常初级的程序,看看我是否理解)但是我至少在明天晚上之前完全理解它们的基础知识是非常重要的。
namespace InterfaceTest
{
class Program
{
static void Main(string[] args)
{
KitchenStaff newKitchen = new KitchenStaff();
newKitchen.getBakers();
newKitchen.getBaristas();
newKitchen.getCooks();
Console.ReadLine();
KitchenDuties newKitchen1 = new KitchenDuties();
newKitchen1.getBakers();
newKitchen1.getBaristas();
newKitchen1.getCooks();
Console.ReadLine();
}
}
interface Bakers
{
void getBakers();
}
interface Cooks
{
void getCooks();
}
interface Baristas
{
void getBaristas();
}
class KitchenInfo
{
private string m_kitchen_name = "";
private Int16 m_bakers = 0;
private Int16 m_baristas = 0;
private Int16 m_cooks = 0;
public string Name
{
get
{
return m_kitchen_name.ToString();
}
set
{
m_kitchen_name = value;
}
}
public string Bakers
{
get
{
return m_bakers.ToString();
}
set
{
m_bakers = Convert.ToInt16(value);
}
}
public string Baristas
{
get
{
return m_baristas.ToString();
}
set
{
if (value != string.Empty)
{
m_baristas = Convert.ToInt16(value);
}
}
}
public string Cooks
{
get
{
return m_cooks.ToString();
}
set
{
if (value != string.Empty)
{
m_cooks = Convert.ToInt16(value);
}
}
}
}
class KitchenStaff : KitchenInfo, Bakers, Cooks, Baristas
{
public KitchenStaff()
{
Console.WriteLine("What is this kitchens name?");
Name = Console.ReadLine();
Console.WriteLine("How many bakers?");
Bakers = Console.ReadLine();
Console.WriteLine("How many baristas?");
Baristas = Console.ReadLine();
Console.WriteLine("How many cooks?");
Cooks = Console.ReadLine();
}
public void getBakers()
{
System.Console.WriteLine("In {0} there are {1} bakers.", Name, Bakers);
}
public void getBaristas()
{
System.Console.WriteLine("In {0} there are {1} baristas.", Name, Baristas);
}
public void getCooks()
{
System.Console.WriteLine("In {0} there are {1} cooks.", Name, Cooks);
}
}
class KitchenDuties : KitchenInfo, Bakers, Cooks, Baristas
{
public KitchenDuties()
{
Console.WriteLine("What is this kitchens name?");
Name = Console.ReadLine();
Console.WriteLine("How many bakers?");
Bakers = Console.ReadLine();
Console.WriteLine("How many baristas?");
Baristas = Console.ReadLine();
Console.WriteLine("How many cooks?");
Cooks = Console.ReadLine();
}
public void getBakers()
{
System.Console.WriteLine("In {0}, the {1} bakers make fattening cookies.", Name, Bakers);
}
public void getBaristas()
{
System.Console.WriteLine("In {0}, the {1} baristas serve hot coffee.", Name, Baristas);
}
public void getCooks()
{
System.Console.WriteLine("In {0}, the {1} cooks make tender steak.", Name, Cooks);
}
}
}
最佳答案
从概念上看,您似乎并不了解接口(interface)。
虽然 C# 中的接口(interface)具有特定含义,但在包括 C++ 在内的任何面向对象语言中,更普遍的是,您可以谈论类的“公共(public)接口(interface)”。这本质上是另一个类可以看到的:它看不到私有(private)成员,也看不到方法的内容,它只能看到其公共(public)成员的签名。
例如,如果我有一个类:
public class MyClass
{
private int _myPrivateInt;
public int MyPublicInt;
private void DoSomethingPrivate(int input)
{
//Some code goes here
}
public void DoSomethingPublic(int input)
{
//Some more code goes here
}
}
对于不同的类,所有“可见”的内容将是:
int MyPublicInt;
void DoSomethingPublic(int input);
这是为了封装——一个类不应该关心另一个类的具体实现细节。 MyClass 公开声明其他类需要知道如何与之交互的内容,并将其他所有内容保留给自己。
这就是接口(interface)实际上是什么的概念,在 C# 中,接口(interface)几乎是一种无需公共(public)方法即可指定信息的方法。要理解其中的原因,您需要了解一个相关概念,多态性。考虑这些类:
public class InMemoryCustomerDataStorage
{
public void StoreCustomerInfo(CustomerInfo info)
{
//Actual implementation goes here
}
public CustomerInfo RetrieveCustomerInfo(int customerId)
{
//Actual implementation goes here
}
}
public class DatabaseCustomerDataStorage
{
public void StoreCustomerInfo(CustomerInfo info)
{
//Actual implementation goes here
}
public CustomerInfo RetrieveCustomerInfo(int customerId)
{
//Actual implementation goes here
}
}
public class FileCustomerDataStorage
{
public void StoreCustomerInfo(CustomerInfo info)
{
//Actual implementation goes here
}
public CustomerInfo RetrieveCustomerInfo(int customerId)
{
//Actual implementation goes here
}
}
这三者的目的相同,但方式不同。它们都允许您存储客户信息,并通过 id 检索它。他们也可能有我没有写出来的其他私有(private)成员,例如 File 可能有一个创建文件的方法,如果它不存在,等等。他们都有实际的实现,而不是那些评论,当然, 我没有把它们全部写出来,因为这只是一个例子。
那么界面如何在这里派上用场呢?关键是您的程序的某些部分可能想要存储和检索客户信息,但并不关心它是如何完成的。事实上,一般来说,他们不应该明确指定如何存储它,除非他们直接需要关心。如果随着程序的增长,您需要更改使用的存储类型怎么办?您必须找到您编写的每个地方 InMemoryCustomerDataStorage
并将其替换为 DatabaseCustomerDataStorage
。或者,如果您大部分时间都想使用数据库,但是当您测试代码时,您想使用内存存储以便它运行得更快怎么办?您甚至可以编写一个需要进行数据存储但本身不包含数据存储类的库。对此的解决方案是使用接口(interface)。你会写:
public interface ICustomerDataStorage
{
void StoreCustomerInfo(CustomerInfo info);
CustomerInfo RetrieveCustomerInfo(int customerId);
}
然后在一个需要做一些客户数据存储的类中可能是这样的:
public class NewCustomerProcessor
{
private ICustomerDataStorage _storage;
public NewCustomerProcessor(ICustomerDataStorage storage)
{
_storage = storage;
}
public void Process(string name, string address, string email, int age)
{
CustomerInfo info = new CustomerInfo(name, address, email, age);
if(Validate(info))
_storage.StoreCustomerInfo(info);
}
private bool Validate(CustomerInfo info)
{
//Some validation logic
}
}
现在这个类功能齐全,在任何时候它都不必担心使用 ICustomerDataStorage 的具体实现。它可以存储在内存中、文件中、数据库中,它并不关心。实现 ICustomerDataStorage 的类可以在完全不同的程序集和项目中,我可以将我的项目放在 github 上,任何下载它以在他们自己的项目中使用的人都可以编写他们自己的 ICustomerDataStorage 实现。
关于c# - 接口(interface)和 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22697454/
我正在尝试在我的代码库中为我正在编写的游戏服务器更多地使用接口(interface),并了解高级概念以及何时应该使用接口(interface)(我认为)。在我的例子中,我使用它们将我的包相互分离,并使
我有一个名为 Widget 的接口(interface),它在我的整个项目中都在使用。但是,它也用作名为 Widget 的组件的 Prop 。 处理此问题的最佳方法是什么?我应该更改我的 Widget
有一个接口(interface)可以是多个接口(interface)之一 interface a {x:string} interface b {y:string} interface c {z:st
我遇到了一种情况,我需要调用第三方服务来获取一些信息。这些服务对于不同的客户可能会有所不同。我的界面中有一个身份验证功能,如下所示。 interface IServiceProvider { bool
在我的例子中,“RequestHandlerProxy”是一个结构,其字段为接口(interface)“IAdapter”,接口(interface)有可能被调用的方法,该方法的输入为结构“Reque
我有一个接口(interface)Interface1,它已由类A实现,并且设置了一些私有(private)变量值,并且我将类A的对象发送到下一个接受输入作为Interface2的类。那么我怎样才能将
假设我有这样的类和接口(interface)结构: interface IService {} interface IEmailService : IService { Task SendAs
有人知道我在哪里可以找到 XML-RPC 接口(interface)的定义(在 OpenERP 7 中)?我想知道创建或获取对象需要哪些参数和对象属性。每个元素的 XML 示例也将非常有帮助。 最佳答
最近,我一直在阅读有关接口(interface)是抽象的错误概念的文章。一篇这样的帖子是http://blog.ploeh.dk/2010/12/02/InterfacesAreNotAbstract
如果我有一个由第三方实现的现有 IInterface 后代,并且我想添加辅助例程,Delphi 是否提供了任何简单的方法来实现此目的,而无需手动重定向每个接口(interface)方法?也就是说,给定
我正在尝试将 Article 数组分配给我的 Mongoose 文档,但 Typescript 似乎不喜欢这样,我不知道为什么它显示此警告/错误,表明它不可分配. 我的 Mongoose 模式和接口(
我有两个接口(interface): public interface IController { void doSomething(IEntity thing); } public inte
是否可以创建一个扩展 Serializable 接口(interface)的接口(interface)? 如果是,那么扩展接口(interface)的行为是否会像 Serilizable 接口(int
我试图在两个存储之间创建一个中间层,它从存储 A 中获取数据,将其转换为相应类型的存储 B,然后存储它。由于我需要转换大约 50-100 种类型,我希望使用 map[string]func 并根据 s
我正在处理一个要求,其中我收到一个 JSON 对象,其中包含一个日期值作为字符串。我的任务是将 Date 对象存储在数据库中。 这种东西: {"start_date": "2019-05-29", "
我们的方法的目标是为我们现有的 DAO 和模型类引入接口(interface)。模型类由各种类型的资源 ID 标识,资源 ID 不仅仅是随机数,还带有语义和行为。因此,我们必须用对象而不是原始类型来表
Collection 接口(interface)有多个方法。 List 接口(interface)扩展了 Collection 接口(interface)。它声明与 Collection 接口(int
我有一个 Java 服务器应用程序,它使用 Jackson 使用反射 API 对 DTO 进行一般序列化。例如对于这个 DTO 接口(interface): package com.acme.libr
如果我在 Kotlin 中有一个接口(interface): interface KotlinInterface { val id: String } 我可以这样实现: class MyCla
我知道Java中所有访问修饰符之间的区别。然而,有人问了我一个非常有趣的问题,我很难找到答案:Java 中的 private 接口(interface)和 public 接口(interface)有什
我是一名优秀的程序员,十分优秀!