What does the 6.sealed modifier do?
For:
The sealed modifier indicates a seal
For a class, it means that the class can no longer be inherited and cannot be used concurrently with abstract because the two modifiers are mutually exclusive in meaning
When used for methods and properties, indicates that the method or property can no longer be inherited and must be used with the override keyword because the method or property using the sealed modifier is definitely the corresponding virtual member in the base class
Typically used to implement a Third-party class library without being inherited by the client or for classes that do not need to be inherited to prevent abuse of inheritance resulting in a hierarchical system confusion
Proper use of the sealed modifier can also improve operational efficiency, because inheriting classes override that member
Example:
Using System;
Using System.Collections.Generic;
Using System.Text;
Namespace Example06
{
Class Program
{
Class A
{
public virtual void F ()
{
Console.WriteLine ("A.F");
}
public virtual void G ()
{
Console.WriteLine ("A.G");
}
}
Class B:a
{
public sealed override void F ()
{
Console.WriteLine ("B.f");
}
public override void G ()
{
Console.WriteLine ("B.G");
}
}
Class C:b
{
public override void G ()
{
Console.WriteLine ("C.G");
}
}
static void Main (string[] args)
{
New A (). F ();
New A (). G ();
New B (). F ();
New B (). G ();
New C (). F ();
New C (). G ();
Console.ReadLine ();
}
}
}
Results:
Class B can override two virtual functions when inheriting class A, as shown in the figure:
Because the F method is sealed in Class B, class C can only override one function when inheriting Class B, as shown in the figure:
Console output, the method F of class C can only be implemented for this method in output Class B:
A.f
A.g
B.f
B.g
B.f
C.g
What is the difference between 7.override and overload?
For:
Override represents an override for inheriting a class's implementation of a virtual member in a base class
Overload represents overloading for implementations of different parameters (including different types or numbers) of the same method in the same class
Example:
Using System;
Using System.Collections.Generic;
Using System.Text;
Namespace Example07
{
Class Program
{
Class BaseClass
{
public virtual void F ()
{
Console.WriteLine ("Baseclass.f");
}
}
Class Deriveclass:baseclass
{
public override void F ()
{
Base. F ();
Console.WriteLine ("Deriveclass.f");
}
public void Add (int left, int right)
{
Console.WriteLine ("Add for Int: {0}", left + right);
}
public void Add (double, double right)
{
Console.WriteLine ("Add for int: {0}", left + right);
}
}
static void Main (string[] args)
{
Deriveclass tmpobj = new Deriveclass ();
TMPOBJ.F ();
Tmpobj.add (1, 2);
Tmpobj.add (1.1, 2.2);
Console.ReadLine ();
}
}
}
Results:
Baseclass.f
Deriveclass.f
ADD for Int:3
ADD for int:3.3
8. What is an index indicator?
For:
A class that implements an index indicator (indexer) can use an object after its instance as an array, but unlike an array, the parameter type of the index indicator is not limited to int
In short, its essence is a parameter attribute
Example:
Using System;
Using System.Collections.Generic;
Using System.Text;
Namespace Example08
{
public class Point
{
Private double x, y;
Public point (Double X, double Y)
{
x = x;
y = y;
}
Rewrite the ToString method for easy output
public override string ToString ()
{
Return String.Format ("x: {0}, Y: {1}", X, y);
}
}
public class Points
{
Point[] points;
Public Points (point[] Points)
{
points = points;
}
public int Pointnumber
{
Get
{
Return points. Length;
}
}
Implementing an index accessor
Public Point This[int Index]
{
Get
{
return Points[index];
}
}
}
Thanks to Watson Hua (http://huazhihao.cnblogs.com/) 's guidance
The essence of the index indicator is the parameter attribute, and the argument is not limited to int
Class Weatherofweek
{
public string This[int Index]
{
Get
{
Note that the case section returns directly using return so no break is required
Switch (INDEX)
{
Case 0:
{
Return ' is cloudy! ';
}
Case 5:
{
Return ' is thundershower! ';
}
Default
{
Return ' is fine! ';
}
}
}
}
public string this[string Day]
{
Get
{
string todayweather = null;
The standard wording 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 < tmppoints.length; i++)
{
Tmppoints[i] = new Point (i, Math.sin (i));
}
Points tmpobj = new Points (tmppoints);
for (int i = 0; i < Tmpobj.pointnumber; i++)
{
Console.WriteLine (Tmpobj[i]);
}
string[] Week = new string[] {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Staurday"};
Weatherofweek Tmpweatherofweek = new Weatherofweek ();
for (int i = 0; i < 6; i++)
{
Console.WriteLine (Tmpweatherofweek[i]);
}
foreach (String tmpday in Week)
{
Console.WriteLine (Tmpweatherofweek[tmpday]);
}
Console.ReadLine ();
}
}
}
Results:
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!
current 1/2 page
1 2 Next read the full text