C#and VB.net

C#中的接口
interface
[public|protected|private] interface InterfaceName
{
//mothed
// propery
// event
//delegate
}
在实现接口时,带接口名与不带接口的区别
不带的区别eg:
public interface IMyShow
{
void Show();
}
public class MyShow:IMyShow
{
public void Show()//必须写上前的public若写成void Show()出错
{
System.Console.Write(" 不带接口名");
}
}
public class MyMain
{
public static void Main()
{
// 用类定义引用
MyShow obj=new MyShow();
obj.Show();
//用接口引用方法
IMyShow obj2=new MyShow();
obj2.Show();

}
}

//带接口名
public interface IMyShow
{
System.Console.Write("带接口名");
}
public class MyShow:IMyShow
{
void IMyShow.Show()// 前面不能带上任何限定词
{
System.Console.Write("带接口名");
}
}
public class MyMain
{
public static void Main()
{
MyShow obj=new MyShow();
obj.Show();//非法因为加了限定词后,这个方法专属于专们的一个引用,只能有接口去引用
IMyShow obj2=new MyShow();
obj2.Show();

}
}
看完上面的内容我想为C#的爱好留个问题。请大家一起来讨论一下
public interface IMyShow
{
void Show();
}
public interface IMyShow2
{
void Show();
}

public class Myclass:IMyShow,IMyShow2
{
public Myclass()
{

}

void IMyShow.Show()
{
System.Console.Write("IMyShow");

}

public void Show()
{
System.Console.Write("Myclass show");
}

void IMyShow2.Show()
{
System.Console.Write("IMyShow2.Show()");

// TODO: 添加 Myclass.Show 实现
}

}

class Class1
{
///

1<summary>   
2/// 应用程序的主入口点。   
3/// </summary>

[STAThread]
static void Main(string[] args)
{
IMyShow obj2=new Myclass ();
obj2.Show();
IMyShow obj1=new Myclass();
obj1.Show();
Myclass obj=new Myclass();
obj.Show();

}
}
}

Namespace wfgSpace
Public Interface IMyShow
Sub Show()
Function Add(ByVal a As Integer, ByVal b As Integer) As Integer

End Interface
Public Interface IMyShow2
Sub Show()
Function Add(ByVal a As Integer, ByVal b As Integer) As Integer

End Interface
Public Class MyCls : Implements IMyShow, IMyShow2
Private iValue As Integer

Sub Show() Implements IMyShow.Show
System.Console.Write("wfng fu guo")
End Sub
Sub Show2() Implements IMyShow2.Show
System.Console.Write("wfg")

End Sub
Function Add2(ByVal a As Integer, ByVal b As Integer) As Integer Implements IMyShow.Add, IMyShow2.Add

iValue = a + b

System.Console.WriteLine("{0}+{1}={2}", a, b, iValue)
End Function

End Class
Public Class Common
Public Shared Sub main()
Dim obj As MyCls = New MyCls
Dim obj2 As IMyShow = New MyCls
Dim obj3 As IMyShow2 = New MyCls
System.Console.WriteLine("Class MyCls Object")
obj.Show2()
obj.Add2(5, 4)

System.Console.WriteLine("interface IMyShow object")
obj2.Show()
obj2.Add(5, 4)
System.Console.WriteLine("interface IMyShow2 object")
obj3.Show()
obj3.Add(5, 4)

End Sub
End Class

End Namespace

Published At
Categories with Web编程
Tagged with
comments powered by Disqus