添加Lesson6
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
using System;
|
||||
|
||||
namespace Lesson6_泛型约束练习
|
||||
{
|
||||
#region 单例模式基类
|
||||
class Test
|
||||
{
|
||||
private static Test instance = new Test();
|
||||
public int value = 10;
|
||||
public static Test Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
private Test() { }
|
||||
}
|
||||
|
||||
class SingleBase<T> where T : new()
|
||||
{
|
||||
private static T instance = new T();
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class GameManager : SingleBase<GameManager>
|
||||
{
|
||||
public GameManager()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 利用泛型知识点,仿造ArrayList实现一个不确定数组类型的类 并 实现增删查改的方法
|
||||
class ArrayList<T>
|
||||
{
|
||||
private T[] array;
|
||||
|
||||
private int count;
|
||||
public ArrayList()
|
||||
{
|
||||
count = 0;
|
||||
array = new T[16];
|
||||
}
|
||||
public void Add(T value)
|
||||
{
|
||||
if (count >= array.Length)
|
||||
{
|
||||
T[] temp = new T[Capacity * 2];
|
||||
for (int i = 0; i < Capacity; i++)
|
||||
{
|
||||
temp[i] = array[i];
|
||||
}
|
||||
array = temp;
|
||||
}
|
||||
array[count] = value;
|
||||
count++;
|
||||
}
|
||||
|
||||
public void Remove(T value)
|
||||
{
|
||||
int index = -1;
|
||||
for (int i = 0; i < count; i++)//小于实际数量不小于容量
|
||||
{
|
||||
if (array[i].Equals(value))//不能写成array[i] == value因为不是所有类型都重载
|
||||
{
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (index != -1)
|
||||
{
|
||||
for (;index < Count - 1;index++)
|
||||
{
|
||||
array[index] = array[index + 1];
|
||||
}
|
||||
array[Count - 1] = default(T);
|
||||
count--;
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveAt(int index)
|
||||
{
|
||||
if (index < 0 || index >= count)
|
||||
{
|
||||
Console.WriteLine("索引不合法");
|
||||
return;
|
||||
}
|
||||
for (; index < Count - 1; index++)
|
||||
{
|
||||
array[index] = array[index + 1];
|
||||
}
|
||||
array[Count - 1] = default(T);
|
||||
count--;
|
||||
}
|
||||
|
||||
public T this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (index < 0 || index >= count)
|
||||
{
|
||||
Console.WriteLine("索引不合法");
|
||||
return default(T);
|
||||
}
|
||||
return array[index];
|
||||
}
|
||||
set
|
||||
{
|
||||
if (index < 0 || index >= count)
|
||||
{
|
||||
Console.WriteLine("索引不合法");
|
||||
return;
|
||||
}
|
||||
array[index] = value;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 得容量和实际数量
|
||||
/// </summary>
|
||||
public int Capacity
|
||||
{
|
||||
get
|
||||
{
|
||||
return array.Length;
|
||||
}
|
||||
}
|
||||
public int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
ArrayList<int> array = new ArrayList<int>();
|
||||
Console.WriteLine(array.Count);
|
||||
Console.WriteLine(array.Capacity);
|
||||
array.Add(1);
|
||||
array.Add(2);
|
||||
array.Add(3);
|
||||
Console.WriteLine(array.Count);
|
||||
Console.WriteLine(array.Capacity);
|
||||
Console.WriteLine(array[1]);
|
||||
Console.WriteLine(array[-1]);
|
||||
Console.WriteLine(array[5]);
|
||||
array.Remove(2);
|
||||
Console.WriteLine(array.Count);
|
||||
Console.WriteLine("/////////////////");
|
||||
for (int i = 0; i < array.Count; i++)
|
||||
{
|
||||
Console.Write(array[i]);
|
||||
}
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("/////////////////");
|
||||
array.RemoveAt(0);
|
||||
for (int i = 0; i < array.Count; i++)
|
||||
{
|
||||
Console.Write(array[i]);
|
||||
}
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("/////////////////");
|
||||
array[1] = 555;
|
||||
for (int i = 0; i < array.Count; i++)
|
||||
{
|
||||
Console.Write(array[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user