单例:
1私有化构造方法
2类的内部提供一个静态实例-instance
3提供获取内部的实例方法(这种实例方法用对象调用,静态可以用类名直接调用)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Player
{
public string name;
public int level;
public int experience;
public int experiencemax;
public int Hp;
private Player() { }
private static Player _instance;
public static Player Get_instance()
{
if (null == _instance)
{
_instance = new Player();
}
return _instance;
}
}
class Project
{
public void Exadd(Player a)
{
a.experience = a.experience + 9;
if (a.experience <= a.experiencemax)
Console.WriteLine("经验+9");
if (a.experience % 10 == 0)
{
a.level++;
Console.WriteLine("等级加一,成为LEVEL" + a.level);
}
}
public void Attack(Player b)
{
if (b.Hp > 0)
{
b.Hp--;
Console.WriteLine("Hp - 1");
}
else Console.WriteLine("角色已死亡");
}
}
class MainClass
{
public static void Main(string[] args)
{
Player He = Player.Get_instance();
He.Hp = 20;
He.level = 1;
He.experience = 1;
He.experiencemax = 50;
He.name = "张艺兴";
Project c = new Project();
c.Attack(He);
c.Exadd(He);
Console.ReadKey();
}
}
}
方法替换:子类继承父类后,可以继承父类的方法,在子类中重现实现,也可以隐藏父类方法,重新在子类实现。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Dad
{
public void hu()
{
Console.WriteLine("在dad中定义的");
}
public void By()
{
Console.WriteLine("在dad中定义的2");
}
}
class Son:Dad
{
public void Js()
{
Console.WriteLine("在son中定义");
}
public new void By()
{
Console.WriteLine("在son中定义的2");
}
}
class MainClass
{
public static void Main(string[] args)
{
Dad p = new Son();
p.By();
p.hu();
Son y = new Son();
y.hu();
y.Js();
y.By();
Console.ReadKey();
}
}
}
索引器
泛型类:使用多种数据类型。
一:索引器
namespace ConsoleApp3
{
class Make
{
//索引器
public int this[int index]
{
set
{
_arr[index] = value;
}
get
{
return _arr[index];
}
}
private int[] _arr;
public int Count=0;
public Make()
{
_arr = new int[100];
}
public void Addnum(int value)
{
_arr[Count] = value;
Count++;
}
public void Putaudience()
{
Console.Write("一共有"+Count+"个元素:(");
for(int i = 0; i < Count; i++)
{
Console.Write (_arr[i]);
if (i != Count - 1)
{
Console.Write(",");
}
else Console.Write(")");
}
Console.WriteLine();
}
}
class MainClass
{
public static void Main(string[] args)
{
Make p = new Make ();
p.Addnum(54);
p.Putaudience();
p.Addnum(67);
p.Putaudience();
p.Addnum(23);
p.Putaudience();
p[0] = 11;
Console.WriteLine("arr[1]=" + p[0]);
Console.ReadKey();
}
}
}
二、泛型。所有的要用到的数据类型都要改成T,泛型的代码中不能用算术运算符,因为类型不确定,可能是自己的定义的类型等等,不能进行大小或者其他比较。
T S U V K
改动的代码
namespace ConsoleApp3
{
class Make<T>
{
//索引器
public T this[int index]
{
set
{
_arr[index] = value;
}
get
{
return _arr[index];
}
}
private T [] _arr;
public int Count=0;
public Make()
{
_arr = new T [100];
}
public void Addnum(T value)
{
_arr[Count] = value;
Count++;
}
public void Putaudience()
{
Console.Write("一共有"+Count+"个元素:(");
for(int i = 0; i < Count; i++)
{
Console.Write (_arr[i]);
if (i != Count - 1)
{
Console.Write(",");
}
else Console.Write(")");
}
Console.WriteLine();
}
}
Make<int > p = new Make<int > ();
p.Addnum(54);