问题描述
如何在C#的接口和类中声明此内容.
How do i declare this in Interface and Class in C#.
viewTrains(string source,string destination):List<ITrain>
我正在制作一个控制台应用程序,该方法有一个要求,但是我很困惑如何在接口中声明此方法并在类中继承.我搜索很多类似带有类型接口的列表",但没有找到任何东西.并且也请告诉我,C#中所谓的什么.请帮我.谢谢.
我尝试过的事情:
我尝试过:-
I am making a Console Application, There is a requirement of This Method, But i am in Confusion how To Declare This Method in Interface and Inherit in Class. I Search a Lot Like "List with Type Interface" but i haven''t find Anything. and also Please tell me, What is called in C#. Please Help Me. Thanks.
What I have tried:
I Tried:-
interface ITrain
{
int Id { get; set; }
string Name { get; set; }
string Source { get; set; }
string Destination { get; set; }
}
public List<ITrain> viewTrains(string source, string destination)
{
}
这样对吗 ?如果正确,则请提供此示例.
is it Right ? if Right Then Please Provide Example of this.
推荐答案
viewTrains(字符串源,字符串目标):List< itrain>
viewTrains(string source,string destination):List<itrain>
这不是正确的",因为它不是有效的C#代码.它看起来像方法声明,但不是.
接口的主要用途是使不同类型的对象实例的集合转换为它们继承的相同接口类型.我认为这就是这个问题的实质.
有关如何序列化将共享一个公共接口的不同类实例的集合序列化为接口实例:"的信息,请参见这篇文章: [ ^ ].
因此,您需要实现一些继承自ITrain的类,它们具有自己的唯一属性,字段或方法:
This doesn''t "look right" in the sense it is not valid C# code. It looks like a method declaration, but it isn''t.
A primary use of Interfaces is to enable having a Collection of different Types of object instances cast to the same Interface Type(s) they have inherited; I assume that''s what this question is really about.
edit: for information on how to serialize a collection of different Class instances that share a common Interface as a collection of Interface "instances:" see this post: [^].
So, you need to implement some Classes that inherit from ITrain, and have their own unique Properties, or Fields, or Methods:
public class RegularTrains : ITrain
{
// implement all the ITrain declared Properties
// unique to this Class
public int YearsInService { get; set; }
public RegularTrains(int id, string name, string source, string dest, int yrsservice)
{
// assign all the ITrain parameters to the correct variables
// unique to this class
YearsInService = yrsservice;
}
}
public enum TechnologyType
{
Maglev,
InternalCombustion,
Other
}
public class HighSpeedTrains : ITrain
{
// implement all the ITrain declared Properties
// unique to this Class
public TechnologyType TechType { get; set; }
public HighSpeedTrains(int id, string name, string source, string dest, TechnologyType techtype)
{
// assign all the ITrain parameters to the correct variables
// unique to this class
TechType = techtype;
}
}
现在的问题是:您在哪里以及如何创建和存储所有列车的列表.一种方法是这样的:
Now the question is: where and how do you create and store a list of all the Trains. One way is like this:
public static class AllTrains
{
public static List<ITrain> Trains { set; get; }
public static List<ITrain> ViewTrains(string source, string destination)
{
// use Linq here to compose a List of ITRains where the supplied parameters
// 'source and 'destination match an ITRain
}
}
因此,这里有一个静态类,可将Train Collections强制转换为ITrain形式.现在,列表如何添加项目?一种方法是使用继承的类构造函数:
So, here you have a static class that keeps a Collection of Trains cast to their ITrain form. Now how does the list get items added; one way is to use the inheriting classes constructors:
public RegularTrains(int id, string name, string source, string dest, int yrsservice)
{
// assign all the ITrain parameters to the correct variables
// unique to this class
YearsInService = yrsservice;
// add the current instance to the static List
AllTrains.Trains.Add(this);
}
然后,您可以执行以下操作:
Then, you can do something like this:
RegularTrain rtrain = new RegularTrain(1,"r1","Topeka", "Chicago", 14);
HighSpeedTrain strain = new HighSpeedTrain(2, "h1", "Topeka", "Chicago", TechnologyType.Maglev);
var allTrains = AllTrains.ViewTrains("Topeka", "Chicago");
请注意,将RegularTrain类的新实例添加到静态ITrain列表中并不需要将实例强制转换为Type ITrain :转换在类型系统中是隐式的.
当您将对象投射到其继承的接口时,实质上是创建了该对象的视图":该视图限制您只能看到(访问)接口中声明的属性或方法:该对象仍然是那里"及其所有唯一字段,属性和方法:尚未复制.
在Visual Studio中使用IntelliSense来检查ITrain列表,并查看一个特定的项目,您可以了解反射如何从界面看向基本类型.
Note that adding the new instance of the RegularTrain Class to the static List of ITrain did not require casting the instance to Type ITrain: the conversion is implicit in the Type system.
When you cast an object to an Interface it inherits from, essentially you have created a "view" of the object: that view restricts you to seeing (accessing) only the Properties or Methods declared in the Interface: the object is still "there" with all its unique fields, properties, methods: it has not been copied.
Use IntelliSense in Visual Studio to examine the List of ITrain, and look at a specific item, and you can get an idea of how Reflection can look beyond the interface into the fundamental Type.
这篇关于我如何在C#中声明这一点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!