C#核心

类和对象

namespace 类和对象
{

    enum E_SexType
    {
        man,
        woman,
    }

    //成员变量,初始值:值类型默认都是0,引用类型默认都是null
    //访问修饰符
    public class Person()
    {
        public string name;
        public int age;
        public enum E_SexType;
        public Person[] friends;
        public void Speak(string str)
        {
            Console.WriteLine("{0}说了{1}", name, str);
        }

        public bool IsAdult()
        {
            return age >= 18;
        }

        public void AddFriend(Person p)
        {
            if(friends == null)
            {
                friends = new Person[] { p };
            }
            else
            {
                Person[] NewFriends = new Person[friends.Length + 1];
                for (int i = 0; i < friends.Length; i++)
                {
                    NewFriends[i] = friends[i];
                }
                NewFriends[NewFriends.Length - 1] = p;
                friends = NewFriends;
            }
        }


    }
    internal class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person();
            p1.name = "aaa";
            p1.age = 20;
            p1.Speak("abc");

            Person p2 = new Person();
            p2.name = "ccc";
            p2.age = 16;
            p1.AddFriend(p2);
            for (int i = 0; i < p1.friends.Length; i++)
            {
                Console.WriteLine(p1.friends[i].name);
            }



            Console.WriteLine( p1.IsAdult());
            Console.WriteLine(p1.age);  
            Console.WriteLine("Hello, World!");
        }
    }
}

封装_构造函数和析构函数

namespace 封装_构造函数和析构函数
{

    //构造函数
    //在实例化对象时,会调用的用于初始化的函数
    //如果不写,默认存在一个无参构造函数

    //1.没有返回值
    //2.函数名和类名必须相同
    //3.没有特殊需求时,一般都是public的
    class Person
    {
        public string name;
        public int age;

        public Person()
        {
            name = "aaa";
            age = 18;
        }

        public Person(int age)
        {
            this.age = age;
        }

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

        //先调用name
        public Person(string name, int age):this(name)
        {
      
        }

        //析构函数,引用类型的堆内存被回收时
        ~Person()
        {

        }
    }


    //垃圾回收机制
    //垃圾分类的过程是在遍历堆(heap)上动态分配的所有对象
    //通过识别它们是否被引用来确定哪些对象是垃圾

    //GC只负责堆内存的回收
    //栈上的内存是由系统自动管理的
    internal class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person();
            Console.WriteLine("Hello, World!");

            //手动触发垃圾回收
            GC.Collect();
        }
    }
}

成员属性

namespace 成员属性
{

    class Person
    {
        private string name;
        private int age;
        private int money;
        private bool sex;

        //成员属性用于保护成员变量
        //为成员属性的获取和赋值添加逻辑处理
        public string Name
        {
            get
            {
                //可以在返回之前添加一些逻辑规则
                //get意味着这个属性可以获取的内容
                return name;
            }
            set
            {
                //可以在设置之前添加一些逻辑规则
                //value关键字用于表示外部传入的值
                name = value;
            }
        }

        public int Money
        {
            get
            {
                //解密处理
                return money-5;
            }
            set
            {
                //加密处理
                money = value+5;
            }
        }

        public bool Sex
        {
            get
            {
                return sex;
            }
        }
        //自动属性
        //没有在get和set中写逻辑的需求或者想法
        public float Height
        {
            get;
            set;
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");

            Person p = new Person();
            p.Name = "aaa";
            Console.WriteLine(p.Name);

            p.Money = 10;
            Console.WriteLine(p.Money);

            Console.WriteLine(p.Sex);
        }
    }
}

索引器

namespace 索引器
{

    class Person
    {
        private string name;
        private int age;
        private Person[] friends;

        //索引器写法
        public Person this[int index]
        {
            get
            {
                return friends[index];
            }
            set
            {
                friends[index] = value;
            }
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}
# 静态成员
namespace 静态成员
{
    class Test
    {
        //静态成员变量
        public static float PI = 3.14f;
        //成员变量
        public int testInt = 10;

        //静态成员方法
        //成员变量只能将对象实例化出来后,才能点出来使用,不能无中生有
        //不能直接使用非静态成员
        public static float Calc(float r)
        {
            return PI * r * r;
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            //静态成员可以不实例化,直接调用
            //程序开始运行时,就会分配内存空间,所以能直接使用
            //静态成员和程序同生共死
            Console.WriteLine(Test.PI);
            Console.WriteLine(Test.Calc(2));
        }
    }
}

封装_静态类和静态构造函数

namespace 封装_静态类和静态构造函数
{
    //静态类,不能实例化
    static class TestStatic
    {
        public static int testIndex = 0;
        public static void Test()
        {

        }

        //静态构造函数
        static TestStatic()
        {

        }

    }
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
        }
    }
}

拓展方法

namespace 拓展方法
{
    static class Tools
    {
        //为int拓展了一个成员方法
        //value代表使用该方法的实例化对象
        public static void SpeakValue(this int value)
        {
            Console.WriteLine("为int拓展的方法" + value);
        }

        public static void SpeakString(this string str,string str2)
        {
            Console.WriteLine("调用方法的对象" + str);
            Console.WriteLine("传的参数" + str2);
        }

        public static void Fun3(this Test t)
        {
            Console.WriteLine("为test拓展的方法");
        }
    }

    //为自定义类型拓展方法
    class Test
    {
        public int i = 10;
        public void Fun1()
        {
            Console.WriteLine("123");
        }
        public void Fun2()
        {
            Console.WriteLine("456");
        }
    }
    internal class Program
    {
        
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            int i = 10;
            i.SpeakValue();


            string str = "aaa";
            str.SpeakString("111");

            Test t = new Test();
            t.Fun3();
        }
    }
}

运算符重载

namespace 运算符重载
{
    class Point
    {
        public int x;
        public int y;

        //一定是一个公共的静态方法
        //返回值写在operator前面
        public static Point operator +(Point p1,Point p2)
        {
            Point P = new Point();
            P.x = p1.x + p2.x;
            P.y = p1.y + p2.y;
            return P;
        }

        //一个运算符可以多次重载
        public static Point operator +(Point p1, int value)
        {
            Point P = new Point();
            P.x = p1.x + value;
            P.y = p1.y + value;
            return P;
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            Point p1 = new Point();
            p1.x = 1;
            p1.y = 2;
            Point p2 = new Point();
            p2.x = 3;
            p2.y = 4;
            Point p3 = new Point();
            p3 = p1 + p2;

            Point p4 = new Point();
            p4 = p1 + 2;
        }
    }
}

装箱拆箱

发生条件:
用object存值类型(装箱)
再把object转为值类型(拆箱)

基于里氏替换原则的object可以用object容器装载一切类型的变量
object是所有类型的基类

装箱
object v = 3;
拆箱
int intvalue = (int)v;

密封类

让类不能再被继承

sealed class Father
{

}

class Son:Father
{

}

多态

多态:让同一类型的对象,执行相同行为时有不同的表现
解决的问题:让同一对象有唯一的行为特征
vob:
v:virtual 虚函数
o:override 重写
b:base 父类
v和o一定是结合使用的 来实现多态
b是否使用根据实际需求 保留父类行为

多态_抽象类和抽象方法

abstrct class Thing
{
    public string name;
}

抽象类不能被实例化
抽象方法1.必须在抽象类中声明2.没有方法体3.不能是私有的4.继承后必须实现,用override重写

不希望被实例化的对象,相对比较抽象的类可以使用抽象类
父类中的行为不太需要被实现的,只希望子类去定义具体的规则的 可以选择抽象类

接口

namespace 接口
{
    //接口是抽象行为的“基类”
    interface IFly
    {
        void Fly();
        string Name
        {
            get;
            set;
        }
        int this[int index]
        {
            get;
            set;
        }
        event Action dosomething;
    }

    class Animal
    {

    }
    //类可以继承一个类,多个接口
    //继承了接口之后,必须实现里面的内容,并且必须是public的
    class Person : Animal,IFly
    {
        //实现的接口函数,可以加virtual再在子类重写
        public virtual void Fly()
        {

        }
        public string Name
        {
            get;
            set;
        }
        public int this[int index]
        {
            get
            {
                return 0;
            }
            set
            {

            }
        }

        public event Action dosomething;
    }
    //接口继承接口时,不需要实现
    //待类继承接口后,类自己去实现所有内容
    interface IWalk
    {

    }
    interface IMove:IFly,IWalk
    {

    }


    //显示实现接口,就是用接口名.行为名实现
    interface IAtk
    {
        void Atk();
    }
    interface ISuperAtk
    {
        void Atk();
    }
    class Player:IAtk,ISuperAtk
    {
        void IAtk.Atk()
        {
            throw new NotImplementedException();
        }
        void ISuperAtk.Atk()
        {
            throw new NotImplementedException();
        }
        public void Atk()
        {

        }
    }
    internal class Program
    {
       
        static void Main(string[] args)
        {
            Console.WriteLine("接口");
            //接口也遵循里氏替换原则
            IFly f = new Person();


            Player p = new Player();
            (p as IAtk).Atk();
            (p as ISuperAtk).Atk();
            p.Atk();
        }
    }
}

string常用方法

字符串指定位置读取

//字符串本质是char数组
string str = "aaa";
Console.WriteLine(str[0]);
//字符串拼接
str = string.Format("{0}{1},1,222333");
Console.WriteLine(str);
//正向查找字符位置
str = "abcde";
int index = str.IndexOf("a");
Console.WriteLine(index);
//移除指定位置后的字符
str = str.Remove(3);

posted @ 2026-01-25 14:48  古月秋筠  阅读(0)  评论(0)    收藏  举报