结构体
文章目录
- 1、概念
- 2、基本语法
- 3、示例
- 4、结构体的使用
- 5、访问修饰符
- 6、结构体的构造函数
- 思考1 描述矩形信息
- 思考2 职业名字释放了技能
- 思考3 小怪兽
- 思考4 多个小怪兽
- 思考5 奥特曼打小怪兽
1、概念
结构体是一种一定义变量类型
它是数据和函数的集合,可以在结构体中声明各种变量和方法
作用:用来表现存在关系的数据集
2、基本语法
1、结构体一般写在namespace语句块中
2、结构体关键字struct
struct 自定义{
变量
构造函数
函数
}
3、示例
//表现学生数据的结构体
struct Student{
//结构体声明的变量不能直接初始化
//Student s 不能包含自己的结构体
public int age;
bool sex;
int number;
string name;
//在结构体中的方法不用static
public void Speak(){
Console.WriteLine("我的名字是{0},今年{1}岁",name,age);
}
}
4、结构体的使用
Student student;
student.age;
5、访问修饰符
public 公共的
private 私有的 不写默认为private
6、结构体的构造函数
1、没有返回值
2、函数名必须和结构体同名
3、必须有参数
4、如果声明了构造函数,必须在其中对所以变量数据初始化
public Student(int age, bool sex, int number, string name){
//this.age 是上面结构体age,把传进来的参数赋值上去
this.age = age;
this.sex = sex;
this.number = number;
this. name = name;
}
Student s = new Student(18,true,2,"小王");
思考1 描述矩形信息
//使用结构体描述矩形信息,长宽;创建一个矩形信息
Rect rect = new(5, 4);
rect.Info();
struct Rect
{
public float w;
public float h;
public float area;
public float perimeter;
public Rect(float wi, float hi)
{
w = wi;
h = hi;
area = w * h;
perimeter = 2 * (w + h);
}
public void Info()
{
Console.WriteLine("矩形的宽为{0},高为{1},面积为{2},周长为{3}", w, h, area, perimeter);
}
}
思考2 职业名字释放了技能
//使用结构体描述玩家信息,玩家名字,玩家职业
//请用户输入玩家姓名,选择玩家职业
while (true)
{
Console.WriteLine("请输入姓名");
string name = Console.ReadLine();
Console.WriteLine("请输入职业(0战士,1猎人,2法师)");
try
{
Occupation occupation = (Occupation)int.Parse(Console.ReadLine());
PlayerInfo playerInfo = new PlayerInfo(name, occupation);
playerInfo.Atk();
}
catch
{
Console.WriteLine("格式错误");
}
}
struct PlayerInfo
{
public string name;
public Occupation occupation;
public PlayerInfo(string name, Occupation occupation)
{
this.name = name;
this.occupation = occupation;
}
public void Atk()
{
string occ = "";
string skill = "";
switch (occupation)
{
case Occupation.Warrior:
occ = "战士";
skill = "冲锋";
break;
case Occupation.Hunter:
occ = "猎人";
skill = "冲锋";
break;
case Occupation.Master:
occ = "法师";
skill = "奥数冲击";
break;
default:
break;
}
Console.WriteLine("{0}{1}释放了{2}", occ, name, skill);
}
}
enum Occupation
{
Warrior,
Hunter,
Master,
}
思考3 小怪兽
Monster[] monsters = new Monster[10];
for (int i = 0; i < monsters.Length; i++)
{
monsters[i] = new Monster("小怪兽" + i);
monsters[i].Atk();
}
//小怪兽
struct Monster
{
public string name;
public int atk;
public Monster(string name)
{
this.name = name;
Random random = new Random();
atk = random.Next(10, 31);
}
public void Atk()
{
Console.WriteLine("{0}的攻击力是{1}", name, atk);
}
}
思考4 多个小怪兽
Monster[] monsters = new Monster[10];
for (int i = 0; i < monsters.Length; i++)
{
monsters[i] = new Monster("小怪兽" + i);
monsters[i].Atk();
}
//小怪兽
struct Monster
{
public string name;
public int atk;
public Monster(string name)
{
this.name = name;
Random random = new Random();
atk = random.Next(10, 31);
}
public void Atk()
{
Console.WriteLine("{0}的攻击力是{1}", name, atk);
}
}
思考5 奥特曼打小怪兽
//结构体描述奥特曼与小怪兽
//定义一个方法实现奥特曼攻击小怪兽
//定义一个方法实现小怪兽攻击奥特曼
OutMan outMan = new OutMan("迪迦奥特曼",10,5,100);
Boss boss = new Boss("小怪兽", 8, 2, 100);
while (true)
{
outMan.Atk(ref boss);
if (boss.hp <= 0)
{
Console.WriteLine("{0}胜利",outMan.name);
break;
}
boss.Atk(ref outMan);
if (outMan.hp <= 0)
{
Console.WriteLine("{0}胜利",boss.name);
break;
}
Console.WriteLine("按任意键继续");
Console.ReadKey(true);
}
struct OutMan
{
public string name;
public int atk;
public int def;
public int hp;
public OutMan(string name,int atk,int def,int hp)
{
this.name = name;
this.atk = atk;
this.def = def;
this.hp = hp;
}
//结构体是值类型 想要在函数内部改变值类型信息,外部受影响,用ref或out
public void Atk(ref Boss monster)
{
monster.hp -= this.atk - monster.def;
Console.WriteLine("{0}攻击了{1},造成了{2}点伤害,{3}剩余血量{4}",name,monster.name,atk - monster.def,monster.name,monster.hp);
}
}
struct Boss
{
public string name;
public int atk;
public int def;
public int hp;
public Boss(string name, int atk, int def, int hp)
{
this.name = name;
this.atk = atk;
this.def = def;
this.hp = hp;
}
public void Atk(ref OutMan outman)
{
outman.hp -= this.atk - outman.def;
Console.WriteLine("{0}攻击了{1},造成了{2}点伤害,{3}剩余血量{4}", name, outman.name, atk - outman.def, outman.name, outman.hp);
}
}