.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:
123456789101112131415161718192021222324252627282930313233343536abstract
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
1C# demo
1
overloading demo:
1234public
static
Complex
operator
+(Complex c1, Complex c2)
{
Return
new
Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
123456public
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}"
;
12345678910111213141516171819202122232425using
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;
}
}
1234567891011121314151617181920212223242526272829303132sing 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 2015https://msdn.microsoft.com/zh-cn/library/618ayhy6.aspx
1
1
1
1
1
1
1
1
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/5755329.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)