- 接口:
定义一个接口在语法上跟定义一个抽象类完全相同,但不允许提供接口中任何成员的实现方式,一般情况下,接口只能包含方法,属性,索引器和事件的声明。
接口不能有构造函数,也不能有字段,接口也不允许运算符重载。
接口定义中不允许声明成员的修饰符,接口成员都是公有的。
//定义接口(飞翔功能)
public interface IFlyHandler{
public void Fly();
}
//实现接口
public class TypeEnemy:IFlyHandler{
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 接口
{
class Program
{
static void Main(string[] args)
{
//Plane p = new Plane();
//p.Fly();
//p.FlyAttack();
//下面的方式是用接口来声明一个对象。
//这里是多态,fly的形态发生了变化,前面是飞机后面是小鸟。
IFly fly;
fly = new Plane();
fly.Fly();
fly.FlyAttack();
fly = new Bird();
fly.Fly();
fly.FlyAttack();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 接口
{
interface IFly
{
void Fly();//这里默认是public函数。
void FlyAttack();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 接口
{
class Plane : IFly
{
public void Fly()
{
Console.WriteLine("飞机在空中飞");
}
public void FlyAttack()
{
Console.WriteLine("在空中攻击");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 接口
{
class Bird : IFly
{
public void Fly()
{
Console.WriteLine("小鸟在空中飞");
}
public void FlyAttack()
{
Console.WriteLine("小鸟在空中攻击");
}
}
}
接口的继承:
派生的接口:接口可以彼此继承,其方式和类的继承方式相同。
public interface A
{
void Method1();
}
public interface B:A
{
void Method2();
}
- 索引器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 接口
{
class Program
{
static void Main(string[] args)
{
//int[] array = { 34,567,432,4,32};
//array[1] = 100;
//Console.WriteLine(array[1]);
// Test t = new Test();
t[9] = 200;
// int temp = t[9];
// Console.WriteLine(t[9]);
// Console.WriteLine(temp);
Test t = new Test();
t[0] = "1";
t[1] = "2";
Console.WriteLine(t[0]);
Console.WriteLine(t[1]);
string[] name = new string[10];
name[0] = "3";
name[1] = "4";
Console.WriteLine(name[0]);
Console.WriteLine(name[1]);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 接口
{
class Test
{
private string[] name = new string[10];
public string this[int index]//定义了索引器
{
get//如果是取值就用get
{
return name[index];
}
set//如果是赋值就用set
{
name[index] = value;
}
}
}
}
星期几转换器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 接口
{
class Program
{
static void Main(string[] args)
{
Week w = new Week();
Console.WriteLine(w.GetDay("Thurs"));
Console.WriteLine(w["Sun"]);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 接口
{
class Week
{
private string[] days = {"Mon","Thus","wed","Thurs","Fri","Sat","Sun" };
public int GetDay(string day)
{
int i = 0;
foreach (string temp in days)
{
if (temp == day) return i+1;
i++;
}
return -1;
}
//这里使用索引器,实现和上面同样的功能。
public int this[string day]
{
get
{
return GetDay(day);
}
}
}
}
- 运算符重载:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Student
{
private int age;
private string name;
private long id;
public Student(int age, string name, long id)
{
this.age = age;
this.name = name;
this.id = id;
}
public static bool operator==(Student s1, Student s2)
{
if (s1.age == s2.age&&s1.name == s2.name && s1.id==s2.id)
{
return true;
}
return false;
}
public static bool operator!=(Student s1, Student s2)
{
bool result = s1 == s2;
return !result;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Student s1 = new Student(20,"张三",12903);
Student s2 = new Student(20, "李四", 12903);
Student s3 = s1;
Console.WriteLine(s1==s2);
Console.WriteLine(s1==s3);
Console.WriteLine(s1!=s2);
}
}
}
- 结构体和类的区别:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Student//在堆里面存放
{
public int age;
public string name;
public Student(int age, string name)
{
this.age = age;
this.name = name;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
struct StudentSt//在栈里面存放
{
public int age;
public string name;
public StudentSt(int age, string name)
{
this.age = age;
this.name = name;
}
}
static void Main(string[] args)
{
//Student s1 = new Student(12,"张三");
//Student s2 = new Student(20, "李四");
//s2 = s1;
//s1.age = 30;
//s1.name = "小叶";
//Console.WriteLine(s2.age);
//Console.WriteLine(s2.name);
StudentSt s1 = new StudentSt(18,"小芳");
StudentSt s2 = s1;
s2.age = 30;
s2.name = "小叶";
Console.WriteLine(s2.age);
Console.WriteLine(s2.name);
}
}
}