美文网首页
OOP(面向对象)

OOP(面向对象)

作者: Unity开发 | 来源:发表于2016-12-05 21:50 被阅读30次

oop:面向对象,全称叫Object oriented programming
 * 面向对象的三大特点:1.封装,
 *                         2.继承
 *                         3.多态
 * 
 * 
 * 类的定义:实际上就是具有相同属性和特征的一类事物的抽象集合
 * 注意:类是对象的抽象,对象是类的实例
 * 
 * 对向的定义:抽象类的实例
 * 
 * 昨天我去了工商银行去办银行卡,在我前边有50个人 、、、53
 * 
 * 1.知道了类的定义,如何定义类?
 *         使用系统关键字:(默认访问修饰符internal)Class +类名+{}
 *         注意1点:类和类属于同级关系
 * 2.如何描述类的特征
 *       叫字段或类成员变量
 *       字段和普通局部变量的区别,访问该局部变量时编译会通过 字段不会
 * 3.实例化对象
 *          采用系统关键词new + 类名()
 *       new 关键字的作用:
 *             1,。开辟一块内存空间(堆区)
 *             2.在开辟出来的内存空间中存对象信息(字段属性方法)
 *            3.初始化对象(调用构造函数)
 * 
 * 4.如何描述类的行为
 *      叫方法或者函数
 *         语法格式:修饰词+函数返回值+函数名(方法名)(形参列表)
 * {
 * 
 * }
 * 5.修饰符()
 *     public:公开·的意思,常用于修饰类,修饰类字段,修饰类种方法
 *                 就是非本类之外也可以访问
 *     private :私有的,常用于修饰字段,方法,也是C#中默认的修饰符,
 *                 就是只有本类才可以访问
 *     intrnal:程序集内部可以访问
 *     protected:基类&&派生类可以访问
 *     
 * 6.如果方法又返回值就返回方法的实际类型的返回值,关键字:return +返回内容;
 * 如果方法没有返回值,就可以省略retrun不写,直接写方法体内的方法快即可
 * 
 * 4种方法
 * 有参有返回值
 * 有参无返回值
 * 无参有返回值
 * 无参无返回值
 * 
 * 
 * 7.枚举变量
 * 格式:enum+枚举名称
 * {
 * 枚举变量1,
 *  枚举变量1,
 *  枚举变量1
 * }
 * 8.构造函数(重点掌握)
 * 构造函数的作用:让一个类更完整。
 * 构造函数其实就是用来创建对象,初始化字段用的
 * 构造函数的语法格式为:修饰符+类名
 * {
 * }
 * 构造函数的特点:
 * 1,没有返回值
 * 2,构造函数的名字必须和类名保持一致
 * 3,构造函数支持方法重载,换句话说就是可以有多个构造函数,但参数个数不能相同
 * 4.构造函数无法进行手动调用(系统自动调用)
 * 5.如果类中不包含构造函数,那么系统会自动为该类添加一个无参的构造函数
 * 6.如果类中存在带有参数的构造函数,那么系统不会为该类添加无惨的构造函数
 */ 

          //定义枚举
 enum Ge
    {
        男,
        女
    }
   代码实现

                                           //学生类
    class Student{
        public int id;
        private string name;
        private string classshu;
        private int fs;

        public Student(int id,string name,string classshu,int fs)
        {
            this.id = id;
            this.name = name;
            this.classshu = classshu;
            this.fs = fs;
            Console.WriteLine ("学号为{0}的学生姓名为:{1}  年级为:{2}  分数为:{3}",id,name,classshu,fs);

        }

    }


    //全真:
    class Quanzhen{
        //public string name; 
        public string zmname ;
        public int zmage; 
        //public string wdname; 

        public Quanzhen(string zmname,int zmage)
        {

            this.zmname = zmname;
            this.zmage = zmage;

            Console.WriteLine ("全真教掌门名字为:{0}年龄为:{1}",zmname,zmage);

        }
        public Quanzhen(string wdname,string name) 
        {

            Console.WriteLine ("武当掌门{0}到{1}来参观",wdname,name);

        }


    }
    //武当:
    class Wudao{
        //        public string name; 
        //        public string qzzmname ;
        //        public string zmname; 
        //        public string zmage; 

        public Wudao(string zmname,int zmage)
        {
            Console.WriteLine ("武当教掌门名字为:{0}年龄为:{1}",zmname,zmage);
        }

        public Wudao(string qzzmname,string name)
        {
            Console.WriteLine ("全真教掌门{0}到{1}来参观",qzzmname,name);
        }


    }
    //丐帮:
    class Gaibao{
        //        public string name; 
        //        public string qzzmname; 
        //        public string zmname; 
        //        public string zmage; 

        public Gaibao(string zmname,int zmage)
        {
            Console.WriteLine ("武当教掌门名字为:{0}年龄为:{1}",zmname,zmage);
        }

        public Gaibao(string qzzmname,string name)
        {
            Console.WriteLine ("全真教掌门{0}到{1}来参观",qzzmname,name);
        }



    }


    class Boss
    {
        public string name;
        //        public int fyl; 
        //        public int gjl;  
        //        public int xl;  
        //        public int mfz;  
        public Boss()
        {

        }


        public Boss(string name, int mfz )
        {
            this.name = name;
            Console.WriteLine ("我的名字是:{0} 魔法值为{1}",name,mfz);

        }

        public Boss(string name,int fyl,int xl ) 
        {

            Console.WriteLine ("我的名字是:{0}防御力是{1}血量是{2}",name,fyl,xl);
        }

    }






    //定义了一个人类


    class Person
    {
        //特征

        //名字
        public string name;  
        //年龄
        public int age;   
        //性别
    //    public string gender;
    //性别枚举
        public Ge gender; 
        //构造函数    
        public Person()
        {
            Console.WriteLine ("构造函数");
        }

        //This关键字:代表这个类的这个函数
        public Person(string name)
        {
            this.name = name;
            Console.WriteLine ("名字为:{0}",name);
        }
    }

    class Boss
    {
        public string name;
        //        public int fyl; 
        //        public int gjl;  
        //        public int xl;  
        //        public int mfz;  
        public Boss()
        {

        }


        public Boss(string name, int mfz )
        {
            this.name = name;
            Console.WriteLine ("我的名字是:{0} 魔法值为{1}",name,mfz);

        }

        public Boss(string name,int fyl,int xl ) 
        {

            Console.WriteLine ("我的名字是:{0}防御力是{1}血量是{2}",name,fyl,xl);
        }

    }






    //定义了一个人类


    class Person
    {
        //特征

        //名字
        public string name;  
        //年龄
        public int age;   
        //性别
    //    public string gender;
    //性别枚举
        public Ge gender; 
        //构造函数    
        public Person()
        {
            Console.WriteLine ("构造函数");
        }

        //This关键字:代表这个类的这个函数
        public Person(string name)
        {
            this.name = name;
            Console.WriteLine ("名字为:{0}",name);
        }
    }


        //行为 1.....吃
        public void Eat(string foodname)
        {
            Console.WriteLine ("{0}在吃{1}",name,foodname);
        
        }
        //行为 2
        public string SayHi()
        {
        
            return "我是" + name + "大家好";

        }
    }
    class Class1{
        public string name;
        public int renshu;  
        public void Shangke(string student)
        {
        
            Console.WriteLine ("班级{0}中李四在上{1}",name,student); 
        }

    }
    class Food{
        public string shuigu;
       public string mianshi; 
        public string rou;
        public void Eat1(string food)
        {
        
            Console.WriteLine ("食物有{0}张三喜欢吃{1}",shuigu,food);
        }
        public void Eat2(string food)
        {
            Console.WriteLine ("食物中{0}李四喜欢吃{1}",rou,food);
        }
    }




    class MainClass
    {


        //练习2:定义一个方法,比较int类型的数据,得到其最大值;
        public int Max(int i,int j)
        {
            
            if (i > j) {
                Console.WriteLine (i);
            } else {
                Console.WriteLine (j);
            }
            return i>j?i:j;    
        }
        public void pj(string a,string  b)
        {
        

            Console.WriteLine (a+b);
        
        }

    public int SumValue(int n)
    {int sum=0;
        for (int i = 1; i <= n; i++) {
             sum +=i;
        }
        return sum;
    }

        //练习2:定义一个方法,比较int类型的数据,得到其最大值;
////            MainClass m=new MainClass();
//            int max1=new MainClass().Max(18,25); 
//            Console.WriteLine (max1);
//            //new MainClass().Max(15,20);
//        //编写方法int SumValue(int n):计算1-n的和
//        Console.WriteLine (new MainClass().SumValue(100) ); 

            //枚举演示
            //访问枚举变量是用枚举名.的方式调用
            //在Unity 中枚举常用来判断游戏状态·1(游戏开始,游戏暂停,游戏结束)
            Person p=new Person();
            p.name = "刘德化";
                p.age=55;
                p.gender = Ge.男;
            Person p1 = new Person ("刘德");


            //练习1:该类包含一个Boss类,该类包含攻击力,防御力,血量及魔法值等字段
            //包含一个介绍自己的行为比如(我是XXX,我的攻击力是XXX。。。)
            //要求:使用至少3种构造函数对该boss类进行实例化,调用自身的行为
            Boss b=new Boss();

            Boss b1 = new Boss ("李四",120);
            Boss b3 = new Boss ("王五",50,90);

            //练习2:设计一个学生类,包含学号,姓名,(私有字段),年级(私有字段)




            // 练习3.设计全真教,武当,丐帮类
            //全真:
            // 字段:掌门姓名,掌门年龄
            // 方法:介绍全真,武学介绍

            Quanzhen q = new Quanzhen ("李四",50); 
            Quanzhen q1 = new Quanzhen ("老王", "全真");
            //武当:
            // 字段:掌门姓名,掌门年龄
            // 方法:介绍全真,武学介绍
            Wudao w=new Wudao("老王",60); 
            Wudao w1=new Wudao("李四","武当"); 

            //丐帮:
            // 字段:掌门姓名,掌门年龄
            // 方法:介绍全真,武学介绍
            //

            Gaibao g = new Gaibao ("王五",67);
            Gaibao g1 = new Gaibao ("李四","丐帮");

            //1.分别采用不同的构造方法对门派进行实例化对象
            //2.每个门派至少包括1个其他门派掌门人来该门派的访问信息
            //比如:全真包含
            // 武当xxx,到本派进行参观



            //练习2.设计一个学生类,包含学号,姓名(私有字段),年级(私有),班级分数(私有)
            //通过构造函数可以对学生进行实例化(不能对私有字段赋值)
            //通过输入学号可以获取该学生的相关信息,比如101,可以显示101对应的学生姓名年级班级等信息

            Student s = new Student (101, "张三", "SHU",90); 
        }


    }
}

class Student{
        private string name;
        public string Name{
            get{ 
                return name;
            }
            set{ 
                name = value;
            }
        }
        public void A()
        {
            Console.WriteLine (name);
        }

        public string xuehao;  
        public int age;  

    }


    class Class{

        public string name;
        public string clasname; 
        public int rs;  
        public string techname; 
        public string time; 

        public Class(string name)
        {
            this.name = name;
            Console.WriteLine ("{0}集体活动与开班仪式",name);

        }
    }

    class Techer{

        public string name; 
        public int age;    
        public string kc; 
        public string gender; 
        public string classname; 


        public Techer()
        {
            Console.WriteLine ("讲课、布置作业、验收作业、解决问题、自我介绍。");

        }


    }
    class Student1{

        public string name; 
        public int age; 
        public string kc; 
        public string gender; 
        public string classname; 

        public int xh;         
        public string techername; 


        public void Arr(int[] a,int[] b)
        {
            Array.Copy (a, b, 4);
        }
        public void Arr1(int []ab)
        {
            foreach (var item in ab) {
                Console.WriteLine (item);
            }
        }

    }

    class MainClass
    {
        public static void Main (string[] args)
        {
//            《第六讲:C#语言编程》类
//
//        
//            1.(*)完成课件的练习,建30个类,每个类有自己的特征,为特征填写Get 方法和Set方法。

            Student s = new Student ();
            s.Name="aaaa";
            s.A ();


//            3.(**)创建一个方法,功能是把一个int类型的数组拷贝到另外一个数组里面,并把数组各元素打印出来;
            Student1 s2=new Student1();
            int []a={1,2,3,4};
            int[] b = new int[4];
            s2.Arr (a,b);
            foreach (var item in b) {
                Console.WriteLine (item);
            }


//            4.(**)创建一个方法,求一个int类型数组里面元素的和,并把和打印出来;
            Student1 s3=new Student1();
            int []ab={1,2,3,4,5};
            s3.Arr1 (ab);

        }
    }     

相关文章

网友评论

      本文标题:OOP(面向对象)

      本文链接:https://www.haomeiwen.com/subject/kyslmttx.html