当前位置:网站首页>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
边栏推荐
- 打卡:4.23 C语言篇 -(1)初识C语言 - (12)结构体
- Can you answer the questions that cannot be answered with a monthly salary of 10k-20k?
- Do you really understand hashcode and equals???
- Flink实时数仓项目—DWS层设计与实现
- Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
- socket編程 send()與 recv()函數詳解
- 一文了解全面静态代码分析
- JS inheritance
- Supersocket is Use in net5 - startup
- JS, bind the event for a label with input, and then bind the stand-alone event in the parent element. The event is executed twice and solved
猜你喜欢

Is it difficult to choose binary version control tools? After reading this article, you will find the answer

《C语言程序设计》(谭浩强第五版) 第7章 用函数实现模块化程序设计 习题解析与答案

The most easy to understand service container and scope of dependency injection

移植tslib时ts_setup: No such file or directory、ts_open: No such file or director

MySQL query specifies that a row is sorted to the first row

Quartz. Www. 18fu Used in net core

二进制文件版本控制工具选择难?看完这篇你会找到答案

2022G2电站锅炉司炉考试题库及在线模拟考试
![Detailed description of MySQL index [B + tree index, hash index, full-text index, overlay index]](/img/1a/a22b4a35d3c083438d0f766a5ecb08.png)
Detailed description of MySQL index [B + tree index, hash index, full-text index, overlay index]

MySQL之explain关键字详解
随机推荐
Mysql database design specification
Query stored procedures in PostgreSQL
Node configuration environment CMD does not take effect
Charles uses three ways to modify requests and responses
When migrating tslib_ setup: No such file or directory、ts_ open: No such file or director
ASP. Net 6 middleware series - conditional Middleware
Super easy to use asynchronous export function of Excel
File upload vulnerability summary and upload labs shooting range documentary
General testing technology [1] classification of testing
oracle 查询外键含有逗号分隔的数据
A comprehensive understanding of static code analysis
Cefsharp stores cookies and reads cookies
2022 Shandong Province safety officer C certificate work certificate question bank and online simulation examination
Why is bi so important to enterprises?
《C语言程序设计》(谭浩强第五版) 第7章 用函数实现模块化程序设计 习题解析与答案
MySql分组查询规则
QT learning summary
First in the binary tree
There is no index in the database table. When inserting data, SQL statements are used to prevent repeated addition (Reprint)
Experiment 5 components and event handling