C#_dailylife02

函数重载

	//重载函数
		public static void func(string s) {
			Console.WriteLine(s);
		}
		public static void func(int i) { Console.WriteLine(i);}
		public static void func(float[] floats) {
			for (int i = 0; i < floats.Length; i++)
			{
				Console.WriteLine(floats[i]);
			}
		}
		static void Main(string[] args)
		{
			string s = "hello";
			int a = 1;
			float[] floats = { 1.1f, 2.2f, 3.3f };
			func(a);
			func(floats);
			func(s);


			Console.ReadLine();
		}

debug模式

  • 局部变量观测、设置断点、监视窗口、调用堆栈(观看函数执行顺序)
  • 逐语句模式
  • 跳出(直接运行完函数体的内容)

异常

static void Main(string[] args)
		{
			int[] array = { 1, 2, 3 };

			try
			{
				Console.WriteLine(array[4]);//放置可能出现的异常

			}
			catch (IndexOutOfRangeException e)
			{
				Console.WriteLine(e.Message);


			}
			catch (InsufficientExecutionStackException e)
			{
				Console.WriteLine(e.Message);
			}
			finally
			{
				Console.WriteLine("finally都会执行,无论是否出现异常"); //放置资源关闭的代码 }

				Console.ReadLine();

			}
		}

数字输入

double x1=Convert.ToDouble(Console.ReadLine()); //需要转换

e.g

2、让⽤户输⼊两个数字,⽤户可能会出⼊⾮数字类型,处理该异常,如果出现该异常就让⽤户重新输⼊,输出这两个数字的和

	static void Main(string[] args)
		{

			//2、让⽤户输⼊两个数字,⽤户可能会出⼊⾮数字类型,处理该异常,如果出现该异常就让⽤户重新输⼊,输出这两个数字的和
			double x1, x2;
			while (true)
			{
				try
				{
					 x1 = Convert.ToDouble(Console.ReadLine());
					 x2 = Convert.ToDouble(Console.ReadLine());
					break;
				}
				catch (FormatException e)
				{
					Console.WriteLine(e.Message);

				}
			}


			//x2=ConWsole.Read();

			Console.WriteLine("x1+x2={0}", x1 + x2);

			Console.ReadLine();
		}

OOP

右键解决方案(第一行),新建项目;

右键项目,新建类;

修改启动项;
float 类型初始化变量记得加f.
float aa = 1.1f;

set & get

 public class human
 {
 	    private string name;
		public string Name
		{
			private get { return name; }
			set { 
				name = value; 
				}
		}
 }
	   

name 对应的Set/get方法是 Name,首字母大写

yiimage.png
引用类型在stack中存储的是字符串变量地址,
stack存储了整型、浮点型常量
静态存储区存储了字符串常量
堆中存储了字符串常量地址

string ss =“zhangsan” ;
ss = “lisi”; //实际上是zhangsan字符串常量失去引用的过程

new过程创建的数据,在heap中

hunman h1 = h2; //h1,h2指向了同一个地址

GC机制
这片(heap)内存区域有一个计数器,记录有多少个对象引用它。当某一片内存的引用数为0是,视为Garage,进行回收。

继承

  • 实现继承(重写父类方法)
  • 接口继承(只继承签名、不继承任何实现的代码)
  • c#不支持多继承,但支持接口的多继承
public class MyDerivedClass : MyBaseClass,IInterface1,IInterface2
{
//etc
}

this 和 base(访问父类成员)

基类与派生类

  • 子类初始化一定会调用相匹配的某一个父类的构造函数
class enemy
	{
		//敌人公有的属性
		protected int hp;//血量
		protected int mp;//生命
		protected int speed;//速度

		public enemy(int hp, int mp, int speed)
		{
			this.hp = hp;
			this.mp = mp;
			this.speed = speed;
		}

		public virtual void attack()
		{
			Console.WriteLine("emeny的攻击方式");
		}

    	public void AI() { Console.WriteLine("敌人自己的ai"); }
	}


 class boss:enemy
	{
		private int level;

	     public boss(int hp, int mp, int speed, int level):base(hp, mp, speed)
		{
			base.hp = hp;
			this.mp = mp;
			this.speed = speed;
			this.level = level;
			Console.WriteLine("boss build.");

		}
//虚函数重写
		public override void attack()
		{
			
			Console.WriteLine("boss独特的攻击");
		}
//隐藏方法实现重写
public new void AI()
		{
			Console.WriteLine("boss自己的AI");
		}



		//自己的构造函数

		public void skill()
		{
			Console.WriteLine("{0}'s level skill attack.",this.level);
		}
	}


重写方式(虚方法、隐藏方法)

//父类
public virtual void attack()
		{
			Console.WriteLine("emeny的攻击方式");
		}
//子类
public override void attack()
		{
			
			Console.WriteLine("boss独特的攻击");
		}

//main
static void Main(string[] args)
		{
			boss Alexandera = new boss(1000,200,10,15);
			enemy guard = new boss(1000, 200, 10, 15);
			enemy enemy1 = new enemy(200, 0, 8);
			Alexandera.skill();
			Alexandera.attack();//虚方法实现重写
			guard.attack();//调用重写方法
			enemy1.attack();//调用基类方法
			//具体是否调用重写方法跟new对象有关(用谁的构造方法有关),跟左值赋给谁无关。

    
			Alexandera.AI();//调用boss的ai
			guard.AI();//调用敌人的ai ,隐藏方法看声明对象是谁,调用谁的,与虚方法重写不同

			Console.ReadLine();
		}

抽象类与密封类

abstract class abstract_emeny
	{
		abstract public void func1();
	}
internal class Inherited_abstract_enemy : abstract_emeny
	{
		public override void func1()
		{
			Console.WriteLine("具体化了抽象类的功能");
		}
	}
//密封类
sealed class A{} //A不能被继承
class B:B_father
{
    sealed override void func() //func只能被B重写,不能再次被重写了
    {
        
    }
}

接口

public interface Fly
	{
		void fly();
		void fly_attack();
	}
//成员必须全是是声明

接口实现多态

public static void Main(string[] args)
		{
			bird bird = new bird();
			bird.fly();
			bird.fly_attack();
			Console.WriteLine("`````````````");

			Fly fly = new plane();
			fly.fly_attack();//fly此时是飞机
			fly = bird;
			fly.fly_attack();//fly此时是鸟
			
			Console.ReadLine();
		}


接口可以实现多继承,类只能单继承

public class B:A(类),interface1,interface2 . . .
{
}

索引器

e.g. 用于转换输入的星期名和进制

internal class week_tr
	{
		private string[] days = { "mon", "tues", "wed", "thurs", "fri", "sat", "sun" };
		//实现
		public int GetDay(string day)
		{
			int i = 0;
			foreach (string s in days) 
			{
				if (s == day) return i+1;
				i++;
			}
			return -1;
		}

		public int this[string day]
		{
			get { return GetDay(day); }	
		}
	}


static void Main(string[] args)
		{
			week_tr week_Tr = new week_tr();
			string day = "THURS";
			Console.WriteLine(week_Tr.GetDay(day.ToLower()));//使用函数

			Console.WriteLine(week_Tr[day.ToLower()]);//使用索引器

			Console.Read();
		}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值