当前位置:网站首页>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
边栏推荐
- . net 5 Web custom middleware implementation returns the default picture
- When migrating tslib_ setup: No such file or directory、ts_ open: No such file or director
- Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
- Top 9 task management system in 2022
- 批量下載文件----壓縮後再下載
- File upload vulnerability summary and upload labs shooting range documentary
- A comprehensive understanding of static code analysis
- be based on. NETCORE development blog project starblog - (2) environment preparation and creation project
- 打卡:4.23 C语言篇 -(1)初识C语言 - (12)结构体
- 一套组合拳,打造一款 IDEA 护眼方案
猜你喜欢

Log4net is in Net core usage

QT learning summary

After the mobile phone is connected to the computer, how can QT's QDIR read the mobile phone file path

Comprehensive calculation of employee information

【无标题】

The most understandable life cycle of dependency injection

2022t elevator repair test simulation 100 questions and online simulation test

MySQL之explain关键字详解

关于idea调试模式下启动特别慢的优化

2022g2 boiler stoker examination question bank and online simulation examination
随机推荐
超好用的Excel异步导出功能
[vs Code] solve the problem that the jupyter file displays exceptions in vs code
你真的懂hashCode和equals吗???
2022年做跨境电商五大技巧小分享
Supersocket is Use in net5 - concept
Scenario Title: how does system a use the page of system B
为什么BI对企业这么重要?
POI create and export Excel based on data
可以接收多種數據類型參數——可變參數
《C语言程序设计》(谭浩强第五版) 第7章 用函数实现模块化程序设计 习题解析与答案
超好用的【通用Excel导入功能】
Quartz. Www. 18fu Used in net core
Oracle query foreign keys contain comma separated data
Comprehensive calculation of employee information
Fundamentals of software testing and development
Flink real-time data warehouse project - Design and implementation of DWS layer
Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
Problem a: face recognition
Web Course Design - his system
月薪10k-20k都无法回答的事务问题,你会吗?