005days
- 反射
- 异常处理
- 正则表达式
- 字符串类
反射
using System;
using System.Reflection; //反射需要用到的命名空间
//反射:可以直接通过类名、成员的名字来进行对象的实例化、操作对象
class Person
{
public int a;
private int b;
public static int c;
private static int d;
public Person()
{
Console.WriteLine("Person类无参的构造方法被调用");
}
private Person(int a,double b )
{
Console.WriteLine("有参构造函数被调用了");
}
public void ShowA() { }
private void ShowB() { }
public static void ShowV() { }
private static void ShowD() { }
public int show(int a,double b)
{
return a;
}
private int show(double a, int b)
{
return b;
}
}
class Program
{
public static void Main(string[] args)
{//实例化一个person对象
//通过类名来获取一个类型
//命名空间:如果这个类在一个命名空间中,那么写类型名的时候需要加上命名空间;
//例如:System.Int32
Type t = Type.GetType("Person");
//实例化一个对象
// object obj = Activator.CreateInstance(t);//访问不到私有的构造方法,默认会使用public无参的构造方法来实例化
//实例化一个对象,如果为true,表示可以匹配任何权限的无参构造方法
// object obj = Activator.CreateInstance(t, true);
//实例化一个对象,是通过public权限的有参构造来实例化的1
// object obj = Activator.CreateInstance(t, 1, 3.1);
//实例化一个对象,通过非public权限的有参构造方法
object obj = Activator.CreateInstance(t, BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { 1, 3.1 }, null);
/*
BindingFlags:要访问的方法或字段的权限描述,必须要同时具备两个描述
1.要访问的权限描述
2.要访问的成员归属
*/
//通过反射访问字段
//1.访问public权限的,非静态的字段a
FieldInfo a = t.GetField("a");
a.SetValue(obj, 1);//给obj对象的a字段进行赋值,赋值为1
Object aa = a.GetValue(obj);//获取对象obj的字段a的值
//2.访问private权限的,非静态的字段a
FieldInfo b = t.GetField("b", BindingFlags.NonPublic | BindingFlags.Instance);
b.SetValue(obj, 2);
object bb = b.GetValue(obj);
//3.访问public权限的,静态的字段a
FieldInfo c = t.GetField("c", BindingFlags.Public | BindingFlags.Static);
c.SetValue(null, 2);//如果要访问的是一个静态的成员,访问的主体是null
object cc = c.GetValue(null);
//4.访问private权限的,静态的字段a
FieldInfo d = t.GetField("d", BindingFlags.NonPublic | BindingFlags.Static);
d.SetValue(null, 2);
object dd =d.GetValue(null);
//通过反射访问类中的方法
//1.获取没有参数的方法
MethodInfo method0= t.GetMethod("ShowD", BindingFlags.NonPublic | BindingFlags.Static);
//方法调用,第一个参数是谁在调用方法,第二个参数:调用的方法的实参列表
method0.Invoke(null, null);
//2.获取有参数有重载的方法
//Type数组中传要访问的访问的参数类型列表
MethodInfo method1 = t.GetMethod("show", BindingFlags.Public | BindingFlags.Instance, null, new Type[] {typeof(int), typeof(double) }, null);
//第二参数是实参列表
//invoke方法的返回值,就是method1方法的返回值
object result = method1.Invoke(obj, new object[] { 1, 3.13 });
Console.WriteLine(result);
}
}
异常处理
using System;
//异常:程序在运行过程中产生的种种的不正常的现象
//异常处理、
/*
try{
* //可能会异常的代码
* }
* catch(异常类型 e){
* //这里捕获try中可能出现的异常
* }
* finally{
* //无论程序中有木有异常,这里面的代码始终会执行
* }
*
*1.一个try可以匹配多个catch来捕获多种异常
* 2.如果有多个catch
* 2.1如果多个catch捕获的异常没有继承关系,顺序无所谓
* 2.2如果多个catch捕获的异常彼此之间有继承关系,那么必须把父类异常放在最后
*
*/
namespace 异常处理
{
class Program
{
static void Main(string[] args)
{//在c#中,所有的异常都是Exception的子类
try
{
int[] ary = new int[3];
ary[4] = 0;
}
//来捕捉IndexOutOfRangeException这个异常
catch (IndexOutOfRangeException)
{
Console.WriteLine("发生了数组越界的异常");
}
catch (DivideByZeroException)
{
Console.WriteLine("除0异常");
}
catch (Exception)
{
Console.WriteLine("所有的异常");
}
Console.WriteLine("程序结束");
try
{
person.ScoreException();
}
catch (ScoreException exception) {
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(exception);
}
}
}
//自定义异常:继承自Exception类,写一个自己的类即可
class ScoreException : Exception {
//如果希望自定义的异常有描述信息,只需要一个有参构造
public ScoreException(string message) : base(message) { }
}
class person
{//设计一个会产生异常的方法
public static void ScoreException()
{
// 实例化一个异常对象
ScoreException exception = new ScoreException("这个是自定义的异常");
//抛出异常
throw exception;
}
}
}
正则表达式
using System;
using System.Text.RegularExpressions;
//正则表达式:用来判断某一个字符串是否匹配一定的格式
//没有代码提示,就是一个字符串
//using System.Text.RegularExpressions;
namespace housebook6
{
class Program
{
static void Main(string[] args)
{//^:匹配一个字符串的开头
//$:匹配一个字符串的结尾
//[]:匹配一个字符,这一位字符可以是这个中括号中的任何一个
//[1-9]:这一位字符可取的范围”[1,9]
//[a-zA-Z1-9]:这一个字符可取的范围[a,z],[A-Z],[1-9]
//[^1-9]:表示可以写[1-9]之外的所有字符
//实例化一个正则表达式对象
//+:前面的一位字符连续出现了1次或者多次
//*:前面的字符联系出现了0次或者多次
//?:表示前面的一位字符联系出现0次或者1次
//{m}:前面的一位字符连续出现m次
//{m,}:前面的一位字符至少出现m次
//{m,n}:前面的一位字符出现[m,n]次
//\d:[0-9]
//\D:[^0-9]
//别忘记\在字符串里面是转义字符
//.:通配符,可以匹配任意字符
Regex r = new Regex("^hello world$");
Regex r1 = new Regex("^[abcdefg]ello world$");
Regex r2 = new Regex("^[1-9]ello world$");
bool result = r.IsMatch("hello world");
bool result1 = r1.IsMatch("hello world");
bool result2 = r2.IsMatch("0ello world");
Console.WriteLine(result);
Console.WriteLine(result1);
Console.WriteLine(result2);
//批量替换
string names = "laowang xiaomin uncle wang";
string pattern = " {2,}";
Console.WriteLine(Regex.Replace(names,pattern,", "));
Console.WriteLine( CheckPhone("18888888888"));
}
public static string CheckPhone(string phone) {
//18888888888=>188****8888
//分组:可以将一个字符串分成几部分
//():用来表示一个分组
string pattern1 = @"^(1[34578]\d)(\d{4})(\d{4})$";
Match match = Regex.Match(phone, pattern1);
GroupCollection groups = match.Groups;
string group1 = groups[1].Value;
string group2 = groups[2].Value;
string group3 = groups[3].Value;
return group1 + "****" + group3;
}
}
}
字符串类
using System;
namespace 字符串类
{
class Program
{
static void Main(string[] args)
{//字符串类
char[] s = { 'h', 'e', 'l', 'l', 'o' };
//字符串实例化
string str = new string(s);//hello
//和另外一个字符串比较
//从低0个字符开始依次比较每一个字符,直到某一个字符可以确定大小
int result = "hello".CompareTo("hellloo");
Console.WriteLine(result);
//判断是否包含某个子串
bool c = "hello world".Contains("llo");
//Endswith:判断是否以指定的字符串结尾
//StartsWith:判断是否以指定的字符串开头
bool d = "harry".EndsWith("ry");
Console.WriteLine(d);
//在一个字符串中插入一个字符,得到一个新的字符串
string result1 = "ello".Insert(0, "h");
Console.WriteLine(result1);
//Remove:删除一部分
//SubString:截取一部分
string[] result2 = "xiaoming,laowang,xiaonghong".Split(',');
}
}
}