• C#基础系列第二篇


    前言: 完全是我在学习过程中记录的笔记,只不过分享一下让很多刚开始学习.net编程的人能够很快的学会C#语言

    1. 使用面向对象

    (1) 首先写一个类,来描述对象

     (2)使用这个类实例化对象

    (3)使用这个对象

    (1) 就是用来描述对象的模板

                1)通过特征     字段和属性

                2)行为,能力   方法

                       class Cup

                       {

                              //类的成员

                              //特征

                              public int Volume;

                              public int height;

                              public string mater;

                              //行为

                              public void PackAble()

                              {

                                     Console.WriteLine("我可以装水");

                              }

                       }

                       实例化:

                       static void Main(string[] args)

                       {

                              Cup cup = new Cup();

                              cup.Volume = 200;

                              cup.height = 15;

                              cup.mater = "玻璃";

                              cup.PackAble();

                              Console.ReadKey();

                       }

    1. 构造方法

    (1) 为字段赋初值

                语法:[public] 类名(参数)

                {

                       //成员及执行方法

                }

                public void DisPlay()

            {

                Console.WriteLine("我是一个{0}ml,高为{1}Cm,用{2}材料做的杯具", volume, height, mater);

            }

                public Cup(int volume, int height, string mater)

            {

                this.volume = volume;

                this.height = height;

                this.mater = mater;

            }

                //执行

                static void Main(string[] args)

            {

                Cup cup = new Cup(300, 20, "玻璃");

                cup.DisPlay();

                Console.ReadKey();

            }

     (2)如果写类的时候没有提供构造方法,系统会自动的提供一个无参构造方法

    (3)如果添加了自定义的构造方法,那么系统不会在添加无参构造方法属性

    1. 属性

    (1) 就是限制字段访问并提供字段访问方法的一个数据类型

     (2)属性就是方法

     (3)get表示获取值得那个方法

     (4)set表示设置那个值得方法

             class Cup

                {

                       //字段

                       private int volume;

                       private int height;

                       private string mater;

     

                       //属性

                       public int Volume

                       {

                              get { return volume; }

                              set

                              {

                                     if (value > 0)

                                     {

                                            volume = value;

                                     }

                                     else

                                     {

                                            Console.WriteLine("容量不能小于0");

                                     }

                              }

                       }

                }

                //执行

                static void Main(string[] args)

            {

                Cup cup = new Cup();

                cup.Volume = 100;

                Console.WriteLine(cup.Volume);

                Console.ReadKey();

            }

     (5)自动属性

                等价于一个字段+get,set属性(属性中无任何逻辑处理)

                语法:public 返回类型 属性名{get;set;}

                       public string Name { get; set; }

                       执行:

                       Person p = new Person();

                p.Name = "韩迎龙";

                Console.WriteLine(p.Name);

                Console.ReadKey();

                      

     (6)属性不具备存储数据的能力,存储数据是存储在字段里面的

     (7)提供get表示可以得到字段的数据(只读属性)

     (8)提供set表示可以为字段赋值(只写属性)

     (9)提供get,set则表示可读可写属性继承

    1. 电子计算器

    (1) 首先建立一个Calculator类,在这个类里面实现了计算的方法

         class Calculator
    
                {
    
                       private int num1;
    
                       private int num2;
    
                       private string oper;
    
                       public int Num1
    
                       {
    
                              get { return num1; }
    
                              set { num1 = value; }
    
                       }
    
                       public int Num2
    
                       {
    
                              get { return num2; }
    
                              set { num2 = value; }
    
                       }
    
                       public string Oper
    
                       {
    
                              get { return oper; }
    
                              set { oper = value; }
    
                       }
    
                       public int Calculating()
    
                       {
    
                              int res = 0;
    
                              switch (oper)
    
                              {
    
                                     case "+":
    
                                            res = num1 + num2;
    
                                            break;
    
                                     case "-":
    
                                            res = num1 - num2;
    
                                            break;
    
                                     case "*":
    
                                            res = num1 * num2;
    
                                            break;
    
                                     case "/":
    
                                            res = num1 / num2;
    
                                            break;
    
                              }
    
                              return res;
    
                       }
    
                }
    
         (2)在Main方法中实现输出信息
    
                static void Main(string[] args)
    
            {
    
                //找到对象
    
                Calculator cal = new Calculator();
    
                //输入数字与运算符
    
                Console.Write("请输入第一个数字:");
    
                cal.Num1 = GetInputNum();
    
                Console.Write("请输入第一个数字:");
    
                cal.Num2 = GetInputNum();
    
                Console.Write("请输入运算符:");
    
                cal.Oper = Console.ReadLine();
    
                //使用对象的计算方法
    
                int res = cal.Calculating();
    
                //得到结果
    
                Console.WriteLine("{0}{1}{2}={3}", cal.Num1, cal.Oper, cal.Num2, res);
    
                Console.ReadKey();
    
            }
    
            public static int GetInputNum()
    
            {
    
                return GetInputNum(int.MinValue, int.MaxValue);
    
            }
    
            /// <summary>
    
            /// 完成int类型数据的输入,并返回,要求输入的数字在0到给定的数字之间
    
            /// </summary>
    
            /// <param name="max">给定的数字的上限</param>
    
            /// <returns></returns>
    
            public static int GetInputNum(int max)
    
            {
    
                return GetInputNum(0, max);
    
            }
    
            /// <summary>
    
            /// 完成int数字的输入,要求在给定范围之间
    
            /// </summary>
    
            /// <param name="min">给定范围的下线</param>
    
            /// <param name="max">给定范围的上线</param>
    
            /// <returns></returns>
    
            public static int GetInputNum(int min, int max)
    
            {
    
                string str = Console.ReadLine();
    
                int num;
    
                while (true)
    
                {
    
                    try
    
                    {
    
                        num = Convert.ToInt32(str);
    
                        if (num > min && num < max)
    
                        {
    
                            break;
    
                        }
    
                       Console.Write("输入数字不再{0}到{1}之间,请重新输入", min, max);
    
                        str = Console.ReadLine();
    
                    }
    
                    catch
    
                    {
    
                        Console.Write("输入有误,请重新输入");
    
                        str = Console.ReadLine();
    
                    }
    
                }
    
                return num;
    
            }
    1. 继承

    (1) 有一个学生,老师和校长做自我介绍

                1)将学生,老师和校长都有的特征先"打包"

                2)再写学生,老师和校长的时候将这个"包"导入进来

                3)通过继承来实现

                4)写一个父类

                5)再写子类的时候调用父类

                ->首先写一个父类Person,实现他们三个都有的方法

                       class Person

                       {

                              private string name;

                              private int age;

                              private char gender;

     

                              public string Name

                              {

                                     get { return name; }

                                     set { name = value; }

                              }

                              public int Age

                              {

                                     get { return age; }

                                     set { age = value; }

                              }

                              public char Gender

                              {

                                     get { return gender; }

                                     set { gender = value; }

                              }

                              public void SayHi()

                              {

                       Console.Write("你好,我是{0},今年{1}岁了,我是{2}的", name, age, gender);

                              }

                       }

                ->然后写Student类

                       class Student : Person

                       {

                              private string classname;

                              private string hobby;

                              public string Classname

                              {

                                     get { return classname; }

                                     set { classname = value; }

                              }

                              public string Hobby

                              {

                                     get { return hobby; }

                                     set { hobby = value; }

                              }

                              public void SayHello()

                              {

                                     SayHi();

                              Console.WriteLine(",我的班级是{0},我的爱好是{1}", classname, hobby);

                              }

                       }

                ->写Main函数

                       static void Main(string[] args)

                       {

                              Student stu = new Student();

                              stu.Name = "韩迎龙";

                              stu.Age = 23;

                              stu.Gender = '男';

                              stu.Classname = ".net";

                              stu.Hobby = "编程";

                              stu.SayHello();

                              Console.ReadKey();

                       }

                ->其余两个类(老师和校长基本一样)

         (2)构造方法实现传参

                public Student(string name, int age, char gender, string classname, string hobby)

            {

                this.Name = name;

                this.Age = age;

                this.Gender = gender;

                this.classname = classname;

                this.hobby = hobby;

            }

                Student stu = new Student("韩迎龙", 23, '男', ".net", "编程");

            stu.SayHello();

            Console.ReadKey();

         (3)在创建子类的时候会默认的调用父类的无参构造方法

                1)创建对象的时候构造方法的执行顺序

         (4)为什么使用继承

                1)代码重用

                2)实现多态(里氏转换原则)

         (5)继承的特征

                1)单根行

                2)传递性

                3)派生于object类型

         注释:写类的时候字段的顺序,成员方法的顺序是不用考虑的,在一个项目中类型的顺序也是不用考虑的

    1. 访问修饰符

    (1) protected

                1)表示受保护的字段,在本类和子类中可以访问

         (2)this

                1)指定当前类提供的成员

                2)可以调用本类的构造方法

                       public 类名()

                       {

                              //代码1

                       }

                       public 类名(string str)

                              :this()

                       {

                              //代码1

                              //代码2

                       }

                       举例说明:

                       class MyClass

                       {

                              public MyClass()

                              {

                                     Console.WriteLine("我没有参数");

                              }

                              public MyClass(string str)

                                     : this()

                              {

                                     Console.WriteLine("我是字符串{0}", str);

                              }

                              static void Main(string[] args)

                              {

                                     MyClass mc = new MyClass("韩迎龙");

                                     Console.ReadKey();

                              }

                       }

         (3)base

                1)调用父类构造方法

                2)指定父类提供的成员

    1使用类库实现方法

      (1)类名与命名空间至少有一个要记得
      (2)每次使用的时候可以添加引用即可

  • 相关阅读:
    VS2012 for SharePoint2013 Tool安装
    SharePoint 2013网站管理网站策略(关闭和删除策略)
    呼风唤雨的公交
    ASP、Access、80040e14、保留关键字、INSERT INTO 语句的语法错误
    表格边框设置
    DotNet开发中关于SQLServer连接的两种方法之比较
    SQL语句集锦
    动态网页设计笔记
    SQL SERVER中日期 where 问题的解决
    配置你的ASP.NET运行环境
  • 原文地址:https://www.cnblogs.com/hanyinglong/p/2709457.html
Copyright © 2020-2023  润新知