c# 关键字,运算符,表达式 -学习笔记

来源:互联网 发布:数据库在线设计 编辑:程序博客网 时间:2024/05/22 11:47
  关键字 
/* public 表示公共的,都能访问
         * private 表示私有的。
         * staic 表示静态的,可以用于类,变量和方法.
         * 
         * 用于变量时,表示这个成员被所有类的实例共享,访问的都是同一个内存地址中存储的值,存在堆上。可以直接通过类名来访问,只和类有关,不再和每个对象相关
         * 用于方法时,表示这个方法可以直接通过类名来访问,只和类有关,而且只能访问其他静态成员(实例成员可以访问静态成员)
         * 用于构造函数时,表示这个静态函数用于初始化静态字段,在引用任何静态成员之前,调用任何实例之前调用
         * 如果一个类只包含 静态方法,和属性,并且 标识为static 那么这个类就是静态类
         * 他不能创建实例,不能被继承
创建一个类,包含静态成员和静态方法,静态构造方法。
class staticClass
    {
        static int num ;
        static staticClass()
        {
            num = 5;
        }
        public staticClass()
        {
            ++num;
        }
        static public int  getNumber()
        {
            Console.WriteLine("the number is "+ num);
            return num;
        }
    }


            //第一次调用静态类,静态成员初始化为5
            Console.WriteLine(staticClass.getNumber());
            //创建一个普通类对象,此时类的构造方法中对于静态成员的操作成功
            staticClass test1 = new staticClass();
            //此时再查看静态成员发现静态成员改变,表明,该类所有实例访问的静态成员都指向同一个地址
            Console.WriteLine(staticClass.getNumber());




         * 静态类可以用为某个已经无法修改的类添加扩展方法,
         * static class extra
         * {
         *  public static void extraMethod (this targetClass class){...}
         * }
         * 第一个参数必须为this+类名  使用this关键字后,targetClass可以直接使用该方法




         * 
         * virtual 表示虚方法
         * 在一个实例方法前添加Virtual关键字可以让基类的所有派生类都能重写这个方法。,重写是需要标注关键字override
         * abstract 表示抽象
         * 可以用在类和方法上,抽象类无法被实例化,抽象方法在不能有方法体,只能在派生类中实现方法。如果一个类有抽象方法,则这个类也一定要声明为抽象的。
         * 抽象方法的作用是让派生类重写具体的方法。体现语言的多态性

abstract 例子请看后面计算器例子中的calculate类

* virtual 和 abstract 的区别

virtual只能用于修饰方法,abstract可以修饰方法也可以修饰类

virtual只是提供了一个基类方法,子类可以选择重写也可以选择不重写,不重写可以使用基类方法

abstract方法没有方法体,abstract类一定要被继承,被实例化以后,重写方法之后才能使用。



运算符
 * sbyte  System.SByte  8位有符号整数      (-2^7~2^7-1)
         * short  System.Int16  16位有符号整数     (-2^15~2^15-1)
         * int    System.Int32  32位有符号整数     (-2^31~2^31-1)
         * long   System.Int64  64位有符号整数     (-2^63~2^63-1)
         * 
         * byte   System.Byte   8位无符号整数        (0~2^8-1)
         * ushort  System.UInt16  16位有符号整数     (0~2^16-1)
         * uint    System.UInt32  32位有符号整数     (0~2^32-1)
         * ulong   System.UInt64  64位有符号整数     (0~2^64-1)
         * 
         * float  System.Single  32位单精度浮点数      
         * double System.Double  64位双精度浮点数
         * 
         * 
         * 
         *装箱:把值类型转换为引用类型
         *原理,把值强制转换成object类型, 运行库会为堆上的对象创建一个临时的引用类型“箱子”
         *拆箱:把引用类型转换为值类型
         * 注意,确保得到的值变量有足够的矿建去存贮拆箱的值中的所有字节
         * 
         * 
         * 比较方法
         * ReferenceEquals()用于比较引用,Equals()用于比较值
         * ==介于两者之间,唯一区别,值类型只用 == 比较时,需要先装箱,才能转化成引用,从而执行比较方法
         * 
         * checked 关键字 用于检查一部分类型转换是否溢出
         * 
         * 运算符在某些需要的情况下可以被重载。例如在矢量求和中,就可以把两个矢量之间的加法通过重载运算符来简化运算。
         * 
         struct Vector
    {
        public double x, y, z;
        public Vector(double x, double y, double z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }


        public Vector(Vector rhs)
        {
            x = rhs.x;
            y = rhs.y;
            z = rhs.z;
        }
        //运算符的重载
        public static Vector operator +(Vector lhs, Vector rhs)
        {
            Vector result = new Vector(lhs);
            result.x += rhs.x;
            result.y += rhs.y;
            result.z += rhs.z;




            return result;
        }
        public static Vector operator *(Vector lhs, Vector rhs)
        {
            Vector result = new Vector(lhs);
            result.x -= lhs.x;
            result.y -= rhs.y;
            result.z += rhs.z;
            return result;
        }


        //重写tostring方法
        public override string ToString()
        {
            return " ( "+ x +" , " + y + " , " + z +" ) ";
        }


      上面定义了一个Vector 结构,在这个结构中,我们重载了+和 *运算符(注意,*重载时,我定义的方法是前两个相减,后一个相加)
      随后我们进行实验
            Vector vect1 = new Vector(3.0, 2.0, 2.0);




            Vector vect2 = new Vector(3.0, 2.0, 2.0);




            Vector vect3 = vect2 + vect1;
            Vector vect4 = vect2 * vect1;
            Console.WriteLine(vect1.ToString());
            Console.WriteLine(vect2.ToString());
            Console.WriteLine(vect3.ToString());
            Console.WriteLine(vect4.ToString());


输出结果 
( 3 , 2 , 2 )
        ( 3 , 2 , 2 )
        ( 6 , 4 , 4 )
        ( 0 , 0 , 4 )
最后,是一个我自己写的小计算器。只能计算加减乘除 开根号,N次方。其中用到了try。catch语句 switch语句
           Console.WriteLine("choose one type to calculate");
            Console.WriteLine("enter 1 to choose +");
            Console.WriteLine("enter 2 to choose -");
            Console.WriteLine("enter 3 to choose *");
            Console.WriteLine("enter 4 to choose /");
            Console.WriteLine("enter 5 to choose √");
            Console.WriteLine("enter 6 to choose ^");








            int calculateType=0;
            bool begin = true;
            while (begin)
            {
                try
                {
                    string type = Console.ReadLine();
                    if (type == "")
                    {
                        begin = false;
                        break;
                    }
                    calculateType = Convert.ToInt32(type);
                    if (calculateType > 6 || calculateType < 1)
                        throw new IndexOutOfRangeException();
                    begin = false;
                }
                catch (IndexOutOfRangeException ex)
                {
                    Console.WriteLine("please enter a number between 1-6");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("please enter a number between 1-6");
                }   
            }


            double firstNum=0;
            Console.WriteLine("please enter the first number to calculate:");
            bool first = true;
            while (first)
            {
                try
                {
                    string type = Console.ReadLine();
                    if (type == "")
                    {
                        begin = false;
                        break;
                    }
                    firstNum = Convert.ToDouble(type);
                    first = false;
                }
                catch (FormatException ex)
                {
                    Console.WriteLine("Error message :"+ex.Message);
                    Console.WriteLine("please enter a number to calculate");
                }
            }


            //开根号方法特殊处理,因为只要一个参数
            if (calculateType == 5)
            {
                double sqrtResult = 0;   
                squareRoot sqrt = new squareRoot();
                sqrtResult = sqrt.getSqrtValue(firstNum);
                Console.WriteLine("The result is " + sqrtResult);
                return;
            }


            double secondNum=0;
            Console.WriteLine("please enter the second number to calculate:");
            bool second = true;
            while (second)
            {
                try
                {
                    string type = Console.ReadLine();
                    if (type == "")
                    {
                        begin = false;
                        break;
                    }
                    secondNum = Convert.ToDouble(type);
                    second = false;
                }
                catch (FormatException ex)
                {
                    Console.WriteLine("Error message :" + ex.Message);
                    Console.WriteLine("please enter a number to calculate");
                }
            }




            double result = 0;
            switch (calculateType)
            {
                case 1:
                    AddCalculate add = new AddCalculate();
                   result= add.getValue(firstNum,secondNum);
                    break;
                case 2:
                    Subtraction sub = new Subtraction();
                    result = sub.getValue(firstNum, secondNum);
                    break;
                case 3:
                    multiplication mul = new multiplication();
                    result = mul.getValue(firstNum, secondNum);
                    break;
                case 4:
                    division div = new division();
                    result = div.getValue(firstNum, secondNum);
                    break;
                case 5:
                case 6:
                    pow po = new pow();
                    result = po.getValue(firstNum, secondNum);
                    break;
                default:




                    break;
            }
            Console.WriteLine("The result is " +result);

其中 calculate 类是我写的一个抽象类,相当于所有计算方法的基类,其他所有计算方法都是这个类的派生类,我个人认为这样看起来比较系统,通过抽象方法在
派生类中的实现, 能够比较好的实现不同的计算方式。应该有更好的方式,欢迎指正。



    public abstract class calculate
    {
        public abstract double getValue(double firstValue,double secondValue);
    }


     其他计算类都是calculate 类的派生类,只有计算方式不同,我展示一下加法的
class AddCalculate:calculate
    {
        public override double getValue(double firstValue, double secondValue)
        {
            double Value = firstValue + secondValue;
            return Value;
        }
    }
   代码已经上传github:https://github.com/lcxxxc/UWP_Readness 
study_day2 文件夹
0 0
原创粉丝点击