C # abstract method and abstract instance--c# Foundation

Source: Internet
Author: User

1. Abstract methods and declarations of abstract classes1) Abstract class and abstract method declarations must contain abstract2) The declaration of the abstract method has no method body: Public abstract void Fly ();3) Abstract classes and abstract methods before adding public, the default will be private. 4) The declaration of an abstract class is for derivation and inheritance, and is marked as sealed and cannot be inherited. 5) Abstract classes cannot be instantiated, that is, they cannot be used with new, sealed, and must be implemented by the derived class by inheriting its abstract methods6) Abstract classes can contain non-abstract methods7) Original notes:Program.cs:Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Abstract classes and abstractions
public abstract Fky () {}//default is private
public abstract void fly (); Abstract method declaration No method body
Multiple declarations of abstract classes and abstract methods must contain abstract
The declaration of an abstract class is for derivation and inheritance, and if marked as sealed, you cannot inherit the
Abstract classes cannot be instantiated, they must be implemented by derived classes through inheritance, and therefore cannot be used with new, sealed
Abstract classes can contain non-abstract methods
namespace abstract classes and abstract methods
{
Class Program
{
static void Main (string[] args)
{ }
}
}2. Abstract class and abstract instancePow.cs:Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.threading.tasks;namespace abstract class and abstract method instances
{
Public abstract class Pow
{
public abstract void Powmehod (int x,int y);
}
}pow1.cs:using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.threading.tasks;namespace abstract class and abstract method instances
{
Class Pow1:pow
{
public override void Powmehod (int x,int y)
{
int POW = 1;
for (int i = 1; I <= y; i++)
{
POW *= x;
}
Console.WriteLine ("The result of exponentiation is:" +pow);
}
}
}Pow2.cs:Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.threading.tasks;namespace abstract class and abstract method instances
{
Class Pow2:pow
{
public override void Powmehod (int x, int y)
{Console.WriteLine ("The result of exponentiation:" +system.math.pow (x, y));
}
}
}Program.cs:Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.threading.tasks;namespace abstract class and abstract method instances
{
Class Program
{
static void Main (string[] args)
{
Pow pow = new pow (); abstract class not instantiated
Pow1 Pow1 = new Pow1 ();
Pow1. Powmehod (2,10);
Pow2 pow2 = new Pow2 ();
Pow2. Powmehod (2,10);
Console.readkey ();
Abstract method: No implementation of its own.
Virtual method
Common: All can be overrid to the original method of rewriting
}
}
}

C # abstract method and abstract instance--c# Foundation

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.