C# 接口interface的学习
2017-02-10 10:36
399 查看
最近看了看C#语法中的接口。记录下学习的内容。
首先是普通的类继承普通的接口。
类显示实现接口成员。
基类实现接口成员。
首先是普通的类继承普通的接口。
using UnityEngine; using System.Collections; using System; using Interface1; namespace Interface1 { //接口可由方法、属性、事件、索引器这4种成员类型任意组合构成,但不能包含字段。 //接口成员不能有定义(代码体) //不能直接实例化接口 //类或结构体可继承多个接口 //接口本身可以继承多个接口 //继承接口的任何非抽象类型都必须实现接口的所有成员 interface MyInterface { string ID //默认访问权限是public,但是不能加修饰符 { set; get; } string name { set; get; } //接口中的方法(函数)不能有定义(实体) void SetID(); } interface MyInterface2 { void Add(); } //类继承接口后,需要实现接口中的所有成员 class MyClass : MyInterface { string id_ = ""; string name_ = ""; //实现接口成员必须有public修饰 public string ID { get { return id_; } set { id_ = value; } } public string name { get { return name_; } set { name_ = value; } } public void SetID() { Debug.Log("MyInterface.SetID"); } } } //类继承多个接口,需要实现所有接口的成员 class MyClass2 : MyInterface, MyInterface2 { string id_ = ""; string name_ = ""; public string ID { get { return id_; } set { id_ = value; } } public string name { get { return name_; } set { name_ = value; } } public void Add() { } public void SetID() { } } public class Interface_Test : MonoBehaviour { // Use this for initialization void Start() { Interface1.MyClass mc = new Interface1.MyClass(); mc.SetID(); } // Update is called once per frame void Update() { } }
类显示实现接口成员。
using UnityEngine; using System.Collections; using System; using Interface2; //显示实现接口成员 //当一个类继承两个接口时,这两个接口中的方法名是完全相同的标识符,而其功能是不相同的。 //为了区分不同接口中相同的标识符的方法,这时就可以显示地实现接口成员,即创建一个仅通过该接口调用并且特定于该接口的类成员。 namespace Interface2 { interface MyInterface { string ID { set; get; } string name { set; get; } void SetID(); } public interface MyInterface2 { string ID { set; get; } string name { set; get; } void SetID(); } //继承的两个接口中的成员标识符完全一样 //显示接口成员实现中不能包含访问修饰符、abstract、override、static、const //显示接口成员属于接口成员,而不是类的成员,不能使用类对象访问,只能通过接口对象访问 class MyClass : MyInterface, MyInterface2 { string id_ = ""; string name_ = ""; //显示地实现MyInterface2中的ID属性 string MyInterface2.ID { get { return id_; } set { id_ = value; } } //显示地实现MyInterface中的ID属性 string MyInterface.ID { get { return id_; } set { id_ = value; } } public string name { get { return name_; } set { name_ = value; } } //显示地实现MyInterface中的SetID方法 void MyInterface.SetID() { Debug.Log("MyInterface.SetID"); } //显示地实现MyInterface2中的SetID方法 void MyInterface2.SetID() { Debug.Log("MyInterface2.SetID"); } } } public class Interface_Test1 : MonoBehaviour { // Use this for initialization void Start () { Interface2.MyClass mc = new Interface2.MyClass(); MyInterface mi = mc; //接口不能直接实例化,这里使用类的对象实例化接口 mi.SetID(); //显示实现接口的成员只能通过接口对象访问 MyInterface2 mc2 = mc; mc2.SetID(); } // Update is called once per frame void Update () { } }
基类实现接口成员。
using UnityEngine; using System.Collections; using System; using Interface3; //基类实现接口成员 namespace Interface3 { interface MyInterface { string ID { set; get; } string name { set; get; } void SetID(); void SetName(); } class MyClass { public virtual void SetName() { Debug.Log("MyClass.SetName"); } } //继承的基类中实现了继承的接口中的成员,意味着派生类隐式地支持这个接口 class MyClass2 : MyClass, MyInterface { string id_ = ""; string name_ = ""; string MyInterface.ID { get { return id_; } set { id_ = value; } } public string name { get { return name_; } set { name_ = value; } } public void SetID() { Debug.Log("MyClass2.SetID"); } } } public class Interface_Test2 : MonoBehaviour { // Use this for initialization void Start () { Interface3.MyClass2 m = new Interface3.MyClass2(); m.SetID(); m.SetName(); } // Update is called once per frame void Update () { } }
相关文章推荐
- C#中接口(interface)的理解
- C#学习笔记(七):接口的执行
- C# 浅谈 接口(Interface)的作用
- C# 浅谈 接口(Interface)的作用
- C#中接口的学习问题(内容来自ProgramFan 本人整理)
- C#中接口(interface)的理解--接口概念的理解
- C#中接口(interface)的理解
- C#学习之接口
- C#学习之接口
- 接口(Interface)的作用,asp.net(C#)
- C#学习笔记(三):抽象类和接口
- C#中的显式接口成员实现(explicit interface member implementation)
- Atlas学习手记(29):JavaScript面向对象的扩展(三):接口Interface
- Java 学习笔记 (3) -抽象类 abstract 接口 interface 内部类 inner class 继承 extends 实现 implements 包 package
- [导入]c#基础:类与接口的学习一
- C#学习笔记(七):接口的执行
- C# 学习笔记(Interface) - 08
- c#基础:类与接口的学习一
- c#基础:类与接口的学习
- C#2005 .NET3.0高级编程学习笔记————接口