C#的面试官问了一个问题:
一个公共类,有5个公共成员。一个人想要一个消耗两个成员的对象,另一个要消耗所有的五个成员。如何实现这一点?
我不明白这一点。谁能帮帮我这是什么意思?会有什么情况需要上面提到的东西呢?
发布于 2022-02-02 16:06:06
嗯,很难说这是否是面试官想听的。你应该问他对人和消费到底意味着什么。
我猜他想听你了解什么是接口:
public interface IPerson: IHaveName
{
DateTime DateOfBirth {get;set;}
string MainAddress {get;set;}
string MainPhoneNumber {get;set;}
}
public interface IHaveName
{
string FirstName {get;set;}
string LastName {get;set;}
}
public class Person: IPerson
{
public string FirstName {get;set;}
public string LastName {get;set;}
public DateTime DateOfBirth {get;set;}
public string MainAddress {get;set;}
public string MainPhoneNumber {get;set;}
}现在可以将Person传递给需要使用整个Person的方法或只需要使用名称的方法:
public static void Main()
{
Person p = new Person(); // skip initialization
ConsumePerson(p);
ConsumeNames(p);
}
public static void ConsumePerson(IPerson person)
{
// do something with the whole Person object
}
public static void ConsumeNames(IHaveName name)
{
// do something with just the FirstName/LastName
}https://stackoverflow.com/questions/70958328
复制相似问题