c#笔记,十一————接口泛型

1. 接口:

定义IBattle接口、声明攻击Attack(),移动Move(), 跳跃Jump()等方法;

定义IRest接口、声明SitDown(),Sleep()等方法;

定义Soldier(战士)、Master(法师)、Assassin(刺客)、Archer(弓箭手)等类,继承上述接口,并实现内部方法。

2. 定义MyList类,该类模拟一个动态数组,可以用来存放数据(以int类型为例)。实现如下功能:

1)定义属性Count,表示当前动态数组存放的int型元素个数;

2)定义方法Clear(),可以清空所有的元素;

3)定义方法Add(),可以实现添加元素的功能;

4)定义方法Insert(int value, int index),可以实现在某个位置插入元素的功能;

5)定义方法Reverse(),可以实现元素的反转。

6)定义方法Contains(),可以查找元素是否存在。

3. 老板招募小秘

(1)当秘书必须要实现的协议用接口IScretary表示。

要想当秘书,必须能够实现如下方法:

端茶倒水

开车

捶背

提包等

(2)有两类人前来应聘秘书:

男人类 Man

女人类 Woman

机器人类 Robot

请让以上三个类继承秘书协议,并根据每个类的特点实现协议中的方法

(3)在Main方法中分别创建男秘对象和女秘对象,并自行设计模拟情景。

如:有一天老板招了一个男秘,让他干这干那,后来不满意,又招了一个女秘...

再后来科技突飞猛进,老板雇佣了一个不知疲倦聪明又从来不抱怨的机器秘书

2.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

/*2. 定义MyList类,该类模拟一个动态数组,可以用来存放数据(以int类型为例)。实现如下功能:

1)定义属性Count,表示当前动态数组存放的int型元素个数;

2)定义方法Clear(),可以清空所有的元素;

3)定义方法Add(),可以实现添加元素的功能;

4)定义方法Insert(int value, int index),可以实现在某个位置插入元素的功能;

5)定义方法Reverse(),可以实现元素的反转。

6)定义方法Contains(),可以查找元素是否存在。 */

namespace h2

{

interface IInterface

{

}

class MyList< T>

{

//该字段用来记录数组里面所放元素的个数

private int length;

//用来记录数组的容量,即最多可放元素的个数

private int capacity;

//数组用来存取数据

private T [] a;

//在默认构造方法里面给字段赋初值

public MyList()

{

//数组元素的个数初始值为0

length = 0;

//数组容量初始值为8

capacity = 8;

//给数组开辟空间

a = new T [capacity];

}

/// <summary>

/// 属性,得到数组的元素的个数

/// </summary>

public int Count

{

get

{

return length;

}

}

public void Add(T x)

{

//先判断数组的容量是否可以再返给数据

if (length>=capacity)

{

//如果不能再放入数据,扩充数组容量

}

//把要加的数据方法放到数组里面

a[length] = x;

//数组元素的个数要加1

length++;

}

/// <summary>

/// 扩充数组容量的方法

/// </summary>

private void EnlargeCapacity()

{

//把容量值增加

capacity += 8;

//根据增加后的容量创建新数组

T[] temp = new T[capacity];

//把a数组里面的元素复制到新数组里面

for (int i = 0; i < length; i++)

{

temp[i] = a[i];

}

//把a指向新数组

a =temp;

}

/// <summary>

/// 清空数组

/// </summary>

public void Clear()

{

//数组元素为0

length = 0;

//数组容量为8

capacity = 8;

//创建一个新的数组

a = new T [capacity];

}

public bool Contains(int x)

{

for (int i = 0; i < length; i++)

{

if (x.Equals(a[i]))

{

return true ;

}

}

return false ;

}

//数组的反转

public void Reverse()

{

for (int i = 0; i < length/2; i++)

{

T temp = a[i];

a[i] = a[length - 1 - i];

a[length - 1 - i] = temp;

}

}

/// <summary>

/// 插入

/// </summary>

/// <param name=" index"></param>

/// <param name=" num"></param>

public void Insert(int index, T num)

{

if (length>=capacity)

{

EnlargeCapacity();

}

//如果插入的位置在数组之外

if (index<0)

{

Console.WriteLine("index不能小于0" );

return;

}

//如果插入的位置刚好在所有元素的最后面

else if (index ==length)

{

a[index]= num;

length += 1;

}

else if (index >= length + 1)

{

}

else

{

//把数组里面index之后的所有元素往后面移动一位

for (int i = length-1; i >=index; i--)

{

a[i + 1] = a[i];

}

a[index] = num;

length += 1;

}

}

/// <summary>

/// 打印数组

/// </summary>

public void PrintArray()

{

//使用for循环把存到数组a里面的元素打印出来

for (int i = 0; i < length; i++)

{

Console.WriteLine(a[i]);

}

}

}

class Program

{

static void Main(string[] args)

{

MyList<int > list = new MyList< int>();

list.Add(5);

list.Add(6);

list.Add(7);

list.PrintArray();

list.Insert(1, 9);

list.PrintArray();

Console.ReadKey();

}

}

}