U3D技术采集

OOP面向对象(封装,参数传递,继承)

2016-12-06  本文已影响159人  Unity开发

封装

oop三大特征之封装
 *
 *
 *为什么要封装?
 * 属性存在的目的其实就是为了保护类的内部结构不被破坏,达到封装性
 * 属性的语法格式为:
 * 访问修饰符public(必须滴)+对应字段的数据类型+字段名称(首字母必须大写)
 * {
 * 
 * }
 * 属性本身就是一种特殊的方法,他没有返回值是因为属性内部提供了可以访问该属性的访问器
 * 
 * ref参数:将值类型引用改变为地址引用
 * 
 * 值类型作为参数传递是,传递的不是本身,而是副本,此时要想修改本身的值,要将值类型改为引用类型
 * 
 * string虽然本身是引用类型,但是作为参数哦传递的时候,默认是值类型,而不是引用传递,这就需要我们
 * 如果想改变原来的字符串,这个时候要将值类型改为引用类型,加上ref关键字
 * 如果不想改变,就用原来的字符串,直接传递字符串
 * 
 * object本身就是引用类型,他和字符串不一样,默认就是引用类型,也就是传递的是地址
 * 数组作为参数进行传递,也是引用传递
 * 
 * out参数:输出到
 * 其实就是保存一个数值在其中
 * 

冒泡排序

public static void MAp(int [] Arr){
            int sum=0;
            for (int j = 0; j < 7;j++) {
                for (int i = 0; i < 7- j -1; i++) {
                    
                
                    if (Arr [i]> Arr [i + 1]) {
                        sum = Arr [i ];
                        Arr [i] = Arr [i + 1];
                        Arr [i + 1] = sum;
                    }
                }
                Console.WriteLine ();
            }
            foreach (var item in Arr) {
                Console.WriteLine (item);
            }
        }

//将冒泡排序封装成方法,在Main方法中遍历输出结果
            //编写一个方法,功能是打印数组里的元素
            int[] Arr = { 1, 2, 5, 8, 3, 5,6 };
            MainClass.MAp(Arr);

英雄类

class Yinxon{
            
            private string name="亚瑟";
            private string dw="坦克";
            private string tc="先手";
            private int sld;
            private string scnl="89";
            private string gjsh="320";
            private string jnxg="100%";
            private string ssnd="容易上手";


            public string Name
            {
                get{ 
                    return name;
                }    
            }
            public string Dw
            {
                get{ 
                    return dw;
                }    
            }
            public string Tc
            {
                get{ 
                    return tc;
                }    
            }


            public int Sld
            {
                set{ 
                    sld=value;
                    Console.WriteLine ("熟练度{0}",sld);
                }
                get{ 
                    return sld;

                }    
            
            }
            public string Scnl
            {

                get{ 
                    return scnl;
                }    
            }
            public string Gjsh
            {    
                get{ 
                    return gjsh;
                }    
            }
            public string Jnxg
            {    
                get{ 
                    return jnxg;
                }    
            }
            public string Ssnd
            {    
                get{ 
                    return ssnd;
                }    
            }

Yinxon y=new Yinxon();
            y.Sld=100; 
            Console.WriteLine ("英雄名:{0}",y.Name);
            Console.WriteLine ("定位:{0}",y.Dw);
            Console.WriteLine ("特长:{0}",y.Tc);
            Console.WriteLine ("生存能力:{0}",y.Scnl);
            Console.WriteLine ("攻击伤害:{0}",y.Gjsh);
            Console.WriteLine ("技能效果:{0}",y.Jnxg);
            Console.WriteLine ("上手难度:{0}",y.Ssnd);

参数传递

//验证值类型
        public static void Swap(ref int a,ref int c)
        {
            int temp=0;
            temp=a;
            a = c;
            c = temp;

        }

        //验证字符串
        public static void Append(string str) 
        {
            str="你好"+str;
            Console.WriteLine (str);
        }
        //验证引用类型
        public static void Update(Student1 s6)

        {
            s6.name="张三";

        }


        //验证数组
        public static void UpdateArr(int [] intArr)
        {
            intArr[0]=100;
        }

// 演示1 整形传递
            int a=10;
            int c=9;
            MainClass.Swap(ref a,ref c);
            Console.WriteLine ("{0}   {1}",a,c); 

            //演示2 字符串传递
            string s1="中国";
            MainClass.Append(s1);
            Console.WriteLine (s1);

            //演示三:对象传递

            Student1 s6 = new Student1 ();
            s6.name="李四";
            MainClass.Update (s6);
            Console.WriteLine (s6.name);

            //演示4 数组传递
            int [] intArr={1,2};
            MainClass.UpdateArr (intArr);
            foreach (var item in intArr) {
                Console.WriteLine (item);
            }

继承

  *继承:
 *继承使用条件:到你在设计类的时候,发现有字段相同
 *                具有相同特征不同行为的可以抽取出来,单独称为一个类,这个类供派生类使用
 *                简称;基类
 * 
 * 
 * 在现实生活中继承的关系横多
 * 1.儿子继承父亲
 * 2.黄焖鸡米饭
 * .......
 * 
 * 在程序中使用关键符号:表示继承
 * 格式:(:+类)
 * 这个类是指继承的类
 * 
 * 继承类的特点
 * 1.子类可以继承父类中的公有字段
 * 2.子类可以继承父类的公开属性
 * 3.子类可以继承父类中的公开方法
 * 4.父类不能拥有子类的字段、方法、属性、索引器
 * 5.在C#中不支持多重继承,也就是一个类只能继承一个类,如果你想支持多重继承,那么请使用接口Iterface
 * 6.继承关系中的构造函数(重点)
 * 1.在继承关系中首先会调用父类的构造函数,然后在调用子类的构造函数
 * 2.在继承关系中子类初始化对象的时候县调用父类的构造函数,然后再看子类构造函数有没有显示
 *     通知编译器指定调用父类的哪个构造函数,如果没有,那么默认去调用父类中的无惨构造函数
 *     此时如果父类重写了带有参数的构造函数,程序编译不会通过,那么解决办法就是给父类添加构造函数
 *     或者在子类构造函数中声明指定调用父类的哪个构造函数

//定义一个父类Hero

public class Hero
    {
        //字段
        public string hero_Name;
        public string hero_Ding;
        public string hero_Te;
        public string hero_Ext;
        public string hero_Live;
        public string hero_Hurt;
        public string hero_Effect;
        //public string hero_Operation;
        //私有字段
        private string _hero_Operation;

        public Hero()
        {
            Console.WriteLine ("构造函数1");
        }
        public Hero(string hero_Name,int hero_Ding)
        {
            this.hero_Name=hero_Name;
            Console.WriteLine ();
            Console.WriteLine ("构造函数2");
        }

        //属性

        public string Hero_Operation
        {
            set{ 
                Console.WriteLine (value);
                _hero_Operation = value;
            }
            get { 
                return _hero_Operation;
            }

        }
        //行为
        public void Baoji()
        {
            Console.WriteLine ("暴击效果");
        }
        //行为1 
        private void Baoji2()
        {
            Console.WriteLine ("暴击效果2");
        }
    }
    class Hero2
    {
        
    }
    class Hero_MonKey:Hero
    {
//        public string name; 
//        public int age; 


        public Hero_MonKey(string name,int age):base(name,age)
        {
            Console.WriteLine (name);
        }
        public static void Main()
        {
//            Hero_MonKey hm = new Hero_MonKey ();
//            hm.hero_Name="111";
//            hm.Hero_Operation = "222";
//            hm.Baoji ();
            Hero_MonKey h = new Hero_MonKey ("XXX",19); 
            Console.WriteLine (h);
        
        }

上一篇下一篇

猜你喜欢

热点阅读