C#面向过程

面向对象

封装:把复杂的逻辑封装到类中,对用户只是暴露出一个简单的接口(类)即可


        class Cat
        {

            private int _age;
            public int Age { get => _age; set => _age = value; }

            private Color _color;
            public Color Color
            {
                set { _color = value; }
                get { return _color; }
            }


            public string Name
            {
                get; set;
            }


            //构造函数的几个方法
            public Cat() { }

            public Cat(string name,int age)
            {
                this.Age= age;
                this.Name = name;
            }

            public Cat(string name):this(name,0)
            {

            }


            public void Eat()
            {
                Console.WriteLine("小猫吃东西");
            }

            public void CatchMouse()
            {
                Console.WriteLine($"{this.Name}抓老鼠");
            }

          
        }

三种初始化对象的方法,对象初始化器

Cat cat = new Cat();
            cat.Name = "豆豆";
            cat.Color=Color.red;
            cat.Age = 18;
            cat.Eat();
            cat.CatchMouse();




            Cat c = new Cat("豆豆1", 25);
            c.Color = Color.gree;
            c.CatchMouse();

            Cat c2 = new Cat() { Name = "王", Color = Color.gree, Age = 30 };//对象初始化器,属性的顺序无要求
            c2.CatchMouse();
            c2.Eat();


            Console.ReadKey();

简单的构成

命名空间的使用:

1.命名空间------>类------->方法
2.解决类的重名问题
3.跨项目访问,访问其他的类
1.添加对将要访问项目的引用
2.导入命名空间
3.将要跨项目的类的访问修饰符,修改为public

计时器和stringBuilder

string 每次都开辟空间,造成存储的浪费

stringBuilder这会将它时间降低

 string str = "";
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < 1000; i++)
            {
                str += i;
            }

            stopwatch.Stop();
          
            Console.WriteLine(stopwatch.Elapsed);
            
            StringBuilder stringBuilder = new StringBuilder();


            Stopwatch s= new Stopwatch();//计时器
            s.Start();
            for (int i = 0; i < 1000; i++)
            {
                stringBuilder.Append(i);
            }
           s.Stop();
            Console.WriteLine(s.Elapsed);

================= 字符串的练习==================

split 分割字符

 Console.WriteLine("请按照输入一个日期");
            string date=Console.ReadLine();

            string [] dateArray=  date.Split('-');

            date = dateArray[0] + '年' + dateArray[1] + '月' + dateArray[2] + '日';
            Console.WriteLine(date);
            Console.ReadKey();

replace 替换字符 Contains 包含

Console.WriteLine("请输入一行字");
            string txt=Console.ReadLine();

            if (txt.Contains("王梦晴"))
            {
                txt=txt.Replace("王梦晴","别想她");
            }

            Console.WriteLine(txt);

            Console.ReadKey();

Substring 截取字符串

//取子字符串Substring 
            Console.WriteLine("请输入一行字");
            string txt = Console.ReadLine();           

            Console.WriteLine(txt.Substring(0, 5));

            Console.ReadKey();

StartsWith 以某个字符开头 Endwith 以某个字符结尾

Console.WriteLine("请输入一行字");
            string txt = Console.ReadLine();

            if (txt.StartsWith("你好,王豆豆"))
            {
                Console.WriteLine(txt);
            }
            else
            {
                Console.WriteLine("再见");
            }
            if (txt.EndsWith("再见"))
            {
                Console.WriteLine("还能再见吗,王豆豆");
            }
            else
            {
                Console.WriteLine(txt);
            }
            Console.ReadKey();

IndexOf 和 LastIndexOf 某个字符开头的下标(索引) 最 后一个的

 Console.WriteLine("请输入一行字");
            string txt = Console.ReadLine();
            Console.WriteLine(txt.IndexOf("豆"));
            Console.WriteLine(txt.LastIndexOf("豆"));

            Console.ReadKey();

Trim 清除空格

Console.WriteLine("请输入一行字");
            string txt = Console.ReadLine();

            Console.WriteLine(txt.Trim()+"p");
            Console.WriteLine(txt.TrimStart()+"p");
            Console.WriteLine(txt.TrimEnd() + "p");

string.join() 将某个进行字符串分割并输出

 Console.WriteLine("请输入一行字");
            string txt = Console.ReadLine();

            string[] names = { "小胡", "小王", "小豆豆", "我爱你" };
            char [] txtArray=txt.ToCharArray();
            Console.WriteLine(string.Join("|",txtArray)); 
            Console.WriteLine(string.Join("|",names)); 

            Console.ReadKey();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值