xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

.NET 面试题: C# override && overloading (C# 覆写 && 重载)

1

1

1

.NET 面试题, C# ,override , overloading, 覆写, 重载,.NET,ASP.NET,

 

 

方法名相同,参数的个数和类型相同,内部实现不同.

The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.

要扩展或修改继承的方法、属性、索引器或事件的抽象实现或虚实现,必须使用 override 修饰符

 

Overloadable Operators (C# Programming Guide)

Operator Overloading

C# Operators

方法名相同,参数的个数或类型不同.

C# allows user-defined types to overload operators by defining static member functions using the operator keyword. Not all operators can be overloaded, however, and others have restrictions, as listed in this table:

 

1

override demo:


abstract class ShapesClass
{
    abstract public int Area();
}
class Square : ShapesClass
{
    int side = 0;

    public Square(int n)
    {
        side = n;
    }
    // Area method is required to avoid
    // a compile-time error.
    public override int Area()
    {
        return side * side;
    }

    static void Main() 
    {
        Square sq = new Square(12);
        Console.WriteLine("Area of the square = {0}", sq.Area());
    }

    interface I
    {
        void M();
    }
    abstract class C : I
    {
        public abstract void M();
    }

}
// Output: Area of the square = 144

 

 

C# demo

 

 

 

 

1

overloading demo:

public static Complex operator +(Complex c1, Complex c2)
    {
        Return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
    }

 

public static Complex operator +(Complex c1, Complex c2) =>
        new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);

    // Override ToString() to display a complex number 
    // in the traditional format:
    public override string ToString() => $"{this.real} + {this.imaginary}";

 

 

using System;
using System.Drawing;
 
class TestBaseCarClass
{
    static void Main()
    {
        Console.WriteLine(Add(1, 1));
    }
 
    static int Add(int a, int b)
    {
        return Add(a, b, 0, 0);
    }
 
    static int Add(int a, int b, int c)
    {
        return Add(a, b, c, 0);
    }
 
    static int Add(int a, int b, int c, int d)
    {
        return a + b + c + d;
    }
}

 

 

sing System;
using System.Text;
using System.Collections.Generic;
 
class Program
{
    static void Main()
    {
        myArrayList myList = new myArrayList();
 
        myList.Add("Hello");
        myList.Add("World");
        myList.Add("123456");
 
        Console.WriteLine(myList.ToString());
    }
}
 
class myArrayList : System.Collections.ArrayList
{
    public override string ToString()
    {
        StringBuilder result = new StringBuilder();
        string[] theItems = (string[])base.ToArray(typeof(string));
 
        foreach (string item in theItems)
        {
            result.AppendLine(item);
        }
        return result.ToString();
    }
}

 

1

1

 

 

参考链接:

Overload and Override:

https://social.msdn.microsoft.com/Forums/zh-CN/d76c1da8-3128-48dc-ad17-c667eebd0476/overload-and-override?forum=csharpgeneral

运算符(C# 参考):

https://msdn.microsoft.com/zh-cn/library/s53ehcz3.aspx

可重载运算符(C# 编程指南):

https://msdn.microsoft.com/zh-cn/library/8edha89s.aspx

C# 参考

Visual Studio 2015
 

https://msdn.microsoft.com/zh-cn/library/618ayhy6.aspx

 

1

1

1

1

1

1

1

1

posted @ 2016-08-10 08:50  xgqfrms  阅读(132)  评论(3编辑  收藏  举报