using System;
class shuxing
{
 private string m_name;
 private string m_sex;
 public void Setm_name(string name)
 {
  m_name=name;
 }
 public string getm_name()
 {
  return m_name;
 }
 public void Setm_sex(string sex)
 {
  if(sex==”男”||sex==”女”)
   m_sex=sex;
  else
   Console.WriteLine(“性别必须为男或女”);
 }
 public string getm_sex()
 {
  return m_sex;
 }
 public string Name//属性
 {
  set
  {
   m_name=value;
  }
  get
  {
   return m_name;
  }
 }
 public string Sex//属性
 {
  set
  {
   if(value==”男”||value==”女”)
    m_sex=value;
   else
    Console.WriteLine(“性别必须为男或女”);
  }
  get
  {
   return m_sex;
  }
 }
}
class text
{
 static void Main()
 {
  shuxing pp=new shuxing();
  pp.Setm_name(“刘德华”);
  pp.Setm_sex(“男”);
  Console.WriteLine(pp.getm_name()+”   “+pp.getm_sex());
  
  pp.Name=”李纹”;
  pp.Sex=”无”;//输出性别为男,因为是同一个对象,前面赋值为男。
  Console.WriteLine(“姓名   “+pp.Name+” 性别”+pp.Sex);

数据连接类

访问修饰符:
pubulc:公共的,只要引用了命名空间,就可以随意进行访问
private:私有的,只有当前类内部才可以访问

  shuxing qq=new shuxing();
  qq.Name=”李纹”;
  qq.Sex=”无”;
  Console.WriteLine(“姓名   “+qq.Name+” 性别”+qq.Sex);
  
 }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

internal:内部的,当前程序集内可以访问,程序集就是命名空间,此修饰符是默认的
protected:被保护的,当前类和它的子类才可以访问

namespace 对战
{
public class DZConnect
{
private static string connstring =
“server=.;database=duizhan;user=sa;pwd=123”;

命名空间:
也叫程序集,项目中每一个文件夹都是一个独立的命名空间
如果要使用某一个命名空间下的类文件,那么必须首先引用此命名空间

public static SqlConnection Conn
{
get
{
return new SqlConnection(connstring);
}
}
}
}


实体类

澳门葡萄京官方网站,封装;
打包,封起来,保护起来

 

private int _Ids; –
私有的成员变量

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public int Ids –
属性,封装基本结构
{
get{ return _Ids; }
set{ _Ids = value; }
}

 

一个成员变量可以有很多个属性

namespace 对战
{
public class DZ
{
private int code;

属性返回类型可以是任意类型,不是必须要与成员变量类型相同

 

属性可以是只读的,也可以是只写的,也可以两者都有

public int Code
{
get { return code; }
set { code = value; }
}


 

namespace
ConsoleApplication46

{
public class Student
{
private string _Code;
private string _Name;
private decimal _Score;
private DateTime _Birthday;
private string _Sex;

private string name;

public string Sex
{
get { return _Sex;}
set
{
if (value == “男” || value ==
“女”)
{
_Sex = value;
}
else
{
_Sex = “您的输入有误”;
}
}
}

 

public DateTime Birthday
{
get { return _Birthday; }
set { _Birthday = value; }
}

public string Name
{
get { return name; }
set { name = value; }
}
private bool sex;

public string BirthdayStr
{
get { return
_Birthday.ToString(“yyy年MM月dd日”); }

 

}

public bool Sex
{
get { return sex; }
set { sex = value; }
}
private int xue;

 

 

 

public int Xue
{
get { return xue; }
set { xue = value; }
}
private int gong;

public decimal Score
{
get { return _Score; }
set{
if (value >= 0 && value <=
100)
{
_Score = value;
}
else
{
_Score=0;
}

 

}

public int Gong
{
get { return gong; }
set { gong = value; }
}
private int fang;

}

 

}
}

public int Fang
{
get { return fang; }
set { fang = value; }
}
private int zhong;

 

 

 

public int Zhong
{
get { return zhong; }
set { zhong = value; }
}
private int shan;

namespace
ConsoleApplication46

{
class Program
{
static void Main(string[]
args)
{
Student s = new Student();
s.Score = 90.5m;
s.Birthday = DateTime.Now;

 

s.Sex = “男”;
Console.WriteLine(s.Sex);

public int Shan
{
get { return shan; }
set { shan = value; }
}

Console.WriteLine(s.BirthdayStr);
Console.ReadKey();

 


}

继承:
子类可以继承父类所有的public方法和属性

 

父类不可以调用子类的方法和属性,哪怕是public的

}

子类可以转换成父类,转换成的这个父类也可以转换回相应子类

数据访问类

子类转换成父类,这个父类不可以转换成其它的子类

 

父类

 

子类 – 某个类的子类,派生类,超类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace 对战
{
class DZDA
{
private SqlConnection _conn;
private SqlCommand _cmd;
private SqlDataReader _dr;

Plane这个类是Fly的派生类,
Plane这个类派生自Fly

public DZDA()
{
_conn = DZConnect.Conn;
_cmd = _conn.CreateCommand();
}


public bool Add(string code, string name)
{

namespace 继承
{
class Program
{
static void Main(string[]
args)
{
Fly f = new Fly();
string s = f.Fly1();
Console.WriteLine(s);

int seed = (int)Convert.ToChar(name.Substring(0, 1)) +
(int)Convert.ToChar(name.Substring(0, 1));
Random rand = new Random();
bool sex = true;
int xue = 800 + rand.Next(100);
int gong = 300 + rand.Next(100);
int fang = 10 + rand.Next(20);
int zhong = rand.Next(80) + 50;
int shan = rand.Next(80) + 10;

Bird b = new Bird();
string bb = b.Fly1();
Console.WriteLine(bb);

_cmd.CommandText = “insert into DZ
values(@code,@name,@sex,@xue,@gong,@fang,@zhong,@shan)”;
_cmd.Parameters.Clear();
_cmd.Parameters.AddWithValue(“@code”, code);
_cmd.Parameters.AddWithValue(“@name”, name);
_cmd.Parameters.AddWithValue(“@sex”, sex);
_cmd.Parameters.AddWithValue(“@xue”, xue);
_cmd.Parameters.AddWithValue(“@gong”, gong);
_cmd.Parameters.AddWithValue(“@fang”, fang);
_cmd.Parameters.AddWithValue(“@zhong”, zhong);
_cmd.Parameters.AddWithValue(“@shan”, shan);

Plane p = new Plane();
string pp = p.Fly1();

_conn.Open();
int n = _cmd.ExecuteNonQuery();
_conn.Close();
if (n > 0)
{
return true;
}
else
{
return false;
}
}
public List<DZ> Select()
{
_cmd.CommandText = “select * from DZ”;
_conn.Open();
_dr = _cmd.ExecuteReader();

Console.WriteLine(pp);

List<DZ> list = new List<DZ>();

Console.ReadLine();

if (_dr.HasRows)
{
while (_dr.Read())
{
DZ data = new DZ();
data.Code = (int)_dr[0];
data.Name = _dr[1].ToString();
data.Sex = Convert.ToBoolean(_dr[2]);
data.Xue = (int)_dr[3];
data.Gong = (int)_dr[4];
data.Fang = (int)_dr[5];
data.Zhong = (int)_dr[6];
data.Shan = (int)_dr[7];

 

list.Add(data);
}

namespace 继承
{
public class Fly
{
public string Fly1()
{
return “我可以飞行”;
}
}
}

}
_conn.Close();

 

return list;

namespace 继承
{
public class Bird:Fly
{

}

}

public List<DZ> Select( string name)
{
_cmd.CommandText = “select * from DZ where Name=@name”;
_cmd.Parameters.Clear();
_cmd.Parameters.AddWithValue(“@name”, name);
_conn.Open();
_dr = _cmd.ExecuteReader();

 

List<DZ> list = new List<DZ>();

namespace 继承
{
public class Plane:Fly
{

if (_dr.HasRows)
{
while (_dr.Read())
{
DZ data = new DZ();
data.Code = (int)_dr[0];
data.Name = _dr[1].ToString();
data.Sex = Convert.ToBoolean(_dr[2]);
data.Xue = (int)_dr[3];
data.Gong = (int)_dr[4];
data.Fang =(int)_dr[5];
data.Zhong = (int)_dr[6];
data.Shan = (int)_dr[7];

}
}

list.Add(data);
}

 

}
_conn.Close();

强转

return list;

Bird b =new Bird();

}

Fly f =(Fly)b;        //b这个类
强制转换成父类 fly ,转换后 不能用bird类的 属性

}
}

Bird bb =(Bird)f;      //
 转换成的这个父类 也可以转换回去,(转回到相应的子类)

主函数

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace 对战
{
class Program
{
static DZ at(DZ d_1, DZ d_2)
{
Random rand = new Random();
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(d_1.Name + “准备发起攻击····”);
Console.ForegroundColor = ConsoleColor.Black;

System.Threading.Thread.Sleep(1000);
int m2 = rand.Next(100) + d_1.Zhong;
int s1 = rand.Next(100) + d_2.Shan;
if (m2 > 100)
{
if (s1 > 150)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(d_2.Name + “躲开了” + d_1.Name + “的攻击”);
Console.ForegroundColor = ConsoleColor.Black;
}
else
{
int a = rand.Next(200) + d_1.Gong;
d_2.Xue = d_2.Xue – a + d_2.Fang;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(d_1.Name + “攻击掉” + d_2.Name + (a – d_2.Fang) +
“血”);
Console.ForegroundColor = ConsoleColor.Black;
}
}
else
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(d_1.Name + “攻击失误”);
Console.ForegroundColor = ConsoleColor.Black;
}
return (d_2);
}
static void Main(string[] args)
{
Console.WriteLine(“1是添加,2是战斗”);
string c = Console.ReadLine();
if (c == “1”)
{
DZDA zz = new DZDA();
int i = 0;
while (i < 1)
{
Console.WriteLine(“请输入代号:”);
string code = Console.ReadLine();
Console.WriteLine(“请输入名字:”);
string name = Console.ReadLine();

if (zz.Add(code, name))
{
Console.WriteLine(“输入成功!”);
}
else
{
Console.WriteLine(“输入失败!”);
}
i++;
Console.WriteLine(“是否继续添加,继续添加选择1,停止选择2”);
int n = int.Parse(Console.ReadLine());
if (n == 1)
{
i–;
}
else
{
Console.WriteLine(“全部添加完毕”);
}
}
Console.WriteLine(“回车显示添加数据”);
Console.ReadLine();

List<DZ> list = zz.Select();

foreach (DZ data in list)
{
string sexname=data.Sex?”男”:”女”;

Console.WriteLine(data.Code + “–” + data.Name + “–” + sexname + “–” +
data.Xue + “血值:” + data.Gong + “攻击:” + data.Fang + “防御:” +
data.Zhong + “命中:” + data.Shan + “闪避:”);
}
Console.ReadLine();
}
else if (c == “2”)
{
DZDA zz = new DZDA();
int j = 0;
while (j < 1)
{
Console.WriteLine(“请输入第一个玩家姓名:”);
string Name_1 = Console.ReadLine();
List<DZ> list1 = zz.Select(Name_1);
Console.WriteLine(“*************”);
Console.WriteLine(“请输入第二个玩家姓名:”);
string Name_2 = Console.ReadLine();
List<DZ> list2 = zz.Select(Name_2);
Console.WriteLine(“*************”);
while (list1[0].Xue > 0 && list2[0].Xue > 0)
{
DZ pp2 = at(list1[0], list2[0]);
list2[0] = pp2;
DZ pp1 = at(list2[0], list1[0]);
list1[0] = pp1;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(list2[0].Name + “:” + (list2[0].Xue < 0 ? 0 :
list2[0].Xue) + “**********” + (list1[0].Name + “:” +
(list1[0].Xue < 0 ? 0 : list1[0].Xue)));
//Console.WriteLine(“aa”);
Console.WriteLine();
Console.WriteLine();
System.Threading.Thread.Sleep(2000);
}
Console.WriteLine(“———战斗结束————“);
Console.WriteLine();
if (list1[0].Xue <= 0 && list2[0].Xue > 0)
{
Console.WriteLine(list2[0].Name + “赢了”);
}
else if (list2[0].Xue <= 0 && list1[0].Xue > 0)
{
Console.WriteLine(list1[0].Name + “赢了”);
}
else if (list1[0].Xue <= 0 && list2[0].Xue <= 0)
{
Console.WriteLine(list2[0].Name + “同归于尽”);
}
j++;
Console.WriteLine(“需要继续战斗吗?继续:1,否:2”);
int d = int.Parse(Console.ReadLine());
if (d == 1)
{
j–;
}
else
{
Console.WriteLine(“对战完毕”);
}
}
}
Console.ReadLine();

}
}
}