首页 > 数据库技术 > 详细

【转载】#473 - Access Interface Members through an Interface Variable

时间:2014-03-13 07:35:52      阅读:582      评论:0      收藏:0      [点我收藏+]

Onece a class implementation a particular interface, you can interact with the members of the interface through any instance of that class.

1 Cow bossie = new Cow("Bossie", 5);
2 
3 // Cow implements IMoo, which includes Moo method
4 bossie.Moo();

You can also declare a variable whose type is an interface, assign it to an instance of any class that implements that interface and then interact with members of the interface through that interface variable.

1 // bossie is a Cow, which implements IMoo, so we can point IMoo variable at her
2 IMoo mooer = bossie;
3 mooer.Moo();

Notice that we can‘t access members of Cow that aren‘t part of IMoo using this interface variable.

1 // Can‘t call Cow methods not in IMoo
2 mooer.MakeSomeMilk();

Even though MakeSomeMilk is a public method in the Cow class, we can‘t access it via IMoo.

原文地址:#437 – Access Interface Members through an Interface Variable

【转载】#473 - Access Interface Members through an Interface Variable,布布扣,bubuko.com

【转载】#473 - Access Interface Members through an Interface Variable

原文:http://www.cnblogs.com/yuthreestone/p/3596316.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!