本文介绍了将对象转换为接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 有人可以解释投射到界面背后的想法吗? 例如: - >我有一个包含Read()方法的IInterface。 - >我有一个对象obj实现IInterface。 为什么有人会这样做,这是什么意思? 对象obj = new Object(); IInterface var =(IInterface)obj var.Read() 谢谢Hi,Can someone explain the idea behind casting to an interface?For example:-> I have an IInterface that contains a Read() method.-> I have an object "obj" that implements IInterface.Why would someone do the following and what does it mean?Object obj = new Object();IInterface var = (IInterface) objvar.Read()Thanks推荐答案 假设Object obj = new Object();实际上意味着SomeObjectThatImplemetsIInterface obj = new SomeObjectThatImplemetsIInterface();,在投射到IInterface时我真的没有意义, 。如果确实SomeObjectThatImplemetsIInterface 实现了IInterface那么你c(w)应该只调用obj.Read(); 我的猜测是代码是由没有经验的程序员写的。如果我错了,请 纠正我。 ChrisAssuming that "Object obj = new Object();" really means "SomeObjectThatImplemetsIInterfaceobj = new SomeObjectThatImplemetsIInterface();", there really is no point,that I can think of, in casting to IInterface. If it is true that SomeObjectThatImplemetsIInterfaceimplements IInterface then you c(w)ould just call obj.Read();My guess is that the code was written by an inexperienced programmer. Pleasecorrect me if I''m wrong.Chris 这篇关于将对象转换为接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-08 21:15