The console outputs the result, the method F of class C can only be the implementation of this method in class B:
What is the difference between and overload?
answer:
override means rewrite, used to inherit the class to implement virtual members in the base class
overload represents overload, used for implementation of different parameters of methods with the same name (including different types or different numbers) in the same class.
Example:
using System;
using ;
using ;
namespace Example07
{
class Program
{
class BaseClass
{
public virtual void F()
{
("");
}
}
class DeriveClass : BaseClass
{
public override void F()
{
();
("");
}
public void Add(int Left, int Right)
{
("Add for Int: {0}", Left + Right);
}
public void Add(double Left, double Right)
{
("Add for int: {0}", Left + Right);
}
}
static void Main(string[] args)
{
DeriveClass tmpObj = new DeriveClass();
();
(1, 2);
(1.1, 2.2);
();
}
}
}
result:
Add for Int: 3
Add for int: 3.3
8.What is an index indicator?
answer:
The class that implements an indexer can use the object after its instance like an array, but unlike an array, the parameter type of the indexer is not limited to int
Simply put, its essence is a parameter-containing attribute
Example:
using System;
using ;
using ;
namespace Example08
{
public class Point
{
private double x, y;
public Point(double X, double Y)
{
x = X;
y = Y;
}
//Rewrite the ToString method to facilitate output
public override string ToString()
{
return ("X: {0} , Y: {1}", x, y);
}
}
public class Points
{
Point[] points;
public Points(Point[] Points)
{
points = Points;
}
public int PointNumber
{
get
{
return ;
}
}
//Implement index accessor
public Point this[int Index]
{
get
{
return points[Index];
}
}
}
//Thanks to watson hua(/) for his advice
//The essence of the index indicator is to contain parameter attributes, and the parameters are not limited to int
class WeatherOfWeek
{
public string this[int Index]
{
get
{
//Note that the case segment uses return to return directly so no break is required
switch (Index)
{
case 0:
{
return "Today is cloudy!";
}
case 5:
{
return "Today is thundershower!";
}
default:
{
return "Today is fine!";
}
}
}
}
public string this[string Day]
{
get
{
string TodayWeather = null;
//Standard writing method of switch
switch (Day)
{
case "Sunday":
{
TodayWeather = "Today is cloudy!";
break;
}
case "Friday":
{
TodayWeather = "Today is thundershower!";
break;
}
default:
{
TodayWeather = "Today is fine!";
break;
}
}
return TodayWeather;
}
}
}
class Program
{
static void Main(string[] args)
{
Point[] tmpPoints = new Point[10];
for (int i = 0; i < ; i++)
{
tmpPoints[i] = new Point(i, (i));
}
Points tmpObj = new Points(tmpPoints);
for (int i = 0; i < ; i++)
{
(tmpObj[i]);
}
string[] Week = new string[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Staurday"};
WeatherOfWeek tmpWeatherOfWeek = new WeatherOfWeek();
for (int i = 0; i < 6; i++)
{
(tmpWeatherOfWeek[i]);
}
foreach (string tmpDay in Week)
{
(tmpWeatherOfWeek[tmpDay]);
}
();
}
}
}
result:
X: 0 , Y: 0
X: 1 , Y: 0.841470984807897
X: 2 , Y: 0.909297426825682
X: 3 , Y: 0.141120008059867
X: 4 , Y: -0.756802495307928
X: 5 , Y: -0.958924274663138
X: 6 , Y: -0.279415498198926
X: 7 , Y: 0.656986598718789
X: 8 , Y: 0.989358246623382
X: 9 , Y: 0.412118485241757
Today is cloudy!
Today is fine!
Today is fine!
Today is fine!
Today is fine!
Today is thundershower!
Today is cloudy!
Today is fine!
Today is fine!
Today is fine!
Today is fine!
Today is thundershower!
Today is fine!