• c#基础一


    1、变量的命名
    首先我们要保证的就是变量的名称一定要有意义(就是我们看到了变量的名字,就知道变量在这段程序中的作用)
    Camel:多余用给变量或者字段命名,第一个单词的首字母小写,其余每个单词的首字母大写。
    我们给字段命名,前面必须加下划线。
    _highSchoolStudent
    Pascal:要求我们每个单词的首字母都要大写,其余每个单词的首字母小写。
    HighSchoolStudent

    int max= GetMax();
    int min= GetMin();

    2、进程
    一个应用程序就是一个进程,而一个进程又是由多个线程组成的。
    进程帮助我们在内存中分配应用程序执行所需要的空间。

    namespace _01进程
    {
        class Program
        {
            static void Main(string[] args)
            {
                //存储着我们当前正在运行的进程
                Process[] pro = Process.GetProcesses();
                foreach (var item in pro)
                {
                //    //item.Kill();不试的不是爷们
                    Console.WriteLine(item.ProcessName);
                    Console.ReadKey();
                }
    
                //使用进程来打开应用程序
                //Process.Start("notepad");//打开记事本
                //Process.Start("mspaint");//打开画图工具
                //Process.Start("iexplore", "http://www.baidu.com");
                //Process.Start("calc");//打开计算器
    
    
                //使用进程来打开文件
    
    
                //封装我们要打开的文件 但是并不去打开这个文件
          //      ProcessStartInfo psi = new ProcessStartInfo(@"C:UsersSpringRainDesktop打开文件练习.exe");
                //创建进程对象
          //      Process pro = new Process();
                //告诉进程要打开的文件信息
          //      pro.StartInfo = psi;
                //调用函数打开
          //      pro.Start();
         //       Console.ReadKey();
            }
        }
    View Code

    3.文件的处理

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _02打开文件练习
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("请输入要打开的文件所在的路径");
                string filePath = Console.ReadLine();
                Console.WriteLine("请输入要打开的文件的名字");
                string fileName = Console.ReadLine();
    
                //通过简单工厂设计模式返回父类
    
                BaseFile bf = GetFile(filePath, fileName);
                if (bf != null)
                {
                    bf.OpenFile();
                }
                Console.ReadKey();
            }
    
            static BaseFile GetFile(string filePath,string fileName)
            {
                BaseFile bf = null;
                string strExtension = Path.GetExtension(fileName);//3.txt
                switch (strExtension)
                { 
                    case ".txt":
                        bf = new TxtFile(filePath, fileName);
                        break;
                    case ".avi":
                        bf = new AviFile(filePath, fileName);
                        break;
                    case ".mp4":
                        bf = new MP4File(filePath, fileName);
                        break;
                }
                return bf;
            }
    
        }
    
        class BaseFile
        { 
            //字段、属性、构造函数、函数、索引器
            private string _filePath;
            public string FilePath//ctrl+R+E
            {
                get { return _filePath; }
                set { _filePath = value; }
            }
    
            //自动属性 prop+两下tab
            public string FileName { get; set; }
    
            public BaseFile(string filePath, string fileName)
            {
                this.FilePath = filePath;
                this.FileName = fileName;
            }
    
          
            //设计一个函数  用来打开指定的文件
            public void OpenFile()
            {
                ProcessStartInfo psi = new ProcessStartInfo(this.FilePath + "\" + this.FileName);
                Process pro = new Process();
                pro.StartInfo = psi;
                pro.Start();
            }
        }
    
        class TxtFile : BaseFile
        { 
            //因为子类会默认调用父类无参数的构造函数
    
            public TxtFile(string filePath, string fileName)
                : base(filePath, fileName)
            { }
        }
    
    
        class MP4File : BaseFile
        {
            public MP4File(string filePath, string fileName)
                : base(filePath, fileName)
            { }
        }
    
        class AviFile : BaseFile
        {
            public AviFile(string filePath, string fileName)
                : base(filePath, fileName)
            { }
        }
    }
    View Code

    3、面向对象

    类决定了对象将要拥有的属性和行为。
    封装:
    --->减少了大量的冗余代码
    --->封装将一坨很难理解的功能封装起来,但是对外提供了一个很简单的使用接口。我们会使用就OK。
    继承:
    --->减少了类中的冗余代码
    --->让类与类产生了关系,为多态打下了基础。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _05动物类继承
    {
        class Program
        {
            static void Main(string[] args)
            {
                //实现多态:声明父类去指向子类对象
                Animal[] a = { new Cat(), new Dog(), new Pig() };
    
                for (int i = 0; i < a.Length; i++)
                {
                    a[i].Bark();
                    a[i].Drink();
                    a[i].Eat();
                }
                Console.ReadKey();
            }
        }
        abstract class Animal
        {
            //抽象成员只能存在于抽象类中
            public abstract void Bark();//父类没有办法确定子类如何去实现
            public void Eat()
            {
                Console.WriteLine("动物可以舔着吃");
            }
    
            public void Drink()
            {
                Console.WriteLine("动物可以舔着喝");
            }
        }
    
        //一个子类继承了一个抽象的父类,那么这个子类必须重写这个抽象父类中的所有抽象成员
        class Cat : Animal
        {
            public override void Bark()
            {
                Console.WriteLine("猫咪喵喵的叫");
            }
    
          
        }
        class Dog : Animal
        {
            public override void Bark()
            {
                Console.WriteLine("狗狗旺旺的叫");
            }
        }
        class Pig : Animal
        {
            public override void Bark()
            {
                Console.WriteLine("猪猪哼哼的叫");
            }
        }
    
    }
    View Code


    特性:
    单根性:一个子类只能有一个父类
    传递性:爷爷类 爹类 儿子类
    里氏转换:
    1、子类可以赋值给父类
    2、如果父类中装的是子类对象,则可以将这个父类转换为对应的子类对象

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _06里氏转换
    {
        class Program
        {
            static void Main(string[] args)
            {
                Person person = new Student();
    
                //if (person is Teacher)
                //{
                //    ((Teacher)person).TeacherSayHello();
                //}
                //else
                //{
                //    Console.WriteLine("转换失败");
                //}
    
                Student s = person as Student;//将person转换为student对象
                if (s != null)
                {
                    s.StudentSayHello();
                }
                else
                {
                    Console.WriteLine("转换失败");
                }
                Console.ReadKey();
                //is as
            }
        }
        class Person
        {
            public void PersonSayHello()
            {
                Console.WriteLine("我是父类");
            }
        }
    
        class Student : Person
        {
            public void StudentSayHello()
            {
                Console.WriteLine("我是学生");
            }
        }
    
        class Teacher : Person
        {
            public void TeacherSayHello()
            {
                Console.WriteLine("我是老师");
            }
        }
    }
    View Code


    ---->关键字
    1、is:返回bool类型,指示是否可以做这个转换
    2、as:如果转换成功,则返回对象,否则返回null
    作用:我们可以将所有的子类都当做是父类来看,针对父类进行编程,写出通用的代码,适应需求的不断改变。
    多态:
    --->虚方法
    virtual override

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _07虚方法
    {
        class Program
        {
            static void Main(string[] args)
            {
                //员工九点打卡  经理11点打卡 程序猿不打卡
    
               // Employee emp = new Employee();
               //// emp.DaKa();
               // Manager m = new Manager();
               // //m.DaKa();
               // Programmer p = new Programmer();
               // //p.DaKa();
    
              //  Employee e = m;
    
                //Employee[] emps = { emp, m, p };
                //for (int i = 0; i < emps.Length; i++)
                //{
                //    //if (emps[i] is Manager)
                //    //{
                //    //    ((Manager)emps[i]).DaKa();  
                //    //}
                //    //else if (emps[i] is Programmer)
                //    //{
                //    //    ((Programmer)emps[i]).DaKa();
                //    //}
                //    //else
                //    //{
                //    //    emps[i].DaKa();
                //    //}
                //    emps[i].DaKa();
                //}
    
                Employee emp = new Programmer();//new Manager(); //new Employee();
                emp.DaKa();
    
    
                Console.ReadKey();
            }
        }
    
        class Employee
        {
            public virtual void DaKa()
            {
                Console.WriteLine("员工九点打卡");
            }
        }
    
        class Manager : Employee
        {
            public override void DaKa()
            {
                Console.WriteLine("经理11点打卡");
            }
        }
    
        class Programmer : Employee
        {
            public override void DaKa()
            {
                Console.WriteLine("程序猿不打卡");
            }
        }
    }
    View Code


    --->抽象类
    abstract override

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _08抽象类
    {
        class Program
        {
            static void Main(string[] args)
            {
                //抽象类不允许创建对象
                Animal a = new Cat(); //new Dog();
                a.Bark();
                Console.ReadKey();
            }
        }
    
        abstract class Animal
        {
            public abstract void Bark();
    
            private string _name;
    
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
    
            int _age;
    
            public int Age
            {
                get { return _age; }
                set { _age = value; }
            }
    
            public Animal(string name, int age)
            {
                this.Name = name;
                this.Age = age;
            }
        }
    
        class Dog : Animal
        {
    
            public override void Bark()
            {
                Console.WriteLine("狗狗旺旺的叫");
            }
        }
    
        class Cat : Animal
        {
            public override void Bark()
            {
                Console.WriteLine("猫咪喵喵的叫");
            }
        }
    }
    View Code

    抽象类的特点

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _09抽象类特点
    {
        class Program
        {
            static void Main(string[] args)
            {
    
            }
        }
    
        abstract class Person
        {
            public abstract int SayHello(string name);
            // public abstract void Test();
        }
    
        class Student : Person
        {
            public override int SayHello(string name)
            {
                return 0;
            }
        }
    }
    View Code


    --->接口
    interface

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _11接口的使用
    {
        class Program
        {
            static void Main(string[] args)
            {
                //实现多态
                IKouLan kl = new Driver();//new Teacher();//new Student();
                kl.KouLan();
                Console.ReadKey();
            }
        }
    
        class Person
        {
            public void CHLSS()
            {
                Console.WriteLine("人类可以吃喝拉撒睡");
            }
        }
    
        class NoTuiPerson : Person
        {
    
        }
    
        class Student : Person, IKouLan
        {
            public void KouLan()
            {
                Console.WriteLine("学生可以扣篮");
            }
        }
        class Teacher : Person, IKouLan
        {
    
            public void KouLan()
            {
                Console.WriteLine("老师也可以扣篮");
            }
        }
    
        class Driver : Person, IKouLan
        {
            public void KouLan()
            {
                Console.WriteLine("司机也可以扣篮");
            }
        }
    
        interface IKouLan
        {
            void KouLan();
        }
    
    
        class NBAPlayer : Person
        {
            public void KouLan()
            {
                Console.WriteLine("NBA球员可以扣篮");
            }
        }
    }
    View Code


    4、关键字
    new
    1、创建对象
    --->在堆中开辟空间
    --->在开辟的堆空间中创建对象
    --->调用对象的构造函数
    2、隐藏父类的成员
    this
    1、代表当前类的对象
    2、显示的调用自己的构造函数
    base
    1、显示调用父类的构造函数
    2、调用父类的成员

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _03面向对象复习
    {
        class Program
        {
            static void Main(string[] args)
            {
                Person p = new Person('');
                //p.Name = "李四";
                //p.Age = -19;
                //Console.WriteLine(p.Age);
                Console.WriteLine(p.Gender);
                Console.ReadKey();
            }
        }
    
        class Person
        {
            //字段、属性、函数、构造函数....
            //字段:存储数据
            //属性:保护字段 get set
            //函数:描述对象的行为
            //构造函数:初始化对象,给对象的每个属性进行赋值
    
            string _name;
    
            public string Name//张三
            {
                get { return _name; }//去属性的值
                set
                {
                    if (value != "张三")
                    {
                        value = "张三";
                    }
                    _name = value;
                }//给属性赋值
            }
    
            int _age;
    
            public int Age
            {
                get
                {
                    if (_age < 0 || _age > 100)
                    {
                        return _age = 18;
                    }
                    return _age;
                }
                set { _age = value; }
            }
    
            public char Gender { get; set; }
    
    
            public Person(char gender)
            {
                if (gender != '' && gender != '')
                {
                    gender = '';
                }
                this.Gender = gender;
            }
        }
    
    }
    View Code

    6、接口
    ---->接口是一种能力
    ---->接口也是一种规范
    ---->如果你继承了这个接口,就必须按照接口的要求来实现这个接口。
    interface I开头...able结尾

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _14抽象类实现接口
    {
        class Program
        {
            static void Main(string[] args)
            {
                I1 i = new PersonDaughter();//new PersonSon(); //new Person();
                i.Test();
                Console.ReadKey();
            }
        }
    
        abstract class Person:I1
        {
            public void Test()
            {
                Console.WriteLine("抽象类实现接口");
            }
        }
    
        class PersonSon : Person
        { }
    
        class PersonDaughter : Person
        { }
    
    
        interface I1
        {
            void Test();
        }
    }
    View Code

    8、访问修饰符
    public private internal protected protected internal
    public:在哪都可以访问
    private:私有的,只能在当前类的内部进行访问
    internal:只能在当前程序集中访问。
    protected:受保护的,可以在当前类以及该类的子类中访问

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _04三个关键字
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Student s = new Student();
                //s.Name = "大黄老师";
                //s.Test();
    
    
                //Teacher t = new Teacher("大黄老师", 18, '女', 50, 50, 50);
                //t.ShowScore();
                //t.SayHi();
    
                //Teacher t2 = new Teacher("春老师", 18, '女');
                //t2.SayHi();
    
                Teacher t3 = new Teacher("张三", 100, 100, 100);
                t3.ShowScore();
                Console.ReadKey();
            }
        }
    
        class Person
        {
            public void SayHello()
            {
                Console.WriteLine("我是人类");
            }
    
    
        }
    
        class Student : Person
        {
            public new void SayHello()//彻底隐藏了父类的SayHello()
            {
                Console.WriteLine("我是学生");
            }
    
            public Student GetStudent()
            {
                return this;
            }
    
            public void GetPerson()
            {
                base.SayHello();
            }
    
            public string Name { get; set; }
    
            public void Test()
            {
                //局部变量优先级高于成员变量
                string Name = "春生老师";
                Console.WriteLine("我的名字叫{0}", this.Name);
            }
        }
    
    
        class Teacher
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public char Gender { get; set; }
    
            public int Chinese { get; set; }
            public int Math { get; set; }
            public int English { get; set; }
    
    
            public Teacher(string name, int age, char gender, int chinese, int math, int english)
            {
                this.Name = name;
                this.Age = age;
                this.Gender = gender;
                this.Chinese = chinese;
                this.Math = math;
                this.English = english;
            }
    
    
            public Teacher(string name, int age, char gender)
                : this(name, age, gender
                    , 0, 0, 0)
            {
    
            }
    
    
            public Teacher(string name, int chinese, int math, int english)
                : this(name, 0, '', chinese, math, english)
            { }
    
    
            public void SayHi()
            {
                Console.WriteLine("我叫{0},今年{1}岁了,我是{2}生", this.Name, this.Age, this.Gender);
            }
    
            public void ShowScore()
            {
                Console.WriteLine("我叫{0},我的总成绩是{1},平均成绩是{2}", this.Name, this.Chinese + this.Math + this.English, (this.Chinese + this.Math + this.English) / 3);
            }
        }
    }
    View Code

    注意点:
    1、能够修饰类的访问修饰符只有两个:public internal(默认就是internal)。
    2、在同一个项目中,public的权限跟internal是一样的。
    3、子类的访问权限不能高于父类。








































































  • 相关阅读:
    windows 2008 64位在指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配
    uva 11552 dp
    A*搜索算法
    Dalvik虚拟机总结
    J2SE基础:5.面向对象的特性2
    Linux/Android——input_handler之evdev (四)【转】
    Linux/Android——input子系统核心 (三)【转】
    Linux/Android——输入子系统input_event传递 (二)【转】
    【Android】Android输入子系统【转】
    Linux/Android——usb触摸屏驱动
  • 原文地址:https://www.cnblogs.com/cb1186512739/p/9481251.html
Copyright © 2020-2023  润新知