当前位置:网站首页>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
边栏推荐
- Student achievement management
- Generate QR code through zxing
- Knowledge of software testing~
- General test technology [II] test method
- 一套组合拳,打造一款 IDEA 护眼方案
- LoadRunner - performance testing tool
- 数据库表中不建索引,在插入数据时,通过sql语句防止重复添加(转载)
- Chapter 8 of C language programming (fifth edition of Tan Haoqiang) is good at using pointer exercises to analyze and answer
- Web Course Design - his system
- Supersocket is Use in net5 - startup
猜你喜欢
2022 Shandong Province safety officer C certificate work certificate question bank and online simulation examination
General testing technology [1] classification of testing
2022G2电站锅炉司炉考试题库及在线模拟考试
[MySQL] left Function | Right Function
Why is bi so important to enterprises?
一套组合拳,打造一款 IDEA 护眼方案
Comprehensive calculation of employee information
Knowledge of software testing~
Configure automatic implementation of curd projects
“如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
随机推荐
Idempotency practice operation, explaining idempotency based on business
There is no index in the database table. When inserting data, SQL statements are used to prevent repeated addition (Reprint)
Course design of Database Principle -- material distribution management system
Supersocket is Use in net5 - startup
IDEA查看历史记录【文件历史和项目历史】
The most easy to understand service container and scope of dependency injection
xutils3修改了我提报的一个bug,开心
Oracle query foreign keys contain comma separated data
批量下载文件----压缩后再下载
MySQL installation pit
Using swagger in. Net5
通过 zxing 生成二维码
Flink customizes the application of sink side sinkfunction
Explication détaillée des fonctions send () et recv () du programme Socket
C-10 program error correction (recursive function): number to character
为什么BI对企业这么重要?
Charles uses three ways to modify requests and responses
ThreadLocal 测试多线程变量实例
socket编程 send()与 recv()函数详解
EasyUI's combobox implements three-level query