接口变量无法访问所有类方法的原因 [英] Interface variable not able to access all class method why

查看:90
本文介绍了接口变量无法访问所有类方法的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个界面和一个类见下:

  interface  ILastName 
{
string LastName();
}

class MyName:ILastName
{
public string FirstName()
{
return Michael;
}

public string LastName()
{
return Dutt;
}
}





现在看看我的以下问题和假设

1)

 MyName myNameObj =  new  MyName(); 





在上面的代码行中新的Myname()有助于创建类的Object并在堆中分配内存这意味着MyName的所有方法都存在于堆中以供访问。和myNameObj包含对该内存的引用,这意味着我们可以使用myNameObj访问所有方法。 (方法名称)。



但是

2)

 ILastName obj =  new  MyName(); 





在上面的行中,同样的事情发生在新的Myname()创建类n的对象在堆中占用内存意味着所有可以在堆中访问的方法。



但我们只能访问接口中存在的那些方法,请参阅以下行我可以访问

 obj.LastName(); 





但是以下是不可能的?为什么?

 obj.FirstName(); 





为什么? obj还持有对MyName类在myNameObj中的MyName类占用的内存的引用,那么我们无法访问接口中不存在的方法???请帮我解决这个困惑。

解决方案

简单:在编译时,系统只知道 obj 拥有一个实现ILastName的类实例:

 ILastName obj =  new  MyName(); 

因为您已明确声明它的类型为 ILastName



您已分配了 MyName 实例到它没关系 - 系统无法确定在某些时候你不会分配 MyOnlyNameIsMyLastName 实例,或者 ArtistFormerllyKnownAs 实例,甚至。



如果你将变量声明为TypeX那么你只能使用在TypeX中声明的属性,字段和方法,或者它通过继承获取的类/接口 - 您不能使用派生类中声明的任何内容,因为并非所有派生类都需要实现它。


因为 FirstName()不是界面的一部分。

如果你想访问它,你必须对混凝土进行投射class:

 ILastName obj =  new  MyName(); 
Console.Write(obj.LastName());
Console.Write(((MyName)obj).FirstName());


ILastName obj = new MyName();

obj是ILastName,编译器不知道它是否是MyName。



在某种程度上,ILastName大于MyName,或者我们可以说它是某种MyName的容器。

就像有人来自纽约,他被介绍为他来自美国,没有人会说他是纽约人。但如果他被称为来自纽约,我们也可以说他是美国人。



在上面的例子中,ILastName = USA,MyName = NewYork ,某人= obj。

英语不是我的语言,我试过。


I have one interface and a class see below:

interface ILastName
{
   string LastName();
}

class MyName : ILastName
{
   public string FirstName()
   {
      return "Michael";
   }

   public string LastName()
   {
      return "Dutt";
   }
}



Now see my following questions and assumptions
1)

MyName myNameObj=new MyName();



in the above line of code new Myname() helps to create Object of class and it allocate memory in the heap that means all the method of MyName is present in heap for access. and myNameObj contain reference to that memory so means we can access all the method by using myNameObj. (name of method).

But
2)

ILastName obj=new MyName();



In the above line same thing happen new Myname() creating object of class n taking memory in heap that means all method available to access in heap.

But we are able to access only those methods which are present in Interface see following line I can access

obj.LastName();



but following is not possible why??

obj.FirstName();



why ?? obj is also holding the reference to the memory which is taken by MyName class in heap same as myNameObj, then we are not able to access method which is not present in interface??? please help me out for my this confusion.

解决方案

Simple: at compile time, the system only knows that obj holds a class instance that implements ILastName:

ILastName obj=new MyName();

Because you have explicitly declared it as being of the type ILastName

That you have assigned a MyName instance to it doesn't matter - the system can't be sure that at some point you won't assign a MyOnlyNameIsMyLastName instance, or a ArtistFormerllyKnownAs instance, even.

If you declare a variable as TypeX then you can only use properties, fields, and methods that are declared in TypeX or a class / Interface it acquires through inheritance - you cannot use anything that is declared in a derived class, because not all derived classes are required to implement it.


Because FirstName() is not part of the interface.
If you want to access it, you have to make a cast to the concrete class:

ILastName obj = new MyName();
Console.Write(obj.LastName());
Console.Write(((MyName)obj).FirstName());


ILastName obj=new MyName();
the obj is ILastName, the compiler doesn't know whether it is MyName or not.

In a way, ILastName is bigger than MyName, or we can say it's some sort of a container of MyName.
Like someone is from New York, and he is introduced like "He is from USA", nobody will say that he is a New Yorker. But if he was introduced as "from New York", we can also say that he is an American.

In the example above, ILastName=USA, MyName=NewYork, the "someone"=obj.
Eng is not my language, I tried.


这篇关于接口变量无法访问所有类方法的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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