interfaces

直接进入我们精彩的内容

==接口与抽象类类似,都无法申明变量

它们的区别与相似之处:

Abstract Class Vs. Interface

ØInterfaces are very similar to abstract classes.

ØC# doesn’t allow multiple inheritance with classes.

ØC# does allow to implement any number of interfaces and derive from one base class.

一个类可以有多个接口,但是必须实现接口里的所有函数

来举个例子:

namespace ConsoleApplication1
{
    class Program
    {

        public interface IStorable
        {
            void Read();
            void Write();
        }

        interface ICompressible
        {
            void Compress();
            void Decompress();
        }
        interface ILoggedCompressible : ICompressible
        {
            void LogSavedBytes();
        }

        public class Document : IStorable, ICompressible
        {
            public void Read()
            {
                Console.WriteLine("read");
            }
            public void Write()
            {
                Console.WriteLine("write");
            }
            public void Compress()
            { }
            public void Decompress()
            { }

        }

        static void Main(string[] args)
        {
            Document document = new Document();
            document.Write();

        }
    }
}

  //类里的函数必须public

接下来介绍显式与隐式接口里函数的定义

增加了一个新的接口:

interface ITalk
{
    void Talk();
    void Read();
}

实现显式定义:

void ITalk.Read()
            {
                Console.WriteLine("reeeead");
            }

//这里不加public

隐式加virtual

        static void Main(string[] args)
        {
            Document firstDocument = new Document();
            firstDocument.Read();
            ITalk secondDocument = firstDocument;
            secondDocument.Read();

        }

  

显示定义的接口方法,不能有abstract, virtual, override, or new 修饰符。

因为显示实现的接口成员不能继承。

最后我感觉接口的使用要比抽象类方便很多。

时间: 2024-10-05 19:21:41

interfaces的相关文章

disable NetworkManager and boot on static ip configuration using /etc/network/interfaces

1.stop and disable NetworkManager sudo systemctl stop NetworkManager sudo systemctl disable NetworkManager 2.drop status of NetworkManger sudo mv /var/lib/NetworkManager/NetworkManager.state /var/lib/NetworkManager/NetworkManager.state.bak 3.edit int

OpenERP-Java调用XML-RPC接口示例(Examples for calling XML-RPC interfaces by Java)

官网没有给出CREATE.SEARCH.WRITE等XML-RPC接口的Java 调用示例,在此补充一下. There is no examples on the official site for the XML-RPC operation interfaces for Java, so I posted my code here. /**  * Created by kylin on 14-9-22.  */ import org.apache.xmlrpc.XmlRpcException;

Not configured to listen on any interfaces!

dhcp错误,日志如下 an  2 02:31:33 lylinux dhcpd: No subnet declaration for eth0 (192.168.88.128). Jan  2 02:31:33 lylinux dhcpd: ** Ignoring requests on eth0.  If this is not what Jan  2 02:31:33 lylinux dhcpd:    you want, please write a subnet declaration

[TypeScript ] What Happens to Compiled Interfaces

This lesson covers using your first TypeScript Interface and what happens to the Interface when it is compiled down to JavaScript. Define the interfaces: // interfaces.ts export interface Person { name: String } export interface SocialNetwork{ title:

SQL Network Interfaces, error: 50 - 发生了 Local Database Runtime 错误。无法创建自动实例。

今天在用VS2013自带的LocalDB调整数据库时出错,在网上也搜到许多方案,如卸载SQLServer LocalDB的程序.重新创建实例等都没有解决我的问题,也重新修改以及修复Vs,问题依旧存在,仔细思索研究后找到了解决方案: 1.先贴问题(注:我这里是LocalDB V11.0不能使用): Microsoft Visual Studio --------------------------- 尝试附加到数据库失败并出现以下信息: 在与 SQL Server 建立连接时出现与网络相关的或特定

[转]network-manager与interfaces冲突

http://blog.sina.com.cn/s/blog_48a45b9501010681.html 网络配置的两种方式 Ubuntu下修改网络配置有两种方式:图形界面方式(network-manager)和修改/etc/network/interfaces 但是如果两种方式的网络设置不同,就会产生冲突,即:network-manager与/etc/network/interfaces文件冲突. Server版本 在Ubuntu Server版本中,因为只存有命令行模式,所以要想进行网络参数

网卡配置/etc/network/interfaces

话说Debian系的网卡配置跟Redhat系很不一样,Redhat是放在/etc/sysconfig/network-scripts目录下面的一大堆文件里面,要修改?你一个一个文件来过吧.Debian系的则是存在/etc/network/interfaces文件里面,无论有多少块网卡,统统扔在这个文件里.下面就来看一下这个文件的内容. 首先,一个基本的配置大概是下面这个样子: 1 auto lo 2 iface lo inet loopback 3 4 # The primary network

Part 30 to 31 Talking about Interfaces in c#

Part 30 Interfaces in c# We create interfaces using interface keyword. just like classes interfaces also contains properties, methods, delegates or events, but only declarations and no implementations. It is a compile time error to privide implementa

Go Methods and Interfaces

[Go Methods and Interfaces] 1.Go does not have classes. However, you can define methods on struct types. The method receiver appears in its own argument list between the func keyword and the method name. 2.You can declare a method on any type that is

Chapel 2.Network interfaces

Note: Copy from TCP/IP Tutorial and Technical Overview (IBM Redbook GG24-3376-07)[000] 2.1 Ethernet and IEEE 802 local area networks (LANs) 2.1.1 Gigabit Ethernet 2.2 Fiber Distributed Data Interface (FDDI) 2.3 Serial Line IP (SLIP) 2.4 Point-to-Poin