C # basic concepts for learning question 25 (Part 2) page 1/2

Source: Internet
Author: User

6. What is the sealed modifier?
A:
Sealed modifier indicates sealed
When used for a class, it indicates that the class cannot be inherited and cannot be used with abstract at the same time, because these two modifiers are mutually exclusive in meaning.
When used for methods and attributes, it indicates that the method or attribute cannot be inherited and must be used together with the override keyword, because the method or attribute using the sealed modifier must be a virtual Member of the base class.
It is usually used to implement third-party class libraries that do not want to be inherited by the client, or for classes that do not need to be inherited to prevent hierarchy confusion caused by misuse of inheritance.
The proper use of the sealed modifier can also improve the running efficiency, because the inherited class will overwrite the member.
Example:
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace Example06
{
Class Program
{
Class
{
Public virtual void F ()
{
Console. WriteLine ("A.F ");
}
Public virtual void G ()
{
Console. WriteLine ("A. G ");
}
}
Class B:
{
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 ();
}
}
}
Result:
Class B can override two virtual functions when inheriting Class ,:

Because the F method is sealed in Class B, Class C can only override one function when inheriting Class B ,:


Console output result. The class C method F can only be the implementation of this method in the output class B:
A.F
A. G
B. F
B .G
B. F
C. G

7. What is the difference between override and overload?
A:
Override indicates rewriting, which is used to inherit the Implementation of Virtual members in the base class.
Overload indicates overload. It is used to implement different parameters (including different types or numbers) of methods with the same name 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 Left, 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 ();
}
}
}
Result:
BaseClass. F
DeriveClass. F
Add for Int: 3
Add for int: 3.3

8. What is an index indicator?
A:
The class that implements index indicator (indexer) can use the object after its instance like an array, but unlike the array, the parameter type of index indicator is not limited to int
Simply put, it is essentially 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 ToString to facilitate 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;
}
}
// Implement the index accesser
Public Point this [int Index]
{
Get
{
Return points [Index];
}
}
}
// Thanks to watson hua (http://huazhihao.cnblogs.com/) for your guidance
// The index indicator is essentially a parameter attribute, and the parameter is not limited to int.
Class WeatherOfWeek
{
Public string this [int Index]
{
Get
{
// Note that break is not required because return is used in the case section.
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 switch syntax
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 ();
}
}
}
Result:
X: 0, Y: 0
X: 1, Y: 0.841470984807897
X: 2, Y: 0.909297426825682
X: 3, Y: 0.141120008059867
X: 4 and y:-0.756802495307928
X: 5 and 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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.