class Program
{
static void Main(string[] args)
{
HeroClass Hero = new HeroClass("项羽", "男", 1234, 2348);
Hero.ShowHero();
MasterClass mas = new MasterClass(Hero);
HeroClass.get();//有 static 只能属于类;不能通过实例名称点出来;
Console.WriteLine(Hero.duanwei);//通过类名中的名字点出来;实例化的变量;
Hero.SetHero("张三丰", "男", 1, 2, 3, 4);
Hero.ShowHero();
int[] myArr = { 123, 456, 678, 987 };
Hero.SetHero("张三丰", "男", myArr);//利用关键字 params 可以传入数组
Hero.ShowHero();
string name1 = "";
int zhanli = Hero.Getzhanli(ref name1);
Console.WriteLine("名字{0} 战力{1}", name1, zhanli);
string names = "";
int zhanlis = Hero.Getzhanli(ref names);
Console.WriteLine(names);
int mofa;
Hero.GetMofa(out mofa);
Console.WriteLine(mofa);
int mofa;
Hero.GetMofa(out mofa);
Console.WriteLine(mofa);
//MasterClass mas = new MasterClass();
MasterClass.Getmas();//把实例化去掉,同样执行静态构造函数
StaticClass str = new StaticClass(); 静态类不能实例化
StatcClass sta = new StatcClass();
StatcClass.Str();
Console.ReadKey();
}
}
class StaticClass
{
//静态类 一定是通过关键字static 修饰;
private static string str = "";
//静态类 的成员一定为静态的 不能有一个实例成员
public static void GetStr()
{
Console.WriteLine(str);
}
}
class StaticsClass
{
private static string str;
public static void Str()
{
str = "123";
Console.WriteLine(str);
}
}
class MasterClass
{
public MasterClass(HeroClass hero)
{
hero.Zhanli = 666;
hero.Mofazhi = 888;
hero.ShowHero();
}
//static MasterClass() {//静态构造函数不能传参;而且有且只执行一次;默认隐式调用;
// Console.WriteLine("执行了静态构造函数!");
//}
public static void Getmas() {
Console.WriteLine("普通方法!");
}
}
class HeroClass
{
//常量
public readonly int duanwei = 6;//段位-只读的
private string name;
private string sex;
private int zhanli;
private int mofazhi;
public string Name
{
set { name = value; }
get { return name; }
}
public string Sex
{
set { sex = value; }
get { return sex; }
}
public int Zhanli
{
set { zhanli = value; }
get { return zhanli; }
}
public int Mofazhi
{
set { mofazhi = value; }
get { return mofazhi; }
}
public HeroClass(string _name, string _sex, int _zhanli, int _mofazhi)
{
this.name = _name;
this.sex = _sex;
this.zhanli = _zhanli;
this.mofazhi = _mofazhi;
duanwei = _zhanli;//只读字段只能在构造函数更改;
}
public void ShowHero()
{
Console.WriteLine("英雄{0} 性别{1} 战力{2}魔法值{3} ", name, sex, zhanli, mofazhi);
//duanwei = 333; 只读字段不能在普通方法里更改;只能在构造函数更改
}
public static void get()
{//注意 static 字眼
Console.WriteLine("666");
}
//params 关键字 参数为数组形式;如果有多个则放到后面 经常用到在方法的参数不确定情况下
public void SetHero(string _name, string _sex, params int[] Arr)
{
this.name = _name;
this.sex = _sex;
this.zhanli = Arr[0];
this.mofazhi = Arr[1];
int[] arr = new int[Arr.Length];
for (int i = 0; i < Arr.Length; i++)
{
arr[i] = i + 1;
}
}
//--------ref关键字-----------
public int Getzhanli(ref string _name)
{
_name = "英雄名称" + name;
return zhanli;
}
public void GetzhanliS(ref string _name)
{
_name = "英雄名称" + name;
}
//------out关键字--------
//public void GetMofa(out int _mofazhi) {
// _mofazhi = mofazhi;
//}
public void GetMofa(out int _mofazhi)
{
_mofazhi = mofazhi;
}
}