本文介绍了接口无法封装方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我了解到接口也用于封装方法,但是在下面的代码中,通过将ojb转换为MainClass,我可以从Mainclass访问其他方法,我没有在接口中声明,所以现在封装发生在哪里。
As I learn that interface is also used for encapsulate method but in the following code by casting ojb to MainClass I am able to access other method from Mainclass which I did not declare in interface so now where is encapsulation occur.
class Program
{
static void Main(string[] args)
{
IInterface obj = new MainClass();
Console.WriteLine(((MainClass)obj).FullName()+" " +obj.LastName());
Console.WriteLine(((MainClass)obj).SayHello());
Console.ReadKey();
}
}
public interface IInterface
{
string LastName();
}
public class MainClass:IInterface
{
public string FullName()
{
return "Raman Singh";
}
public string SayHello()
{
return "Hello Sir111";
}
public string LastName()
{
return "Chauhan";
}
}
推荐答案
这篇关于接口无法封装方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!