什么是使用接口名称创建类的实例的概念? [英] What is the concept of creating class instance using interface name?

查看:209
本文介绍了什么是使用接口名称创建类的实例的概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

 的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;

这篇关于什么是使用接口名称创建类的实例的概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆