C#Lesson06

using System;


namespace Lesson06_1
{
class MainClass
{
//系统为每一个应用定义的一个主函数(Main函数)
//Main: 应用程序的入口,一般情况下所有应用程序的Main都是一个死循环
public static void Main (string[] args)
{
// //定义Person类的一个对象
// // new 向系统申请内存
// Person p1 = new Person();
// Person p2 = new Person();
// Car c1 = new Car ();
// Gun g1 = new Gun ();
// Fruit f1 = new Fruit ();
// Dog d1 = new Dog ();
//
// p1.name = "球球";
// p1.age = 18;
// p1.sex = "男";
// //赋值的本质: set方法
//
// //获取对象中字段内容
// string p1Name = p2.name;
// Console.WriteLine (p1Name);
// //取值的本质: get方法
//
// //对象是否值引用类型(Yes)
// Person p3 = p1;
// p1.name = "氤氲";
// Console.WriteLine (p3.name);
//
// c1.name = "法拉利";
// c1.color = Color.Orange;
// c1.speed = 200;
//
// g1.name = "98K";
// g1.type = Gun_Type.狙击;
// g1.color = Color.Black;
//
// f1.name = "火龙果";
// f1.price = 50;
// f1.color = Color.Red;
//
// d1.price = 10000;
// d1.name = "柴犬";
// d1.color = Color.Orange;
//
// string nc = c1.name;
// string ng = g1.name;
// string nf = f1.name;
// float p = d1.price;
// Console.WriteLine (nc);


// -------------------------------------------------------------------------


// Dog dog1 = new Dog ();
// dog1.name = "小黑";
// dog1.color = Color.Black;
// dog1.price = 1000;
// //调用狗睡觉的方法
dog1.Sleep();
// //dog1吃西瓜
// Fruit watermelon = new Fruit();
// watermelon.name = "西瓜";
// watermelon.color = Color.Green;
// watermelon.price = 20;
// dog1.Eat (watermelon);
// //实例化一条叫做小白的小狗,在键盘输入C的时候,让小白取睡觉
// Dog dog2 = new Dog();
// dog2.name = "小白";
// dog2.color = Color.White;
// dog2.price = 2000;
// while (true) {
// char c = Console.ReadKey ().KeyChar;
// if (c == 'C') {
// Console.WriteLine ();
// dog2.Sleep ();
// }
// }


// Dog dog3 = new Dog ();
// dog3.Init ("大黄", 500, Color.Yellow);


// Gun gun1 = new Gun ();
// gun1.SetType (Gun_Type.狙击);
// Console.WriteLine (gun1.GetType());


Player p1 = new Player ();
p1.Init ("飞虎队",Sex.Man,100,WeaponType.狙击枪);


Player p2 = new Player ();
p2.Init ("猎狐者",Sex.Woman,100,WeaponType.机枪);


p1.Introduce ();
p2.Introduce ();


//输入1: p1打p2
//输入2: p2打p1
//按下Q键,随机p1或者p2,让它们切换武器
//武器的切换,按顺序换到下一个
while (true) {
int a = int.Parse (Console.ReadLine ());
if (a == 1) {
p1.Attack (p2);
} else if (a == 2) {
p2.Attack (p1);
}
}


}
//C#中的方法(函数)
//1.无参数没有返回值
//将大象关进冰箱
public void PutTheElephantInTheRefrigerator() {
//设计,将大象塞进冰箱的具体操作
}
//2.有参数没有返回值
//计算两个整型的和
public void Sum(int a,int b){
//求和
}
//3.无参数, 有返回值
//获取当前时间
public string GetDate(){
//获取当前时间
string currentTime = DateTime.Now.ToString();
//将currentTime的值返回出去
return currentTime;
}
//4.有参数,有返回值
//将一个无序的整型数组变成有序的(升序)
public int[] SortArray(int[] target){
//设计排序算法
for (int i = 0; i < target.Length - 1; i++) {
for (int j = 0; j < target.Length - 1 - i; j++) {
if (target[j] > target[j+1]) {
int t = target [j];
target [j] = target [j + 1];
target [j + 1] = t;
}
}
}
return target;
}
}

}

-------------------------------------------------------------------------------------------------------------------------------

using System;


namespace Lesson06_1
{
public class Dog
{
public string name;
public Color color;
public float price;


//吃,叫,睡觉
public void Sleep() {
Console.WriteLine ("{0}睡觉了",name);
}
public void Eat(Fruit fruit){
Console.WriteLine ("{0}吃了一个{1}",name,fruit.name);
}


//设计一个Dog构造方法(伪构造方法)
public void Init(string name,float w,Color color)
{
//谁调用这个方法,this指的就是谁
this.name = name;
price = w;
this.color = color;
}


}
}


---------------------------------------------------------------------------------------------------------------------------


using System;


namespace Lesson06_1
{
public enum Sex
{
Man,
Woman
}
public enum WeaponType
{
匕首,
步枪,
机枪,
狙击枪
}
public class Player
{
string name;
Sex sex;
float hp;
WeaponType type;


public void Init(string name,Sex sex,float hp,WeaponType type){
this.name = name;
this.sex = sex;
this.hp = hp;
this.type = type;
}


public void Introduce(){
Console.WriteLine ("我叫作{0},我性别为{1},我装备了{2},我目前血量{3}",name,sex,type,hp);
}


public void ChangeWeapon(WeaponType type){
this.type = type;
Console.WriteLine ("我换上了{0}",type);
}


public void ReduceHp(float x){
this.hp -= x;
if (this.hp <= 0) {
Console.WriteLine ("玩家死亡");
} else {
Console.WriteLine ("{0}收到了{1}伤害",name,x);
}
}


public void Attack(Player p){
if (hp > 0) {
Console.WriteLine ("你对玩家{0}造成了伤害", p.name);
p.ReduceHp (10);
} else {
Console.WriteLine ("{0}已经死亡",p.name);
}
}
}
}

-----------------------------------------------------------------------------------------------------------------


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值