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
新聞熱點
疑難解答
圖片精選