C#and VB.net

來源:互聯網
上載者:User
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
{
/// <summary>
/// 應用程式的主進入點。
/// </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





相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.