C # abstract classes and methods, overload, overwrite, and hide

Source: Internet
Author: User

 

From:

Http://www.51testing.com /? 61753/action_viewspace_itemid_76398.html

C # abstract classes and methods, overload, overwrite, and hide

16:38:07/personal classification: programming Learning

In the fourth example, the abstract classes and methods of C # Are reloaded, overwritten, and hidden.

 

Using system;

namespace testclassapp
{< br> ///


// class1 abstract description.
///
class class1
{< br> ///
// application Program .
//
[stathread]
static void main (string [] ARGs)
{< br> //
// todo: add Code here to start the application
//

Double Len = 2.5;
Double WID = 3.0;
Double rad = 4.1;
Rectangle arect = new rectangle ();
Arect. Length = Len;
Arect. width = WID;
Circle acirc = new circle (RAD );
Console. writeline ("area of rect is: {0}", arect. Area ());
Console. writeline ("area of CIRC is: {0}", acirc. Area ());
}
}

Abstract class shape // abstract base class, which cannot be instantiated
/* 1) declare an abstract class using the abstract keyword
* 2) as long as the class containing abstract methods is an abstract class (even if there is only one), when all the methods in an abstract class are abstract, we can define them as interfaces.
* A class can contain one or more abstract methods.
* 3) abstract classes can have non-Abstract METHODS (methods can be implemented in a specific way)
* 4) abstract classes cannot be instantiated.
* 5) the abstract class is implemented using ":", and the abstract method is implemented using the override keyword.
* 6) the abstract class can be inherited by the abstract class, and the result is still the abstract class.
* 7) after an abstract method is implemented, the modifier cannot be changed.
*/
{
Public const double Pi = 3.14; // constant
Protected Double X, Y; // Private, can inherit Variables
// Constructor
/*
1) The initial Object Instantiation process is called the construction phase. This process is completed by the constructor, And the constructor is the function used to initialize data.
2) all objects (classes) have a default constructor with no parameters and the same name as the class. However, a class can contain several constructor with parameters.
It is called a non-default constructor.
3) The constructor is called with the New Keyword, for example:
* Call the default constructor: Instance name of the class name = New Class Name ();
* Call non-default constructor: Instance name of the class name = New Class Name (parameter );
* 4) constructors and fields, attributes, and methods are the same.
* 5) the constructor is called only when the object is created. If no code is written or the constructor is not implemented, the class will also be created,
* This function only indicates that the object has an opportunity to set the initial value of the object when it is created.
*/
Public shape () // default constructor
{
X = y = 0;
}
Public shape (Double X, Double Y) // constructor with Parameters
{
This. x = X;
This. Y = y;
}
Public abstract double area (); // specifies the abstract method, which must be overloaded.
/*
* 1) declare an abstract method using the abstract keyword. The abstract method only contains the method definition, but there is no specific implementation method. It must be implemented by its subclass or subclass.
* 2) after the subclass inherits the abstract parent class, you can use the override keyword to overwrite the abstract methods in the parent class and perform specific implementation. You can also leave the abstract method to future generations for implementation,
* The subclass is still an abstract class and must be declared as abstract
* 3) the inherited abstract methods cannot be hidden.
*/
}

Class rectangle: Shape
{
Public rectangle (): Base (){}
Public rectangle (Double X, Double Y): Base (x, y) {}// use the base class Constructor
/*
1) implicit call of base class constructor: If base () is not explicitly specified (that is, base () is not used), The subclass automatically calls the default constructor of the base class.
2) display call: The above shows the call, using the base (parameter)
*/
Public override double area () // function overwrite. After the subclass inherits the abstract parent class, you can use the override keyword to overwrite the abstract methods in the parent class and perform specific implementation.
// You can leave the abstract method to future generations for implementation,
{
Return (x * y );
}
Public double length // attribute: rectangular Length
{
Get
{
Return X;
}
Set
{
If (value> 0) {x = value ;}
}
}
Public double width // attribute: rectangle width
{
Get
{
Return y;
}
Set
{
If (value> 0) {Y = value ;}
}
}

}

Class ellipse: Shape
{
Public ellipse (Double X, Double Y): Base (x, y) {}// use the constructors of the base class shape
Public override double area () // function Overwrite
{
Return pI * x * Y;
}
}
Class circle: Ellipse
{
Public circle (Double R): Base (R, 0) {}// use the base class ellipse Constructor
Public override double area () // function Overwrite
{
Return pI * x * X;
}
}
// Hide: Create a method in the subclass with the same signature as the method in the parent class (same method name, same parameter list-parameter type and order) methods (with the keyword "virtual" or "Override") can be implemented, but we recommend that you use the "new" keyword to explicitly hide it.
// Only the "Override" keyword can be used to overwrite (override) Methods marked as "virtual", "abstract", or "Override" in the parent class, and the method marked as override in the subclass, it must also be the method marked as "virtual", "abstract", or "Override" in the parent class.
// Override: the override keyword must be used. methods that can be overwritten include methods marked as abstract, virtual, and override;
// Hide: You can use the new keyword or the keyword. methods that can be hidden include general methods and methods marked as virtual "or" Override;
// Overload: no special keywords are required.
// Static methods can be hidden or overloaded.
/* Differences between overload, overwrite, and hide
Overload is used for member functions in the same class. Its features are as follows:
* 1) in the same class
* 2) Same Function Name
* 3) Different parameters (including different parameter types, different numbers, or different values). Note: it is irrelevant to the return value)
* 4) it has nothing to do with whether it is a virtual function.
Override refers to the function of a derived class that overrides a base class function. Its features are as follows:
* 1) different scopes (located in the derived class and the base class respectively)
* 2) Same Function Name
* 3) Same Parameters
* 4) The base function must be a virtual function.
Hidden (hide) refers to the function of a derived class that shields the base class functions with the same name. Its features are as follows:
* 1) different scopes (located in the derived class and the base class respectively)
* 2) Same Function Name
(3) If the parameters are different, the base class function does not have the virtual keyword, and the base class function will be hidden. (Because the derived class and the base class are not in the same range, they are hidden rather than overloaded );
() If the parameters are different, the base class function has the virtual keyword. The base function will be hidden. (Because the derived class and the base class are not in the same range, they are hidden rather than overloaded; because the parameters are different, they are hidden rather than overwritten );
() If the parameters are the same, the basic functions do not have the virtual keyword. The base function will be hidden. (Because the base-class functions are not virtual functions, they are hidden rather than overwritten ).
() If the parameters are the same, the base class function has the virtual keyword. If the base class function has multiple overloaded versions and the derived class does not override all virtual functions with the same name, when the function is called in the derived class, hidden virtual functions that are not overwritten in the base class. (Of course, The rewritten function is overwritten ).
Note: To Call hidden functions in the base class in a derived class, you can enter the following code in the derived class: using base: fun2
*/

}

Progress of C #100 examples

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.