microroom microroom
洲者沧海之浮排,球者宇宙之尘埃,无海洋、太空科技之国无未来。
关注数: 132 粉丝数: 530 发帖数: 1,441 关注贴吧数: 146
输出、引用、可选参数,参数数组、匿名方法、lambda表达式 using System; using static System.Console; //定义委托类型时可以指定输出参数、引用参数、可选参数、参数数组 delegate void Del(out int a,ref int b,int c=77777,params int[] arr); class Exam { static void Main(string[] args) { int a=1,b=2; f(out a,ref b,555,6,7,8); f(out a,ref b); g(out a,ref b,999,6,7,8); g(out a,ref b); WriteLine("############"); Del d=f; d+=g; //可选参数c使用的也是定义委托Del时的默认值77777,未使用函数f和g的默认值, //参数数组为空 d(out a,ref b); b=112233; //匿名方法的参数不允许可选参数和参数数组 d+=delegate(out int a2,ref int b2,int c2/*=444*/,/*params*/ int[] arr2){ a2=123; WriteLine($"in delegate {a2},{b2},{c2},{String.Join("+",arr2)}"); }; //lambda表达式不允许有可选参数和参数数组 d+=(out int a3,ref int b3,int c3/*=444*/,/*params*/ int[] arr3)=>{ a3=456; WriteLine($"in lambda表达式 {a3},{b3},{c3},{String.Join("+",arr3)}"); }; d(out a,ref b,445566,8,8,8); b=666888; //可选参数c使用定义委托Del时的默认值77777,参数数组为空 d(out a,ref b); ReadKey(); } static void f(out int a,ref int b,int c=88,params int[] arr) { //输出参数使用前需赋值 a=111; WriteLine($"{a},{b}"); Action<int> d=delegate(int x) { WriteLine($"{c},{x}"); //匿名方法内部不能使用输出参数、引用参数 //WriteLine($"{a},{b}"); WriteLine($"{String.Join("+",arr)}"); }; Action<int> d2=(int x)=>{ WriteLine($"{c},{x}"); //lambda表达式内部不能使用输出参数、引用参数 //WriteLine($"{a},{b}"); WriteLine($"{String.Join("+",arr)}"); }; d(4); d2(44); } //输出参数、引用参数、参数数组不能指定默认值 //static void g(out int a=1,ref int b=2,params int[] arr=new int[]{5,6,7}) static void g(out int a,ref int b,int c=999,params int[] arr) { a=222; WriteLine($"{a},{b},{c},{String.Join("+",arr)}"); } }
使用结构时的限制 结构: 1 不能有显式无参构造函数,不能有析构函数 2 不能继承自其它结构或类,不能被其它结构或类继承 3 构造函数中必须初始化所有实例字段,实例字段、实例属性不能在类中初始化,未调用构造函数时,实例字段的值是不确定的,使用某个实例字段前必须为实例字段赋值,不能通过实例引用访问静态成员 4 不能包含抽象、密封、虚方法,不能包含保护成员 5 字段默认是私有的 using static System.Console; public class Test { static void Main() { Point p; //未调用构造函数时,字段的值是不确定的,使用字段前必须赋值 p.x=11; p.y=22; //p.st=8; //p.sf(); Point.st=9; Point.sf(); WriteLine($"{p.x},{p.y},{Point.st}"); WriteLine("ok"); } } struct Shape { public Shape(int n) { WriteLine("public Shape(int n)"); } } struct Point //struct Point:Shape //xxx { public Point(int x=0,int y=2) { //必须初始化所有实例字段 this.x=1; this.y=y; st=6; } /* //xxx public Point() { x=y=0; } public abstract void af() {} public sealed void sf() {} public virtual void vf() {} ~Point(){} protected void pf() { WriteLine($"protected void pf() z={z}"); } protected int z; */ static Point(){WriteLine("static Point()");st=7;} public static void sf() {WriteLine("public static void sf()");} //默认私有 public int x,y; //不能初始化实例字段 //public int y=0; //会默认初始化为0 public static int st; } //struct Point2D:Point{} //xxx
选择排序、鸡尾酒排序、冒泡排序 using System; using System.Linq; class P { static void Main() { const int Min=6,Max=21; var R=new Random(); var arr=Enumerable.Range(Min,Max-Min+1).ToArray(); Action<int[]> Rand=(int[] a)=>{ for(int i=0,j,t;i<a.Length;i++) { j=R.Next(0,Max-Min+1); t=a[i]; a[i]=arr[j]; a[j]=t; } }; Action<int[]> Print=(int[] a)=>{ Array.ForEach(a,e=>Console.Write("{0},",e)); Console.WriteLine(); }; Action<string,Action<int[]>> Sort=(string msg,Action<int[]> ac)=>{ int[] dst=new int[arr.Length]; Array.Copy(arr,dst,arr.Length); Console.WriteLine("{0}前:",msg); Print(dst); ac(dst); Console.WriteLine("{0}后:",msg); Print(dst); Console.WriteLine(); }; Rand(arr); Sort("选择排序",SelectSort); Sort("鸡尾酒排序",CocktailSort); Sort("冒泡排序1",BubbleSort1); Sort("冒泡排序2",BubbleSort2); Console.Write("按任意键继续。。。"); Console.ReadKey(true); } static void Swap<T>(ref T a,ref T b) { T x=a; a=b; b=x; } //选择排序 static void SelectSort(params int[] arr) { int a; for(int i=0;i<arr.Length-1;i++) { a=i; for(int j=i+1;j<arr.Length;j++) if(arr[j]<arr[a]) a=j; if(a!=i) { Swap(ref arr[i],ref arr[a]); } } } //鸡尾酒排序 static void CocktailSort(params int[] arr) { int a,b; for(int i=0,j=arr.Length-1;i<j;i++,j--) { a=b=i; for(int k=i;k<=j;k++) { if(arr[k]<arr[a]) a=k; else if(arr[k]>arr[b]) b=k; } if(a!=i) { Swap(ref arr[i],ref arr[a]); if(b==i) b=a; } if(b!=j) { Swap(ref arr[j],ref arr[b]); } } } //冒泡排序1 static void BubbleSort1(params int[] arr) { int a=0; bool sw=true; while(sw) { sw=false; for(int i=arr.Length-1;i>=a+1;i--) { //小数上浮 if(arr[i]<arr[i-1]) { Swap(ref arr[i],ref arr[i-1]); sw=true; } } a++; } } //冒泡排序2 static void BubbleSort2(params int[] arr) { bool sw=true; for(int i=0;sw&&i<arr.Length-1;i++) { sw=false; for(int j=0;j<arr.Length-1-i;j++) { //大数下沉 if(arr[j]>arr[j+1]) { Swap(ref arr[j],ref arr[j+1]); sw=true; } } } } }
快速排序的3种实现方法 import java.util.*; import static java.lang.System.*; import java.util.stream.*; import java.util.function.*; public class Program { public static void main(String[] args) { final int Count=81,Max=51; int[] arr=new int[Count]; Random r=new Random(); Arrays.setAll(arr,(int operand)->r.nextInt(Max)); int[] arr1=Arrays.copyOf(arr,Count); qsort1(arr1,0,Count-1); out.println("qsort1排序后:"); IntStream.of(arr1).forEach(e->out.printf("%d,",e)); out.println(); int[] arr2=Arrays.copyOf(arr,Count); qsort2(arr2,0,Count-1); out.println("qsort2排序后:"); Arrays.stream(arr2).forEach(e->out.printf("%d,",e)); out.println(); int[] arr3=Arrays.copyOf(arr,Count); qsort3(arr3,0,Count-1); out.println("qsort3排序后:"); Arrays.stream(arr3).forEach(e->out.printf("%d,",e)); out.println(); out.println("原数组:"); IntStream.of(arr).forEach(e->out.printf("%d,",e)); out.println(); Arrays.sort(arr); out.println("调用类库方法Arrays.sort对原数组排序后:"); IntStream.of(arr).forEach(e->out.printf("%d,",e)); out.println(); boolean eq=true; int[][] arrs={arr1,arr2,arr3}; for(int i=0;eq&&i<arrs.length;i++) { eq=Arrays.equals(arr,arrs[i]); } out.printf("4个排序后的数组相等?%b%n",eq); } //3个while版 static void qsort1(int[] arr,int l,int r) { int p,l2=l,r2=r; if(l>=r) return; p=arr[l]; while(l<r) { while(l<r&&arr[r]>p) r--; arr[l]=arr[r]; while(l<r&&arr[l]<=p) l++; arr[r]=arr[l]; } arr[l]=p; qsort1(arr,l2,l-1); qsort1(arr,l+1,r2); } //一个for版 static void qsort2(int[] arr,int l,int r) { int s,t; if(l>=r) return; s=l; for(int i=l+1;i<=r;i++) { if(arr[i]<arr[l]) { s++; t=arr[s]; arr[s]=arr[i]; arr[i]=t; } } t=arr[l]; arr[l]=arr[s]; arr[s]=t; qsort2(arr,l,s-1); qsort2(arr,s+1,r); } //非递归版 static void qsort3(int[] arr,int l,int r) { Deque<int[]> stack; int[] lr; int s,t; if(l>=r) return; stack=new ArrayDeque<>(); stack.push(new int[] {l,r}); while(!stack.isEmpty()) { lr=stack.pop(); l=lr[0]; r=lr[1]; s=l; for(int i=l+1;i<=r;i++) { if(arr[i]<arr[l]) { s++; t=arr[s]; arr[s]=arr[i]; arr[i]=t; } } t=arr[l]; arr[l]=arr[s]; arr[s]=t; if(l<s-1) stack.push(new int[] {l,s-1}); if(s+1<r) stack.push(new int[] {s+1,r}); } } }
实现接口的方法和属性 1 用类实现接口的属性可以添加额外的访问器,用显式接口成员实现不能添加额外的访问器。 2 显式接口成员实现的方法和属性只能通过接口的引用变量来访问。 3 可同时用类和显式接口成员实现来实现接口的方法和属性。 例子: using System; class Program : I1 { internal Program(){i3=1234;} //类实现f1 public void f1() { Console.WriteLine("f1"); } //显式接口成员实现I1.f2 void I1.f2() { Console.WriteLine("I1.f2"); } //同时类实现、显式接口成员实现f3 public void f3() { Console.WriteLine("f3"); } void I1.f3() { Console.WriteLine("I1.f3"); } //类实现属性p1可添加额外的访问器set public int p1 { get;set; } //显式接口成员实现属性p2不能添加额外的访问器set int I1.p2 { get; //错误 //set; } int i3; //同时类实现、显式接口成员实现属性p3 public int p3 {get;set;} int I1.p3 { get{return i3;} //错误,显式接口成员实现时不能添加额外的访问器set //set{i3=value;} } static void Main() { var o=new Program(); I1 i=o; o.f1(); o.p1=1; int p1=o.p1; //错误,f2、p2是通过显式接口成员实现的,不能通过类的引用变量调用 //o.f2(); //int a=o.p2; //正确,通过接口的引用变量调用显式接口成员实现的函数、属性 i.f2(); int b=i.p2; //正确,同时类实现、显式接口成员实现了f3、p3 o.f3(); o.p3=3; int c1=o.p3; i.f3(); //错误,显式接口成员实现的没有set访问器 //i.p3=33; int c2=i.p3; Console.WriteLine("end"); Console.ReadKey(); } } interface I1 { void f1(); void f2(); void f3(); int p1{get;} int p2{get;} int p3{get;} }
1 下一页