脚本宝典收集整理的这篇文章主要介绍了C#.Net 接口,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
class PRogram
{
static void Main(string[] args)
{
}
}
//接口不能实例化
//接口就是让子类来实现的。
//1.接口可以实现“多继承”(多实现),一个类只能继承自一个父类,但是可以实现多个接口。
//2.接口解决了不同类型之间的多态问题,比如鱼与船不是同一类型,但是都能在水里“游泳”,
//只是万式不一样,要对游泳”实现多态,就只能考虑接口。
//定义一个接口,建议:一定要以大写 I 开头
public interface IFlyable
{
//接口里面能包含什么成员,接口里面只能包含【方法】,
//(属性,索引器,事件)
//归根结底是只包含 [方法]
//接口中的所有成员,都不能显示的写任何访问修饰符
//默认是pub1ic的访问修饰符
void SayHi();
void M1(string msg);
//在接口中这样写表示是一个未实现的属性。
string Name {
get;
set;
}
//索引器
string this [int index] {
get;
set;
}
}
//接口中的成员,子类必须实现
public class Myclass : IFlyable
{
string name;
public void Sayhi()
{
Console.WrITeLine("Hi...");
}
public void M1(string msg)
{
Console.WriteLine(msg + "Hi...");
}
public string Name {
get { return name; }
set { name = value; }
}
public string this[int index] {
get { }
set { }
}
}
public
接口中可以有属性、方法、索引器等(其实都是方法),但不能有字段namespace xueXi_接口
{
class Class1
{
static void Main(string[] args)
{
IFlyable bird = new Parrot()/*Sparrow()*/;
bird.Fly();
}
}
// 鸟
public class Bird
{
public void brak()
{
Console.WriteLine("叫....");
}
}
public interface IFlyable
{
void Fly();
}
// 麻雀
// 当一个类同时继承父类,并且实现了多个接口的时候,必须将继承类,写在第一个。
public class Sparrow : Bird, IFlyable
{
public void Fly()
{
Console.WriteLine("麻雀飞...");
}
}
// 鸵鸟
public class Ostrich : Bird
{
}
// 企鹅
public class Penguin: Bird
{
}
// 鹦鹉
public class Parrot : Bird, IFlyable
{
public void Fly()
{
Console.WriteLine("鹦鹉飞...");
}
}
}
namespace xueXi_接口
{
class Class2
{
static void Main()
{
ICollectHomework person = new Teacher()/*Student()*/;
person.Collect();
}
}
public interface ICollectHomework
{
void Collect();
}
public class Person
{
}
public class Student : Person, ICollectHomework
{
public void Collect()
{
Console.WriteLine("学生收作业...");
}
}
public class Teacher : Person, ICollectHomework
{
public void Collect()
{
Console.WriteLine("老师收作业...");
}
}
public class SchoolMaster : Person { }
}
DengJi(Person person)
//人口登记,Person
是抽象类DengJi(Car car)
//汽车也要登记DengJi(House house)
//房子也要登记.JiKou
DengJi(lJieKou jk);
namespace xuexi_接口练习
{
class Program
{
static void Main(string[] args)
{
German g = new German();
Car car = new Car();
Dengji(g);
Dengji(car);
}
public static void Dengji(IShowInfo dengji)
{
dengji.show();
}
}
public interface IShowInfo
{
void show();
}
class Chinese { }
class Americn { }
class German : IShowInfo
{
public void show()
{
Console.WriteLine("德国人....");
}
}
class Car : IShowInfo
{
public void show()
{
Console.WriteLine("兰博基尼...");
}
}
}
IL
是private
的,防止通过类来调用)接口名.方法名()
,并且没有访间修饰符,private
private
。
namespace xuexi_显示接口
{
class Program
{
static void Main(string[] args)
{
IFace1 stu = new Student();
stu.Fly();
IFace2 stu1 = new Student();
stu1.Fly();
IFace1 stu3 = new Teacher();
stu3.Fly();
IFace2 stu4 = new Teacher();
stu4.Fly();
}
}
interface IFace1
{
void Fly();
}
interface IFace2
{
void Fly();
}
class Student : IFace1, IFace2
{
public void Fly()
{
Console.WriteLine("Iface1");
}
}
internal class Teacher : IFace1, IFace2
{
public void Fly()
{
Console.WriteLine("1");
}
void IFace2.Fly() // 只能私有,通接口调用
{
Console.WriteLine("2");
}
}
}
public
,不能修改。(默认为public
)父类A
,并实现了接口IA
,那么语法上A
必须写在IA
的前面。class MyClass:A, IA {}
,因为类是单继承的。abstract
。(抽象类也能实现接口,用abstrac
标记)private
)。private
interface
)
reflector
查看源码)virtual
和override
?
Person per = new Student();
per.SasHI()
调用的子类重写的SayHi
方法(语法、应用(多态))abstract
和override
?overload
”、“方法重写override
”、"隐藏new
”是同一个概念吗?virtual
: 是用来标记虚方法的。
override
: 是进行方法重写的,当子类继承父类的时候重写父类的方法。
参考: 1.link-01 // B站 -> C#.Net基础加强第四天-接口与异常处理 -> 传智播客
以上是脚本宝典为你收集整理的C#.Net 接口全部内容,希望文章能够帮你解决C#.Net 接口所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。