The specific usage of new, override, virtual in C #

Source: Internet
Author: User

The specific usage of new, override, virtual in C #

Class Program
{
static void Main (string[] args)
{
Testshape ();
Console.WriteLine ("Testshape End =============" + Environment.NewLine);
Testderive ();
Console.WriteLine ("Testderive End =============" + Environment.NewLine);
Testderive2 ();
Console.WriteLine ("Testderive2 End =============" + Environment.NewLine);
Console.readkey ();
}

private static void Testshape ()
{
system.collections.generic.list<shape> shapes = new system.collections.generic.list<shape> ();
Shapes.add (New Circle ());
Shapes.add (new Rectangle ());
Shapes.add (new triangle ());
Shapes.add (New diamond ());
foreach (shape s in shapes)
{
S.methodvirtual ();
S.method ();
Console.WriteLine ();
}

}

private static void Testderive ()
{
Circle circle = New Circle ();
Rectangle rectangle = new Rectangle ();
Triangle Triangel = new triangle ();
Diamond Diamond = new diamond ();
Circle.methodvirtual ();
Circle.method ();
Console.WriteLine ();
Rectangle.methodvirtual ();
Rectangle.method ();
Console.WriteLine ();
Triangel.methodvirtual ();
Triangel.method ();
Console.WriteLine ();
Diamond.methodvirtual ();
Diamond.method ();
Console.WriteLine ();
}

private static void Testderive2 ()
{
Circle circle = New Circle ();
Printshape (circle);
Rectangle rectangle = new Rectangle ();
Printshape (Rectangle);
Triangle Triangel = new triangle ();
Printshape (Triangel);
Diamond Diamond = new diamond ();
Printshape (Diamond);
Out put:
Circle Override Methodvirtual
Base method call

Base Methodvirtual Call
Base method call

Base Methodvirtual Call
Base method call

Base Methodvirtual Call
Base method call
}

static void Printshape (Shape Sharpe)
{
Sharpe.methodvirtual ();
Sharpe.method ();
Console.WriteLine ();
}
}

public class shape
{
public virtual void methodvirtual ()
{
Console.WriteLine ("Base methodvirtual call");
}

public void Method ()
{
Console.WriteLine ("Base method call");
}
}

Class Description: Override the virtual method of the base class
///
The first way to use: transition to a parent class
Sharp s = New Circle ()
S.methodvirtual ();
S.method ();
Because subclasses have already override the methodvirtual of the parent class, the method of calling or subclasses is called even if the subclass transitions to sharp
Out put:
Circle Override Methodvirtual
Base method call
///
Second class usage: using the subclass itself
This is very well understood, the whole output is a subclass of the method
Circle circle = New Circle ();
Circle.methodvirtual ();
Circle.method ();
Out put:
Circle Override Methodvirtual
Base method call
Class Circle:shape
{
public override void Methodvirtual ()
{
Console.WriteLine ("Circle Override Methodvirtual");
}
}

Class Description: No processing
///
The first method of use
Sharp s = new Rectangle ()
S.methodvirtual ();
S.method ();
Out put:
Base Methodvirtual Call
Base method call
///
Second class usage: using the subclass itself
This is very well understood, the whole output is a subclass of the method
Rectangle rectangle = new Rectangle ();
Rectangle.methodvirtual ();
Rectangle.method ();
Out put:
Base Methodvirtual Call
Base method call
Class Rectangle:shape
{

}

Class Description: New virtual method of base class that is not virtual method
///
The first method of use
Sharp s = new triangle ()
S.methodvirtual ();
S.method ();
Because subclasses have new methods of the parent class, so s outputs the method of the parent class
Out put:
Base Methodvirtual Call
Base method call
///
Second class usage: using the subclass itself
This is very well understood, the whole output is a subclass of the method
Triangle Triangel = new triangle ();
Triangel.methodvirtual ();
Triangel.method ();
Out put:
Triangle New methodvirtual
Triangle New Method
Class Triangle:shape
{
Public new void Methodvirtual ()
{
Console.WriteLine ("Triangle New methodvirtual");
}

Public new void Method ()
{
Console.WriteLine ("Triangle New Method");
}
}

Class Description: The same method was created for the base class method, not new and override
The compiler prompts "Hide inheritance" and performs the action as if there is a new keyword
///
The first method of use
Sharp s = new diamond ()
S.methodvirtual ();
S.method ();
The output is the same as the explicit new modifier because of the effect of the default new
Out put:
Base Methodvirtual Call
Base method call
///
Second class usage: using the subclass itself
This is very well understood, the whole output is a subclass of the method
Diamond Diamond = new diamond ();
Diamond.methodvirtual ();
Diamond.method ();
Out put:
Diamond Default Methodvirtual
Diamond Default method
Class Diamond:shape
{
public void methodvirtual ()
{
Console.WriteLine ("Diamond Default methodvirtual");
}

public void Method ()
{
Console.WriteLine ("Diamond Default Method");
}
}

The result of the code above is the same effect if the base class uses an interface instead.

When you look at override or new methods of the base class, the output of the caller after transforming the subclass object into the parent class is

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.