【Unity之c#专题篇】—核心章题单实践

news2024/9/22 13:45:19

在这里插入图片描述


👨‍💻个人主页:@元宇宙-秩沅

👨‍💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!

👨‍💻 本文由 秩沅 原创

👨‍💻 收录于专栏unityc#专题篇习题

在这里插入图片描述


习题总结专题篇


文章目录

    • 习题总结专题篇
    • 🎶前言
    • 🎶(==A==) 面向对象
    • 🎶(==B==)封装-成员变量和成员方法
    • 🎶(==C==)封装-构造函数和析构函数
    • 🎶(==D==)封装-成员属性
    • 🎶(==E==)封装-索引器
    • 🎶(==F==)封装-静态成员
    • 🎶(==G==)封装-静态类和静态构造函数
    • 🎶(==H==)封装-拓展方法
    • 🎶(==III==)封装-运算符重载
    • 🎶(==III==)继承
    • 🎶(==J==) 继承-里氏替换原则
    • 🎶(==K==)继承-构造函数
    • 🎶(==L==)继承-万物之父
    • 🎶(==M==)继承-综合练习
    • 🎶(==N==)多态
    • 🎶(==O==)多态-虚方法,重写,base
    • 🎶(==P==)多态-抽象类和抽象方法
    • 🎶(==Q==)多态-接口
    • 🎶(==R==)面向对象相关-命名空间
    • 🎶(==S==)面向对象相关-Object的方法
    • 🎶(==T==)面向对象相关-String
    • 🎶(==U==)面向对象相关-优化内存
    • ⭐相关文章⭐


🎶前言

核心章知识点详解入口


🅰️ 题单来自:B站唐老狮


🎶(A 面向对象


  • 实践经验

    1.类是对象,清楚类的本质
    2.对象和对象之间可作为参数相互联系(学生继承人,食物作为参数给学生)

在这里插入图片描述

 class Person
    {
      public   int age = 18;
      public   String name = "张三";
    }
    class Student : Person
    {
        String school ="北大";
        String sClass ="英语21102班";
        public void Eat(Food nice )
        {
           
            Console.WriteLine("我叫{0},今年{1}岁,就读于{2}{3},喜欢吃{4}", name, age, school, sClass, nice.foodName);
        }
    }
    class Food
    {
        public  String foodName ="西瓜";
        int foodForce;
    }
    class Program
    {
        static void Main(string[] args)
        {
            Student A  = new Student();
             Food  nice = new Food();
            A.Eat(nice);
        }
    }

🎶(B封装-成员变量和成员方法


  • 实践经验

    1.成员变量是对象的特征
    2.成员方法是对象的行为
    3.用面向对象的角度去看待和解决问题

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _成员变量和访问修饰符
{
    /// <summary>
    /// 对象为人类
    /// </summary>
    class Person
    {
        public String name;
        public float heigt;
        public  Int32 age;
        public String area;
        public Person(String name,float  heigt ,Int32  age ,String area):this()
        {
            this.name = name;
            this.age = age;
            this.heigt = heigt;
            this.area = area;
            Console.WriteLine("信息构造成功,该学生的姓名为{0},身高为{1},年龄为{2},地址为{3}", this.name, this.heigt, this.age, this.area);
        }
        public Person()
        {
            Console.WriteLine("构造函数的有参构造的最大作作用开始启动——一键赋值!");
                
        }

            

        public void Speak()
        {
            Console.WriteLine("嗨你好,请叫我智能语音助手");
        }
        public void Work()
        {
            Console.WriteLine("行走方式为直立行走");
        }
        public void Eat()
        {
            Console.WriteLine("进食方式为使用餐具");
        }

    }
    /// <summary>
    /// 对象为学生
    /// </summary>
    class Student 
    {
        public String name;
        public Int32 idea;
        public Int32 age;
        public String classMate;
    
        public void S_Main()
        {
            Console.WriteLine("我的学习方式为复盘");
        }
        public void Eat()
        {
            Console.WriteLine("进食方式为使用餐具");
        }
    }
    /// <summary>
    /// 对象为班级
    /// </summary>
    class MClass
    {
        public String major;
        public String capitary;
        public Int32 studentS;
    }
    class Food
    {
        String name;
        float Hat;
    }

    /// <summary>
    /// 项目入口
    /// </summary>
    class Mian
    {
        static void Main(string[] args)
        {
            //开始创建多个人对象
            Person A = new Person("张三", 163, 23, "湖南");
        }
    }

}


🎶(C封装-构造函数和析构函数


  • 实践经验

    1.最大作用一键赋值
    2.this对构造函数调用顺序的改变
    在这里插入图片描述
class Ticket
    {
        public float distance;
        public float price;
        public float rate = 1;
        Ticket() //无参构造
        {
            Console.WriteLine("欢迎浏览车票信息!");
        }
        public Ticket(float dis):this() //调整构造函数调用的顺序
        {
            if (dis < 0)
            {
                Console.WriteLine("请传入不为负数的参数");
            }
            else
            {
                distance = dis;

            }
            ShowTicket();
        }
        public void GetPrice()
        {
            if (distance > 100 && distance <= 200) rate = 0.95f;
            else if (distance > 200 && distance <= 300) rate = 0.9f;
            else if (distance > 300) rate = 0.8f;
            price = rate * distance;
        }
        public void ShowTicket()
        {
            GetPrice();
            Console.WriteLine("本车票信息如下:\n 全程{0}公里\n 票价共{1}元", distance, price);
        }
        static void Main(string[] args)
        {
            Ticket B = new Ticket(288);
        }
    }
    #endregion


🎶(D封装-成员属性


  • 实践经验

    1.解决了3P的局限性,提高了安全性
    2.用属性的逻辑规则进行保密处理
    3.自动属性(无逻辑规则的时候用)

在这里插入图片描述

 #region 18.学生自我介绍行为实现
    class Student
    {
     
        String sex;
        Int32 age;
        float cGrade;
        float uGrade;

        /// <summary>
        /// 四个属性的定义
        /// </summary>
        /// 
        public int A { get; set; } = 666;

        public String Name{ get; set; }
        public int Age
        {
            get
            {
                return this.age;
            }
            set
            {
                if (value >= 0 && value <= 150)
                    this.age = value;
                else Console.WriteLine("年龄超出范围!");
            }
        }
        public String Sex
        {
            get
            {
                return this.sex;
            }
            set
            {
                if (value == "男" && value == "女") this.sex = value;

                else Console.WriteLine("请传入男或者女");
            }
        }
        public float CGrade
        {
            get
            {
                return this.cGrade;
            }
            set
            {
                if (value >= 0 && value <= 100)
                    this.cGrade = value;
                else Console.WriteLine("成绩超出范围!");
            }
        }
        public float UGrade
        {
            get
            {
                return this.uGrade;
            }
            set
            {
                if (value >= 0 && value <= 100)
                    this.uGrade = value;
                else Console.WriteLine("成绩超出范围!");
            }
        }
        /// <summary>
        /// 构造函数一键赋值
        /// </summary>
        /// <param name="name"></param>
        /// <param name="sex"></param>
        /// <param name="age"></param>
        /// <param name="cGrade"></param>
        /// <param name="uGrade"></param>
        public Student(String name, String sex, int age, float cGrade, float uGrade)
        {
            Name = name;
            this.sex = sex;
            Age = age;
            CGrade = cGrade;
            UGrade = uGrade;
            SHi();
            Grade();

        }
        /// <summary>
        /// 问候方法
        /// </summary>
        /// 
        public void SHi()
        {
            Console.WriteLine("我是{0},今年{1},性别{2}", Name, Age, Sex);
        }
        /// <summary>
        /// 成绩计算方法
        /// </summary>
        /// <param name="args"></param>
     
        public void Grade()
        {
            Console.WriteLine("总分为{0},平均分为{1}", CGrade + UGrade, (CGrade + UGrade) / 2);
 
            Console.WriteLine(A);
        }
        static void Main(string[] args)
        {
            Student A = new Student("张三", "男", 18, 90, 56);
        }

    }
    #endregion

🎶(E封装-索引器


  • 实践经验

    1.通过索引器的索引对类中的成员进行便捷操作
    2.class Person ; person A = new person() ; A[1] = 2 ;
    3.模拟构造了用索引器的容器
    在这里插入图片描述
  • 便捷操作
    //定义一个 Name 属性来操作 name 字段
    public string Name
    {
        set { name = value; }
        get { return name; }
    }

    //定义一个 Password 属性来操作 password 字段
    public string Password
    {
        set { password = value; }
        get { return password; }
    }

    //定义索引器,name 字段的索引值为 0 ,password 字段的索引值为 1
    public string this[int index]
    {
        get
        {
            if (index == 0) return name;
            else if (index == 1) return password;
            else return null;
        }
        set
        {
            if (index == 0) name = value;
            else if (index == 1) password = value;
        }
    }
}

  • 拟构容器
class INT
    {
        private int[] arrary;
        private int cerioty ;
        public int length;
        public INT() //构造函数中初始化
        {
            arrary = new int[cerioty];
            cerioty = 5;
            length = 0;
        }
        public int this[int index]
        {
            get
            {
                if (index > length) Console.WriteLine("越界!");
                return arrary[index];
            }
            set
            {
                arrary[index] = value;
                length++;
            }
        }
        //增
        public void Add(int vaule)
        {
            if(length+1 < cerioty )
            arrary[length++] = vaule;
            //进行扩容操作
            else              
            {
                cerioty *= 2;
                int[] tempArray = new int[cerioty];
                for (int i = 0; i < length; i++)
                {
                    tempArray[i] = arrary[i]; //值全部先放过去
                }
                arrary = tempArray;          //然后地址变更成新地址 ,旧的空间变成垃圾回收
            }
            
        }
        //删
        public void Remove(int index)
        {
            if(arrary[index]!=null)
            {
                arrary[index] = 0;
            }
        }
        static void Main(string[] args)
        {
            INT A = new INT();
            A.Add(666);
            A.Add(999);
            A[0] = 250;
            Console.WriteLine(A[0]+" "+A[1]+" "+A[2]+" "+A[3]);
        }
    }


🎶(F封装-静态成员


  • 实践经验

    1.静态函数不能直接调用非静态成员
    2.在单例模式中的应用

单例模式的实现‘

在这里插入图片描述

   class MathS
    {
        private static MathS m; //2.此时外部可以访问并且只有一个该对象存在,因为它在该对象内部实例化了
                                //但是外部还是可以修改为NULL,就达不到第三个条件通过类名点出来得到唯一的对象
        public int secret = 10;
        public static MathS M//3.通过属性只返回一个唯一实例化的对象,就可以实现第三部分直接通过类名点出来得到唯一的对象并且不可被修改
        {
            get
            {
                return m = new MathS();
            }
        }
        private MathS() //1.构造函数变成私有,此时外部无法进行实例化
        {

        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(MathS.M.secret);
        }
    }

🎶(G封装-静态类和静态构造函数


  • 实践经验

    1.静态类两大特点的底层原因:存储于静态存储区,所以和堆区栈区的操作就无关了
    2.静态构造函数的三大特点也是如此:三无,只构造静态变量,码字时就分配了空间
   static  class MathS //静态类测试
    {  
        static  float   CArea( float a ,float b)
        {
            return a * b;
        }
        static void Main(string[] args)
        {
            MathS.CArea(2.0f, 3.0f);
        }
    }
    class Self  //静态构造函数测试
    {
        static int a;
       static Self () 
        {
            a = 2;
        }       //现在已经赋值了
        Self () //此时还需实例化后才能赋值
        {
            a = 2;
        }
    }

🎶(H封装-拓展方法


  • 实践经验

    1.声明定义的条件:无泛型的静态类中的静态方法
    2.声明时有参和无参在调用时的区别,和对于格式的理解
    static public void xx( this int yy)

在这里插入图片描述
在这里插入图片描述

  #region 拓展自杀的方法
    // class Player
    // {
    //     string name;
    //     int blood;
    //     int tacket;
    //     int defens;
    //     public void Hit()
    //     {

    //     }
    //     public void Move()
    //     {

    //     }
    //     public void Heart()
    //     {

    //     }
    // }
    //static  class Running
    // {
    //    static  public void KillSelf(this Player a)
    //     {
    //         Console.WriteLine("我自杀了");
    //     }
    //     static void Main(string[] args)
    //     {
    //         Player boss = new Player();
    //         boss.KillSelf();
    //     }
    // }

    #endregion
    #region      拓展求平方的方法

  static class Programe
    {

        static public int PinFan( this int A)
        {
            return (int)Math.Pow(A, 2);
        }

        static void Main(string[] args)
        {
            int a = 10;
           Console.WriteLine( a.PinFan());
        }

    }

    #endregion

🎶(III封装-运算符重载


  • 实践经验

    1.熟悉怎么声明一个运算符重载的静态方法
    2.&&,|| , = ,三目运算符等运算符无法重载,逻辑运算符需要成对的重载方法

在这里插入图片描述

 #region  _运算符重载 25
 
    class Vector3
    {
        public int x, y, z;
        /// <summary>
        /// 三维向量得加法
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        static public Vector3 operator +(Vector3 a ,Vector3 b)
        {
            Vector3 c = new Vector3();
            c.x = a.x + b.x;
            c.y = a.y + b.y;
            c.z = a.z + b.z;
            return c;
        }
        /// <summary>
        /// 三维向量的减法
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        static  public Vector3  operator -(Vector3 a, Vector3 b)
        {
            Vector3 c = new Vector3();
            c.x = a.x - b.x;
            c.y = a.y - b.y;
            c.z = a.z - b.z;
            return c;
        }
        /// <summary>
        /// 三维向量的乘法
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        static public Vector3 operator -(Vector3 a, int b)
        {
            Vector3 c = new Vector3();
            c.x = a.x * b;
            c.y = a.y * b;
            c.z = a.z * b;
            return c;
        }
    }

    class Program
    {
        struct S_PD //用结构体定义的方式来实现运算符重载
        {
            public int x1, x2;
            static public bool operator +(S_PD a, S_PD b)
            {
                if (a.x1 == b.x1 && a.x2 == b.x2)
                {
                    return true;
                }
                return false;
            }
        }
        public int x1,x2;
        public static bool operator +( Program a ,  Program b)
            {        
            if (a.x1 == b.x1 && a.x2 == b.x2 ) return true;
            return false;
            }
        static void Main(string[] args)
        {
            Program a = new Program();
            Program b = new Program();
            S_PD A = new S_PD();
            S_PD B = new S_PD();
            a.x1 = 1; b.x1 = 1;
            a.x2 = 2; b.x2 = 2;
            A.x1 = 3; B.x1 = 4;
            A.x2 = 3; B.x2 = 1;
            Console.WriteLine(a + b);
            Console.WriteLine(A + B);

        }
    }
    #endregion

🎶(III继承


  • 实践经验

    1.父类中私有的可以被继承但是不能被调用
    在这里插入图片描述
  /// <summary>
    /// 人类
    /// </summary>
    class Person
    {
       public  String name;
       public  int Age { get; set; } 
       public  void Speak()
        {
            Console.WriteLine("我会说英语");
        }
    }
    /// <summary>
    /// 战士类继承人类
    /// </summary>
    class Fighter : Person 
    {
       public  void  attent()
        {
            Console.WriteLine("我具有攻击性");
        }
    }
    /// <summary>
    /// 主类
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            //第27题
            Fighter boss = new Fighter();
            boss.name = "牛魔王";
            boss.Age = 32;
            boss.Speak();
            boss.attent();
        }
    }


🎶(J 继承-里氏替换原则


  • 实践经验

    1.父类装载子类,可以通过父类调用子类的方法(里氏替换原则)
    2.is和as的使用(里氏替换原则)
    在这里插入图片描述
  //29题
    /// <summary>
    /// 怪物基类
    /// </summary>
    class Monster
    {
        virtual public void attend()
        { }
    }
    /// <summary>
    /// 派生类boss类
    /// </summary>
    class Boss : Monster
    {
        override public void attend()
        {
            Console.WriteLine("Boss释放技能");
        }
    }
    /// <summary>
    /// 派生类小怪类
    /// </summary>
    class Goblin : Monster
    {
        override public void attend()
        {
            Console.WriteLine("Goblin普通攻击");
        }
    }
    /// <summary>
    /// 主类
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            //第28题 : //is是类型所属判断操作符,as是引用类型转换操作符
            //第29题
            Random rand = new Random();
            int flag;
            Monster[] totall = new Monster[10];
            for (int i = 0; i < 10; i++)
            {
                if (rand.Next(2) == 1)
                {
                    totall[i] = new Boss(); //用父类装子类——里氏替换原则
                }
                else
                {
                    totall[i] = new Goblin();
                }
            }
            foreach (var item in totall)
            {
                item.attend();
                //as 和 is 的调用搭配
                //if (item is Boss) (item as Boss).attend();
                //else if (item is Goblin) (item as Goblin).attend();   
            }
        }
    }
    //30题 FPS游戏模拟
 //30题 FPS游戏模拟
    //武器类
    class Weapon
    {

    }
    //冲锋枪类
    class SubmachineGun : Weapon
    { }
    //散弹枪类
    class ShotGun:Weapon
    { }
    //手枪类
    class Pistol :Weapon
    { }
    //匕首类
    class Dagger:Weapon
    { }
    class Player
    {
        public Weapon knif;
        public Weapon Gun { get; set; }
        public Player() //默认武器为匕首
        {
            knif = new Dagger();
        }
        public void steadGun( Weapon gun) //替换武器
        {
            Gun = gun;
        }
    }

🎶(K继承-构造函数


  • 实践经验

    1.继承时构造函数默认的调用顺序
    2.通过base 和 this 巧妙的调节继承时构造函数的 调用顺序
    在这里插入图片描述
 /// <summary>
    /// 打工人基类
    /// </summary>
    class Worker
    {
    
        public String Jobs { get; set; }
        public String Contain { get; set; }
        virtual  public void Method() { } 
    }
    /// <summary>
    /// 程序员类
    /// </summary>
    class Programmer : Worker
    {
        public Programmer(String job,String contain )
        {
          
            Method();
        }
        public override void Method()
        {
            Console.WriteLine("我打代码");
        }
    }
    /// <summary>
    /// 策划类
    /// </summary>
    class Mastermind :Worker
    {
        public Mastermind(String job, String contain)
        {
            Jobs = job;
            Contain = contain;
            Method();
        }
        public override void Method()
        {
            Console.WriteLine("我搞策划");
        }
    }
    /// <summary>
    /// 美术类
    /// </summary>
    class Art:Worker
    {
        public Art(String job, String contain)
        {
            Jobs = job;
            Contain = contain;
            Method();
        }
        public override void Method()
        {
            Console.WriteLine("我美术设计");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Programmer garmmer = new Programmer("程序员","打代码");
            Mastermind master = new Mastermind("策划员", "搞策划");
            Art art = new Art("ui设计师", "美术设计");
        }
    }

🎶(L继承-万物之父


  • 实践经验

    1.拆箱装箱的本质是值类型和引用类型的相互转换
    2.好处和坏处(有利于不确定对象的装载,但是跨存储区的转换消耗性能)
    在这里插入图片描述
    //口头描述拆箱装箱:
    //1.装箱 : 将值类型的对象装在Object基类(引用类型)中的行为叫装箱
    //2.拆箱 : 将装好的Object对象进行强制转换成值类型的行为叫拆箱
    //
    class Program
    {
        static void Main(string[] args)
        {
            object god;
            int mm = 2;
            god = mm;//装箱
            //拆箱
            int zz = (int)god;
        }
    }

🎶(M继承-综合练习


  • 实践经验

    1.当继承有参构造函数时直接用base关键字
    2.面向对象本质就是拿现实来看待问题。定义特征和抽象行为,从现实生活的角度用代码创造生活的一切
    在这里插入图片描述
 /// <summary>
    /// 定义基类Person
    /// </summary>
    class Person
    {

     public  String name { get; set; }
     public Person ()
     {

     }
     public  Person (String name )
        {
            this.name = name;
        }
    }

    class Drive:Person //司机类
    {
     
    }
    class Passgener:Person//乘客类
    {

    }
    class Car
    {
         public int velocity; 
         public int number = 1;
        private int capacity { get; } = 20;
        private int maxVelocity { get; } = 200;
        public Person driver;
        public Person [] constorm  ;    
        public Car()
        {
            constorm = new Person[capacity];
            driver = new Person("张三");
            constorm[0] = driver;
        }
        public void GoCar(Person passenger)   //上车的行为
        {
            if(number<20)
            {
                Console.WriteLine("乘客上车");
                constorm[number++] = passenger;
            }
            else
            {
                Console.WriteLine("已满载!");
            }
        }
        public void ExitCar(Person passenger) //下车的行为
        {
            Console.WriteLine("乘客下车");
            constorm[number--] = null ;
        }
        public void Driving()                //开始行驶的行为
        {
            Console.WriteLine("车辆开始行驶");
            velocity = 20;
        }
        public void Accident()              //发生事故的行为      
        {
            Console.WriteLine("车辆发生事故");
            velocity = 0;
            number = 0;
        }
        public void StateCar()             //车辆情况汇报系统
        {
            Console.WriteLine("此时车辆的状况如下:\n车辆速度:{0}\n 车辆人数:{1}", velocity, number);
        }
    }
    /// <summary>
    /// 车辆新闻人播报
    /// </summary>
class NewsPeople
    {
        static void Main(string[] args)
        {
            Car BWM = new Car();
            BWM.GoCar(new Person ("郭富城"));
            BWM.Driving();
            BWM.StateCar();

        }
    }

🎶(N多态



🎶(O多态-虚方法,重写,base


  • 实践经验

    1.编译时的多态是重载
    2.运行时的多态是重写,虚方法,base,抽象类和抽象接口
    3.虚方法可选择性重写
    在这里插入图片描述
  #region 1.鸭子的叫法,利用重写体现多态
    class Duck
    {
        virtual public void Speak() {  }
    }
    class RealDuck:Duck
    {
        override public void Speak()
        {
            Console.WriteLine("嘎");
        }
    }
    class WoodDuck:Duck
    {
        override public void Speak()
        {
            Console.WriteLine("吱");
        }
    }
    class RubberDuck:Duck
    {
        override public void Speak()
        {
            Console.WriteLine("叽");
        }
    }
    #endregion
    #region 2.员工打卡,利用重写体现多态
    class Worker
    {
        virtual public void Sign()
        {
            Console.WriteLine("我是员工九点打卡");
        }
       
    }
    class Massger:Worker
    {
        override public void Sign()
        {
            Console.WriteLine("我是经理十一点打卡");
        }
    }
    class Programme:Worker
    {
        override public void Sign()
        {
            Console.WriteLine("我是程序员我不打卡");
        }
    }
    #endregion
    #region 3.利用重载体现多态
    class Graphic
    {
       virtual  public void Area() { }
       virtual  public void Circumference() { }
    }
    class Rectangle:Graphic 
    {
         public int Area(int heiht,int wigth)
        {
            return heiht * wigth;
        }
        public  int  Circumference(int heiht, int wigth)
        {
            return 2 * (heiht + wigth);
        }
    }
    class Round : Graphic
    {
        public float  Area(int Side)
        {
            return 2 * 3.14f * Side;
        }
        public float  Circumference(int Side)
        {
            return (float )Math.Pow(Side, 2)*3.14f;
        }
    }
    class Square :Graphic
    {
        public int Area(int Radius)
        {
          return (int)Math.Pow(Radius , 2);
        }
        public int  Circumference(int Radius)
        {
            return 4 * Radius;
        }
        class Progeam
        {
            static void Main(string[] args)
            {
                Rectangle MM = new Rectangle();
                Square NN = new Square();
                Round OO = new Round();
                Console .WriteLine ("矩形的面积为:"+ MM.Area(2, 3));
                Console.WriteLine("矩形的周长为:"+MM.Circumference(2, 3));
            }
        }
    }
    #endregion

🎶(P多态-抽象类和抽象方法


  • 实践经验

    1.抽象类抽象方法必须被重写,抽象方法没有函数体
    2.抽象类不能被实例化但是遵从里氏替换原则
    3.什么时候用抽象类,比如动物类,图形类这种抽象未落地的事务就可以用抽象类

在这里插入图片描述

#region 动物抽象类
    abstract class Animal
    {
        abstract public void Speak();
    }
    class Person:Animal
    {
        public override void Speak()
        {
            Console.WriteLine("你干嘛"); 
        }

    }
    class Dog:Animal 
    {
        public override void Speak()
        {
            Console.WriteLine("汪汪汪");
        }
    }
    class Cat:Animal
    {
        public override void Speak()
        {
            Console.WriteLine("喵喵喵");
        }
    }
    #endregion
    #region 图形类
   abstract  class Circle
    {
        //抽象函数不能有方法体,并且是公共的
        abstract public int Area(int a ,int b);
        abstract public int Perimeter(int a,int b);
    }
    class Rectangle :Circle
    {
        public override int Area(int a,int b)
        {
            return a * b;
           
        }
        public override int Perimeter(int a,int b)
        {
            return 2 * (a + b);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //抽象类存在里氏替换原则
            Circle Round = new Rectangle();
            Dog Hashiqi = new Dog();
            Console.WriteLine(Round.Area(1, 3));
        }
    }
    #endregion

🎶(Q多态-接口


  • 实践经验

    1.接口中只能有(无函数体的方法,属性,事件。索引器)
    2.接口方法不需要public修饰,它默认就是public
    3.接口就是抽象一个共用的功能,谁继承即可学习(相当于恶魔果实)
    4.接口继承接口不用全部实现其中成员
    5.接口用里氏替换原则做父类装class
    在这里插入图片描述
    在这里插入图片描述

   #region 继承登记功能接口
    interface IRigster
    {
        //接口中不含变量可包含,无函数体的方法,属性,索引器,事件
        //接口方法不需要public修饰,它默认的就是pulic
        void RigsterMessage(); 
    }
    class Person : IRigster
    {
        public void RigsterMessage()
        {
            Console.WriteLine("派出所登记");    
        }
    }
    class Hourse : IRigster
    {
        public void RigsterMessage()
        {
            Console.WriteLine("房管局登记");
        }
    }
    class Car : IRigster
    {
        public void RigsterMessage()
        {
            Console.WriteLine("车管所登记");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //利用里氏替换原则
            IRigster[] Rigster = new IRigster[3] { new Person(), new Hourse(), new Car() };
            for (int i = 0; i < Rigster.Length ; i++)
            {
                Rigster[i].RigsterMessage();
            }
        }
    }
    #endregion
    #region 继承飞行,游泳,走路的功能接口
    interface IFly
    {
        void Flay();
    }
    interface ISwim
    {
        void Swim();
    }
    interface IWalk
    {
        void Walk();
    }
    abstract class Bird : IFly, IWalk
    {
        abstract public void Flay();


        abstract public void Walk();

    }
    class MaQue : Bird
    {
        public override void Flay()
        {
            Console.WriteLine("我是麻雀可以飞");
        }

        public override void Walk()
        {
            Console.WriteLine("我是麻雀也可以走");
        }
    }
    class YinWU : Bird
    {
        public override void Flay()
        {
            Console.WriteLine("我是鹦鹉可以飞");
        }

        public override void Walk()
        {
            Console.WriteLine("我是鹦鹉也可以走");
        }
    }
    class TiamE : Bird, ISwim
    {
        public override void Flay()
        {
            Console.WriteLine("我是天鹅可以飞");
        }

        public void Swim()
        {
            Console.WriteLine("我可以游泳");
        }

        public override void Walk()
        {
            throw new NotImplementedException();
        }
    }
    class TuoNiao : IWalk
    {
        public void Walk()
        {
            Console.WriteLine("我可以走");
        }
    }
    class QIE : IWalk
    {
        public void Walk()
        {
            Console.WriteLine("我可以走");
        }
    }
    class Plan : IFly
    {
        public void Flay()
        {
            Console.WriteLine("我可以飞");
        }
    }
    #endregion

在这里插入图片描述

  interface IUSB
    {
        void ReadState();
    }

    class MoveSave : IUSB
    {
        private string name;

      public   MoveSave (string name )
        {
            this.name = name;
        }
        public void ReadState()
        {
            Console.WriteLine(name +"可以进行传输");
        }
    }
    class MP3 : IUSB
    {
        public void ReadState()
        {
            Console.WriteLine("可以进行传输了");
        }
    }
    class Computer 
    {
        public IUSB flacity;  //传输接口作为变量
  
    }
    class Program
    {
        static void Main(string[] args)
        {
            MoveSave USave = new MoveSave("U盘");
            MoveSave Msove = new MoveSave("移动硬盘");
            MP3 Xmp3 = new MP3();
            Computer XMi = new Computer();
            XMi.flacity = USave;  //里氏替换原则,接口作为父类装了类的子类
            XMi.flacity.ReadState();

        }
    }

🎶(R面向对象相关-命名空间


  • 实践经验

    1.要引用其他项目的命名空间时,在解决方案中添加需要项目的引用
    2.相同命名空间有同名类时用命名空间+”."的格式进行区分

在这里插入图片描述

在这里插入图片描述

//当引用其他项目的命名空间时
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UI;
using Graph;
namespace _命名空间Text
{
    class Program
    {
        static void Main(string[] args)
        {
               UI.Image a = new UI.Image();
            Graph.Image b = new Graph.Image();
        }
    }
}

🎶(S面向对象相关-Object的方法


  • 实践经验

    1.七个方法:两个静态方法(判断两个对象,判断两个引用对象)
    两个普通方法(得到运行时返回的类型,浅拷贝引用对象)
    三个虚方法(自定义判断规则,自定义返回的Tostring,自定义返回的哈希码)
    2.以下是虚方法重写制定Tostring 和 浅拷贝引用对象(A变B不变)
    在这里插入图片描述
  class Player
    {
        string name;
        int blood;
        int attack;
        int defens;
        //重写万物之父中的ToString方法
        public override string ToString()
        {
            return "玩家"+name+"血量"+blood+"攻击力"+attack+"防御力"+defens;
        }
    }
    class Monster
    {
     public   int attack = 10;
        int defend = 20;
        int blood = 30 ;   

        public object Man()
        {
            return new Monster().MemberwiseClone();
            //适合给引用对象实施浅拷贝——A变B不变
        }
        public void print()
        {
            Console.WriteLine(attack +" "+defend+" "+blood );
        }
    }
    class Program
    {
       static void Main(string[] args)
       {
           // Player text = new Player();
           // Console.WriteLine(text.ToString());
            Monster A = new Monster();
            Monster B = A.Man() as Monster ; //as比()更安全-引用对象
            B.attack = 30;
            B.print();
            A.print();  
       }  
    }

🎶(T面向对象相关-String


  • 实践经验

    1.七个API:拼接Format,替换Replace,截取Substring,移除Remove,查找Indexof,切割Split,大小写转换ToUpper/Tolower
    2.String是特殊的引用类型,你变它不变,原因是它已经自己来开辟了空间
    3.使用拼接一般会在堆里面开辟三个空间
    4,所以它的缺点就是多次赋值开辟空间浪费了空间
    在这里插入图片描述
  • 第50题: 开辟了3个堆的空间(= null的时候不开辟,“123”是存在常量池中)
  • 第49题: String和string,int32和int 前者是Framwork当中的类,后者可以映射成为前者,最好写后者

        //string中的截取方法:a.Split();
        //string中的替换方法:a.replace();
        //string中的移除方法:a.Remove();
        //string中的拼接方法:String.Format();

        //
        static void Main(string[] args)
        {
            string a = "1|2|3|4|5|6|7";
            string [] b = a.Split('|');
            string c = "";
            
          
            for (int i = 0; i < b.Length ; i++)
            {
                b[i] = (int.Parse(b[i])+1).ToString() ;
                if(i < b.Length )
               c= string.Format("{0}{1}{2}",c,b[i],'|');
        }


            Console.WriteLine(c);
        }

🎶(U面向对象相关-优化内存


  • 减少new的使用
  • 减少GC回收机制的运行
  • 合理使用static(static与程序同生共死,不会受GC回收影响)
  • 合理的使用String 和StringBuilder

⭐相关文章⭐

⭐本站最全-unity常用API大全(万字详解),不信你不收藏

⭐【2023unity游戏制作-mango的冒险】-4.场景二的镜头和法球特效跟随

⭐“狂飙”游戏制作—游戏分类图鉴(网易游学)



你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/506190.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

电脑开关机-第14届蓝桥杯省赛Scratch初级组真题第1题

[导读]&#xff1a;超平老师的《Scratch蓝桥杯真题解析100讲》已经全部完成&#xff0c;后续会不定期解读蓝桥杯真题&#xff0c;这是Scratch蓝桥杯真题解析第130讲。 电脑开关机&#xff0c;本题是2023年5月7日举行的第14届蓝桥杯省赛Scratch图形化编程初级组真题第1题&#…

flstudio21有什么新功能,主题随心换,苹果M2/1家族芯片原生支持

FL Studio 21推出 – 新功能和改进。如果您从事音乐制作&#xff0c;那么您不可能没有听说过 FL Studio&#xff0c;或者很可能已经使用过这个音乐程序。好了&#xff0c;新版本的 FL Studio 21 DAW已经准备好向公众发布了。Image-line 正在为 2023 年的大型揭幕准备最终细节。…

go 源码解读 - sync.WaitGroup

go version 1.19.7 在 Go 语言中&#xff0c;sync.WaitGroup 是一个并发编程的同步工具&#xff0c;用于等待一组 Goroutine 执行完毕。 当需要等待多个 Goroutine 完成任务后才能执行下一步操作时&#xff0c;我们可以使用 sync.WaitGroup 实现协程间的同步。它提供了 Add()…

测试:概念篇

目录 简单介绍测试 我们先简单的介绍一下测试工程师 简单来看看测试和开发的区别 测试的基本概念 什么是需求 BUG 的概念 测试用例 什么是测试用例&#xff1f; 为什么有测试用例 测试周期 开发模型 瀑布模型&#xff1a; 螺旋模型&#xff1a; 敏捷软件开发 V …

PostgreSQL 查找重复数据(二)

创建表和测试数据&#xff1a; -- DROP TABLE IF EXISTS people; CREATE TABLE people (id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,name varchar(50) NOT NULL,email varchar(100) NOT NULL );INSERT INTO people(name, email) VALUES (张三, zhangsantest.com),(李…

操作系统考试复习-—第四章 分段式 段页式存储方式

分段从存储管理方式&#xff1a;一方面是通常的程序都可以分为若干段&#xff0c;另一方面是实现和满足信息共享&#xff0c;信息保护&#xff0c;动态链接以及信息的动态增长等需要。也都是以段为基本单位实现的。所以说&#xff0c;分段存储管理方式更符合用户和程序员多方面…

JWT认证

一、什么是JWT 官网地址: https://jwt.io/introduction/ jsonwebtoken&#xff08;JWT&#xff09;是一个开放标准&#xff08;rfc7519&#xff09;&#xff0c;它定义了一种紧凑的、自包含的方式&#xff0c;用于在各方之间以JSON对象安全地传输信息。此信息可以验证和信任&…

华为nqa实验拓扑案例

bqa是一种实时的网络性能探测和统计技术&#xff0c;可以对响应时间、网络抖动、丢包率等网络信息进行统计。如图1所示&#xff0c;接口备份与NQA联动功能配置相对简单&#xff0c;只需在本端RouterA上配置NQA测试例&#xff0c;并在RouterA的备份接口上配置接口备份与NQA联动&…

自定义组件中如何注入Spring底层的组件

1.概述 自定义的组件要想使用Spring容器底层的一些组件&#xff0c;比如ApplicationContext&#xff08;IOC容器&#xff09;、底层的BeanFactory等等&#xff0c;那么只需要让自定义组件实现XxxAware接口即可。此时&#xff0c;Spring在创建对象的时候&#xff0c;会调用XxxA…

搞懂 API,API 常见技术使用场景分享

API&#xff08;应用程序编程接口&#xff09;是一种允许软件应用程序之间相互交互和通信的技术。以下是API常用的使用场景&#xff1a; 应用程序开发 API通常被用于网站或应用程序的开发中&#xff0c;以便在不同平台、语言及数据库之间获取数据或进行消息传递。例如&#xff…

探索数字化转型新道路!流辰信息微服务与您一起创未来!

科技在进步&#xff0c;社会在发展&#xff0c;办公自动化也在高速发展中。数字化转型是当下企业获得长久发展的趋势之一&#xff0c;在信息瞬间万变的社会中&#xff0c;谁掌握了核心技术&#xff0c;谁能与时代同步&#xff0c;谁就能开启新的康庄大道&#xff0c;谁就能在转…

VS2017配置Qt——超详细步骤教学(看完不会算你狠)

一、环境要求 visual studio 2017 vsaddin Qt14.1 mysql 注意mysql环境与msvc2017编译器环境保持一致。 mysql32位 配 msvc2017 32位 或 mysql64位 配 msvc2017 64位 注意&#xff1a;环境不一致会导致软件运行错误&#xff0c;为了避免这些错误&#xff0c;要将…

第1章计算机系统漫游之 “源代码的编译与执行” 及 “操作系统管理硬件”

文章目录 1、信息就是位上下文2、程序被其他程序翻译成不同的格式3、了解编译系统如何工作的益处4、处理器读并解释储存在存储器中的指令4.1 系统的硬件组成4.2 执行 hello 程序 5、高速缓存6、形成层次结构的存储设备7、操作系统管理硬件7.1 进程7.2 线程7.3 虚拟存储器7.4 文…

docker容器内使用cat命令修改文件

有时候docker容器内部没装vi 或vim命令&#xff0c;无法使用vi来修改文件 可以使用cat命令来查看文件 cat 主要功能一次显示整个文件:cat filename 从键盘创建一个文件:cat > filename 只能创建新文件,不能编辑已有文件 将几个文件合并为一个文件:cat file1 file2 > fi…

最新黄金市场价格分析之干掉调整浪

等待的过程无疑是最令人心烦的。各位朋友应该试过&#xff0c;等待自己的朋友、亲人&#xff0c;等等结果&#xff0c;等待成绩公布等等。但是等待是我们干任何事都必不可少的过程&#xff0c;是我们缓冲、蓄力的阶段。最新黄金市场价格分析中的等待&#xff0c;体现在调整浪的…

Python心经(3)

这一节总结点demo和常用知识点 目录 有关字符串格式化打印的 lambda匿名函数&#xff0c;&#xff0c;将匿名函数作为参数传入 文件读写 生成器 python的装饰器 简单的网站代码&#xff1a; 有关三元运算 推导式&#xff1a; 新浪面试题&#xff1a; 有关面向对象里…

SpringBoot项目中一些常用的,工具类

推荐多使用这个&#xff1a; Hutool参考文档Hutool&#xff0c;Java工具集https://hutool.cn/docs/#/core/%E9%9B%86%E5%90%88%E7%B1%BB/%E9%9B%86%E5%90%88%E5%B7%A5%E5%85%B7-CollUtil?id%e4%bb%8b%e7%bb%8d 1&#xff1a;断言 断言是一个逻辑判断&#xff0c;用于检查不应…

芯片封装技术(三)

Interposer 是一种用于连接芯片的中间层技术&#xff0c;它的基底通常是一块硅基底&#xff0c;而硅基底也是 Substrate 的一种。因此&#xff0c;Interposer 与 Substrate 有一定的关系。对于RDL Interposer来说&#xff0c;Si Interposer的信号布线密度进一步提高&#xff0c…

[Linux] Linux文件系统

&#x1f941;作者&#xff1a; 华丞臧. &#x1f4d5;​​​​专栏&#xff1a;【LINUX】 各位读者老爷如果觉得博主写的不错&#xff0c;请诸位多多支持(点赞收藏关注)。如果有错误的地方&#xff0c;欢迎在评论区指出。 文章目录 一、Linux文件系统1.1 磁盘1.2 inode1.3 软硬…

Philosophy of life: growing flowers in your heart

Growing flowers in your heart An aged man lived in a nice cottage with a large garden in a town in England. He is seen busy looking after his flowers all time. 第一部分介绍的是: 有一个老人在英格兰的镇上有一个带大花园的屋子&#xff0c;他一直在忙着照顾他的花…