什么是使用接口名称创建类的实例的概念?实例、接口、概念、名称

2023-09-03 04:14:20 作者:情伤玫瑰

什么是组变量或对象的概念或我不知道我什么时候创建类的实例,并投入左手接口的名字它叫什么,,,我知道我们能不能​​创建和对象类型的接口。

只有我需要更多的澄清一下这个过程命名或者什么是净做细节,当我宣布这些类型的对象。

 的IDataReader oSQLReader =新SqlDataReader的();
IDataReader的oOLEReader =新OleDbDataReader();
 

解决方案

究竟会发生什么事是你创建的具体类的实例,那么你的上溯造型提及的类型接口。

引用的类型定义是什么,你可以在实例访问。您只能使用该接口知道的类的成员。

对象的实际类型仍然是您所创建的实例的类型,这样你就可以再次垂头丧气的引用,类型:

  SqlDataReader的读卡器=(SqlDataReader的)oSQLReader;
 

这个叫什么接口 叫什么名称

what is the concept of set variable or object or i don't know what it's called when i create instance of class and putting in left hand the name of interface,,, I Know that we can't create and object of type interface.

Only I need more clarification what this process named or what is the details done by .Net when I declare these type of object.

IDataReader oSQLReader = new SqlDataReader();
IDataReader oOLEReader = new OleDbDataReader();

解决方案

What happens exactly is that you are creating an instance of the specific class, then you are upcasting the reference to the type of the interface.

The type of the reference defines what you can access in the instance. You can only use members of the class that the interface knows about.

The actual type of the object is still the type of the instance that you created, so you can downcast the reference to that type again:

SqlDataReader reader = (SqlDataReader)oSQLReader;