lwj_C#_类的属性,方法参数 举例习题

来源:互联网 发布:软件服务龙头股 编辑:程序博客网 时间:2024/04/30 21:26
//    public class Hero
//    {
//        public string name;
//        //字段
//        private int age;
//        //属性  //属性里面不要使用属性本身;
//        public int Age{
//            get{ 
//                return age;
//            }
//            set{   //可以在这里面操作 分支语句if
//                age = value;//只有set里面有value;
//            }
//        }
//        public  void  Change(int[]array){
//            array [0] = 4;
//        }
//
//        //排序
//        //冒泡排序
//        public void Sort(int[]array){
//            for (int i = 0; i < array .Length - 1; i++) {
//                for (int j = 0; j < array.Length - i - 1; j++) {
//                    if (array[j] > array[j + 1]) {
//                        int temp = array [j];
//                        array[j] = array[j + 1];
//                        array[j + 1] = temp;
//                    }
//                }
//            }
//        }
//
//        //选择排序
//        public void Sort1(int[]array){
//            for (int i = 0; i < array.Length - 1; i++) {
//                int min = array [i];
//                int minIdex = 0;
//                for (int j = i + 1; j < array.Length; j++) {
//                    if (min > array[j]) {
//                        min = array [j];
//                        minIdex = j;
//                    }
//                }
//                array[minIdex] = array [i];
//                array[i] = min;
//            }
//        }
//    
//        //方法参数
//        //引用参数 ref  调用时也要加ref; 传递前要赋值 初始值;
//        public void Swap(ref int []x, ref int[] y){
//            int[] temp= x;
//            x = y;
//            y = temp;
//
//
//        }
//
//        //输出参数 out  out传递前可以没有值; 可以返回x y;
//        public int Cal(int a ,int b, out int x, out int y){
//            x = a + b;
//            y = a - b;
//            return x * y;
//        }
//
//        //params 必须时一维数组 输入任意个数的参数;
//        public float Avg(params int[] array){
//            int sum = 0;
//            foreach (int a in array) {
//                sum += a;
//            } 
//            return sum / array.Length;
//        }
//
//        //三个数从小到大排序
//        public void Max(ref int a, ref int b, ref int c){
//            int max = (a > b ? a : b) > c ? (a > b ? a : b) : c;
//            int min = (a < b ? a : b) < c ? (a < b ? a : b) : c;
//            int mid = (a + b + c) - max - min;
//            a = min; b = mid; c = max;
//            //方法二
////            if (a < b) {
////                int temp = a;
////                a = b;
////                b = temp;
////            }
////            if (a > c) {
////                int temp = a;
////                a = c;
////                c = temp;
////            }
////            if (b > c) {
////                int temp = b;
////                b = c;
////                c = temp;
////            }
//
//        }
//
//        //作业求和 数组奇数位为负数;
//        public int Sum(params int [] array){
//            int sum = 0;
//            for (int i = 0; i < array.Length; i++) {
//                if (i % 2 != 0) {
//                    array [i] = -array [i];
//                } 
//                sum += array [i];
//            }
//            return sum;
//        }
//    }
原创粉丝点击