当前位置:网站首页>C interface
C interface
2022-04-23 03:22:00 【C # primary advanced】

Be careful :1、 The interface cannot contain fields , Such as variable , Otherwise, the compilation will report an error ;
2、 Members in an interface cannot have definitions , For example, you cannot write a method body ;
3、 An interface is the implementation specification of a class , In other words, the interface specifies the prototype of the method, and there are classes to implement the prototype defined by the interface , Call the interface definition method prototype through class inheritance interface
4、 The class calling the interface must implement the method prototype defined in the called interface
5、 The method prototype of the class calling interface must have sufficient access rights (public)
6、 Interface can realize that the interface calls the same interface and different classes
7、 A class can implement multiple interfaces ( class : Interface 1, Interface 2{})
8、 Once the interface is implemented , The implementation class must implement all members in the interface , Unless the implementation class itself is an abstract class .
9、C# It's single inheritance , The interface is to solve C# The inner class can inherit multiple base classes at the same time .
10、 Interface cannot be instantiated directly
The difference between interface and abstract class :
1、 The interface is used to specify , Abstraction is used for commonality ; Abstract classes are intelligent classes that are inherited by sheets , The interface can implement multiple inheritance .
2、 Interfaces can only declare methods 、 attribute 、 event 、 Indexer , Abstract classes can be implemented with methods , You can also define variables of non static classes
// Case study
//USB Realization : Can point 、 Can blow
interface IUSB
{
// This method has no method body , The default is
//public、virtual、abstract modification
void ChaBa();
}
class ShuBiao:IUSB
{
public void ChaBa()
{
Console.WriteLine(" Can point ");
}
}
class FengShan:IUSB// Class implementation interface
{
public void ChaBa()
{
Console.WriteLine(" Can blow ");
}
}
static void Main(string[] args)
{
//IUSB a = new IUSB();
IUSB a = new FengShan();// polymorphic
a.ChaBa();
// // a = new ShuBiao();// polymorphic
// // a.ChaBa();
// IUSB b = new ShuBiao();// polymorphic
// b.ChaBa();
// FengShan fs = new FengShan();// object-oriented programming
// fs.ChaBa();
//}
// Interface is a capability
// Case study
abstract class Door
{
public abstract void OpenDoor();
public abstract void CloseDoor();
}
interface Lock
{
void OpenLock();
void CloseLock();
}
class FangDaoDoor:Door,Lock
{
public override void OpenDoor()
{
Console.WriteLine(" Open door ");
}
public override void CloseDoor()
{
Console.WriteLine(" close ");
}
public void OpenLock()
{
Console.WriteLine(" The lock ");
}
public void CloseLock()
{
Console.WriteLine(" locked ");
}
//static void Main(string[] args)
//{
// FangDaoDoor fd = new FangDaoDoor();
// Door d = fd;
// Lock a = fd;
// d.CloseDoor();
// a.CloseLock();
// a.OpenLock();
// d.OpenDoor();
//}
}
// An interface is a convention
// Case study
interface Printer
{
void PrintA4();
void PrintB5();
}
class User:Printer
{
string color;
public void PrintA4()
{
color = " colour ";
Console.WriteLine(" Print "+color+"A4 paper ");
}
public void PrintB5()
{
color = " Black and white ";
Console.WriteLine(" Print " + color + "B5 paper ");
}
static void Main(string[] args)
{
Printer p = new User();
p.PrintA4();
p.PrintB5();
}
}
版权声明
本文为[C # primary advanced]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220621093772.html
边栏推荐
- Explication détaillée des fonctions send () et recv () du programme Socket
- JS implementation of new
- QT dynamic translation of Chinese and English languages
- MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】
- Preview of converting doc and PDF to SWF file
- Problem C: realize Joseph Ring with linked list
- Five tips for cross-border e-commerce in 2022
- The most understandable life cycle of dependency injection
- Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions
- Is it difficult to choose binary version control tools? After reading this article, you will find the answer
猜你喜欢

Fiddler use

A comprehensive understanding of static code analysis
![Eight elder brothers chronicle [4]](/img/87/f695d0275f8a66b9def48a75668d15.png)
Eight elder brothers chronicle [4]
![[MySQL] left Function | Right Function](/img/26/82e0f2280de011636c26931a74e749.png)
[MySQL] left Function | Right Function

研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台

Explanation keyword of MySQL

Configure automatic implementation of curd projects

. net webapi access authorization mechanism and process design (header token + redis)

AWS from entry to actual combat: creating accounts

超好用的【通用Excel导入功能】
随机推荐
C-11 problem I: find balloon
MySQL keyword group_ Concat, combined connection query
Aspnetcore configuration multi environment log4net configuration file
[MySQL] left Function | Right Function
Improvement of ref and struct in C 11
xutils3修改了我提报的一个bug,开心
Flink实时数仓项目—DWS层设计与实现
js 中,为一个里面带有input 的label 绑定事件后在父元素绑定单机事件,事件执行两次,求解
Eight elder brothers chronicle [4]
关于idea调试模式下启动特别慢的优化
Quartz. Www. 18fu Used in net core
Docker拉取mysql并连接
socket编程 send()与 recv()函数详解
打卡:4.23 C语言篇 -(1)初识C语言 - (12)结构体
When migrating tslib_ setup: No such file or directory、ts_ open: No such file or director
集合之List接口
JS implementation of new
ThreadLocal 测试多线程变量实例
Xutils3 corrected a bug I reported. Happy
Explication détaillée des fonctions send () et recv () du programme Socket