美文网首页
C# 第三节

C# 第三节

作者: allen151 | 来源:发表于2019-11-21 19:29 被阅读0次

    C# 运算符重载

    您可以重定义或重载 C# 中内置的运算符。因此,程序员也可以使用用户自定义类型的运算符。重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的。与其他函数一样,重载运算符有返回类型和参数列表。

    重载运算符,方法名operator+:

    public static Box operator+ (Box b, Box c){
      Box box = new Box();
      box.length = b.length + c.length ;
      box.height = b.height + c.height ;
      box.breadth = b.breadth + c.breadth ;
      return box ;
     }
    

    例如,请看下面的函数:

    using System ;
    namespace OperatorOvlApplication{
        class Box{
            private double length ;
            private double breadth ;
            private double height ;
            
            public void setLength(double len){
                length = len ;
            }
            public void setBreadth(double bre){
                breadth = bre ;
            }
            public void setHeight(double hei){
                height = hei ;
            }
            public double getVolume(){
                return length * breadth * height ;
            }
            public static Box operator+ (Box b, Box c){
                Box box = new Box();
                box.length = b.length + c.length ;
                box.height = b.height + c.height ;
                box.breadth = b.breadth + c.breadth ;
                return box ;
            }
        }
        class Tester{
            static void Main(string[] args){
                Box box1 = new Box();
                Box box2 = new Box();
                Box box3 = new Box();
                double volume = 0.0 ;
                
                //Box1 详述
                box1.setLength(6.0);
                box1.setHeight(5.0);
                box1.setBreadth(7.0);
                
                //Box2 详述
                box2.setLength(12.0);
                box2.setBreadth(13.0);
                box2.setHeight(10.0);
                
                //Box1 的体积
                volume = box1.getVolume();
                Console.WriteLine("Box1的体积是:{0}", volume);
                
                //Box2 的体积
                volume = box2.getVolume();
                Console.WriteLine("Box2的体积是:{0}",volume);
                
                //把两个对象相加
                box3 = box1 + box2 ;
                
                //Box3 的体积
                volume = box3.getVolume();
                Console.WriteLine("Box3的体积是:{0}",volume);
                Console.ReadKey();
            }
        }
    }
    

    C# 接口(Interface)

    接口定义了所有类继承接口时应遵循的语法合同。接口定义了语法合同 "是什么" 部分,派生类定义了语法合同 "怎么做" 部分。
    接口定义了属性、方法和事件,这些都是接口的成员。接口只包含了成员的声明。成员的定义是派生类的责任。接口提供了派生类应遵循的标准结构。
    接口使得实现接口的类或结构在形式上保持一致。
    抽象类在某种程度上与接口类似,但是,它们大多只是用在当只有少数方法由基类声明由派生类实现时。

    定义接口: MyInterface.cs

    interface IMyInterface
    {
        void MethodToImplement();
    }
    

    以上代码定义了接口 IMyInterface。通常接口命令以 I 字母开头,这个接口只有一个方法 MethodToImplement(),没有参数和返回值,当然我们可以按照需求设置参数和返回值。
    值得注意的是,该方法并没有具体的实现。

    接口继承:

    如果一个接口继承其他接口,那么实现类或结构就需要实现所有接口的成员。

    C# 命名空间(Namespace)

    命名空间的设计目的是提供一种让一组名称与其他名称分隔开的方式。在一个命名空间中声明的类的名称与另一个命名空间中声明的相同的类的名称不冲突。

    namespace namespace_name
    {
       // 代码声明
    }
    

    完整的一个实例:

    using System;
    namespace first_space
    {
       class namespace_cl
       {
          public void func()
          {
             Console.WriteLine("Inside first_space");
          }
       }
    }
    namespace second_space
    {
       class namespace_cl
       {
          public void func()
          {
             Console.WriteLine("Inside second_space");
          }
       }
    }   
    class TestClass
    {
       static void Main(string[] args)
       {
          first_space.namespace_cl fc = new first_space.namespace_cl();
          second_space.namespace_cl sc = new second_space.namespace_cl();
          fc.func();
          sc.func();
          Console.ReadKey();
       }
    }
    

    using 关键字

    using 关键字表明程序使用的是给定命名空间中的名称。例如,我们在程序中使用 System 命名空间,其中定义了类 Console。

    嵌套命名空间

    命名空间可以被嵌套,即您可以在一个命名空间内定义另一个命名空间。

    静态声明的语句只执行一次,因此,不必要特定的对象调用,可以直接使用类名调用。

    相关文章

      网友评论

          本文标题:C# 第三节

          本文链接:https://www.haomeiwen.com/subject/ozdyictx.html