Abstrakte Klasse und Schnittstelle in C#

1- Introduction

Im diesem Dokument gebe ich die Hinweise über die Interface und die Abstract Classe. Gleichzeitig analysiere ich die Gleichheit und den Unterschied von ihnen..
Zuerst brauchen Sie ein Projekt mit dem Name von AbstractClassInterface um mit den Beispiele durchzuführen.
Stellen Sie es als das Default Projekt.

2- Abstract Class

Abstract class (die abstrakte Klasseg).Sehen Sie bitte ein Beispiel über solche Klasse
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractClassInterface
{
 
   // A class has at least one abstract method, must be declared as abstract.
   public abstract class ClassA
   {
 
       // This is an abstract method.
       // It has no function body.
       // This method has access modifiers is public.
       public abstract void DoSomething();

       // This method has access modifiers is protected.
       protected abstract String DoNothing();

       
       protected abstract void Todo();
   }

   // This is an abstract class.
   // Declaring it is abstract, although it does not have any abstract methods.
   public abstract class ClassB
   {

   }
}
Die Merkmale einer abstrakten Klasse sind:
  1. Es wird mit dem Schlusselwort  abstract.angemeldet
  2. Es kann  0, 1 oder viele abstrakte Methode anmelden.
  3. Sie können ein Objekt direkt aus einer abstrakten Klasse nicht erstellen.

3- Abstract class, the examples

Sehen Sie das folgende Beispiel:
AbstractJob.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractClassInterface
{

    // Class has at least one abstract method, must be declared as abstract.
    public abstract class AbstractJob
    {

        public AbstractJob()
        {

        }
        // This is an abstract method,
        // It has no body
        // This method returns the name of the job.
        public abstract String GetJobName();

        // This is an abstract method,
        // It has no body
        public abstract void DoJob();

        // None abstract method.
        public void StopJob()
        {
            Console.WriteLine("Stop");
        }
    }
}
JavaCoding.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractClassInterface
{
    public class JavaCoding : AbstractJob
    {

        public JavaCoding()
        {
        }

        // This method implement abstract method declared in the parent class.
        // This method will have body.
        // (Requires  'override' keyword).
        public override void DoJob()
        {
            Console.WriteLine("Coding Java...");
        }

        // This method implement abstract method declared in the parent class.
        // This method will have body.
        // (Requires  'override' keyword).
        public override String GetJobName()
        {
            return "Java Coding";
        }

        public void TestExample()
        {
            Console.WriteLine("Testing Example...");
        }
    }

}
CSharpCoding.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractClassInterface
{
   class CSharpCoding : AbstractJob
   {
       public CSharpCoding()
       {
       }

       // Method này triển khai method trìu tượng khai báo tại class cha.
       // (Cần phải có từ khóa 'override').
       public override void DoJob()
       {
           Console.WriteLine("Coding CSharp...");
       }

       // Method này triển khai method trìu tượng khai báo tại class cha.
       // Method này sẽ có thân hàm đầy đủ
       // Method trả về tên của công việc.
       // (Cần phải có từ khóa 'override').
       public override String GetJobName()
       {
           return "CSharp Coding";
       }

       public void RunningExample()
       {
           Console.WriteLine("Running Example...");
       }
   }
}
ManualJob.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractClassInterface
{

    // The parent class (AbstractJob) has two abstract methods.
    // This class is just implementing one abstract method of the parent class.
    // Therefore it is mandatory to declare as abstract.
    public abstract class ManualJob : AbstractJob
    {

        public ManualJob()
        {

        }

        // This method implement abstract method declared in the parent class.
        // This method will have body.
        // (Requires  'override' keyword).
        public override String GetJobName()
        {
            return "Manual Job";
        }

    }
}
BuildHouse.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractClassInterface
{

    // This class extends from ManualJob.
    // BuildHouse undeclared abstract
    // So it needs to implement all remaining abstract methods.
    public class BuildHouse : ManualJob
    {

        public BuildHouse()
        {

        }

        // This method implement abstract method declared in the parent class.
        // This method will have body.
        // (Requires  'override' keyword).
        public override void DoJob()
        {
            Console.WriteLine("Build a House");
        }

    }

}
Demo
JobDemo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractClassInterface
{
    public class JobDemo
    {

        public static void Main(string[] args)
        {
            // Create AbstractJob object via constructor of JavaCoding.
            AbstractJob job1 = new JavaCoding();

            // Call DoJob().
            job1.DoJob();

            // GetJobName() is abstract method in AbstractJob class.
            // But it has been implemented in a certain subclass
            // So can call it.
            String jobName = job1.GetJobName();

            Console.WriteLine("Job Name 1= " + jobName);


            // Create AbstractJob object via constructor of CSharpCoding.
            AbstractJob job2 = new CSharpCoding();

            // Call DoJob()
            job2.DoJob();

            String jobName2 = job2.GetJobName();

            Console.WriteLine("Job Name 2= " + jobName2);

            // Create AbstractJob object via constructor of BuildHouse.
            AbstractJob job3 = new BuildHouse();

            job3.DoJob();

            String jobName3 = job3.GetJobName();

            Console.WriteLine("Job Name 3= " + jobName2);


            Console.ReadLine();
        }
    }
}
Das Ergebnis durchführen

4- Interface

Wir kennen, dass eine Klasse nur aus einer anderen Klasse ausgeweitert werden kann.
// Class B is subclass of A, or B is extended from A
// CSharp only allow class extends from only one other class.
public class B : A  
{
  // ....
}

// In case you do not extend from any class.
// CSharp understood that this class extends from the Object class.
public class B
{

}

// The way to declare this class is similar to above way.
public class B : Object
{

}
Aber eine Klasse kann aus vielen Interface ausgeweitert werden
// A class extends from only one other class
// but can implememts many interfaces
public class Cat : Animal, CanEat, CanDrink
{

// ....
}

Die Eigenschaften der Interface in CSharp sind

  1. Interface hat modifier  public oder internal, wenn sie internal als Default nicht schreibt.
  2. Interface kann die Field nicht definieren.
  3. Ihre Methode sind abstract und public und hat keinen Funktionskörper. Aber bei der Anmeldung der Methode dürfen Sie public oder abstract.nicht schreiben
  4. Interface hat keinen Constructor

4.1- Structure of an Interface

Eine interface im  CSharp kann modifier   public oder  internal anmelden. Und bei der Nicht-Anmeldung kann man selbst   internal als Default verstehen. Interface mit modifier public kann irgendwo benutzt werden. und die Interface mit modifier  internal kann nur die interne Assembly verwendet werden.

An assembly is the compiled output of your code, typically a DLL, but your EXE is also an assembly. It's the smallest unit of deployment for any .NET project.

The assembly typically contains .NET code in MSIL (Microsoft Intermediate language) that will be compiled to native code ("JITted" - compiled by the Just-In-Time compiler) the first time it is executed on a given machine. That compiled code will also be stored in the assembly and reused on subsequent calls.

NoAccessModifierInterface.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractClassInterface
{

    // This is an Interface undeclared  access modifiers.
    // Its default modifier is 'internal'.
    // It can only be used within an Assembly.
     interface NoAccessModifierInterface
    {

    }

}
Die Methode in Interface sind public und abstrakt. Sie hat keinen Körper aber Sie können bei der Definition der Methode  public oder  abstract nicht schreiben
CanMove.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractClassInterface
{
 
    public interface CanMove
    {

        // The methods of the Interface are public and abstract.
        // (But you are not allowed to write the public or abstract here)
        void Run();
 
        void Back();
 
        int GetVelocity();

    }

}
CanDrink.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractClassInterface
{

 
 public interface CanDrink
 {

       void Drink();

 }

}
CanEat.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractClassInterface
{

 
 public interface CanEat
 {

       void Eat();
 }

}

4.2- Class implements Interface

Wenn eine Klasse eine Interface implementiert,  müssen Sie alle Methode der Interface wieder anmelden oder durchführen.
  1. Wenn Sie eine Methode von Interface implementieren, sollen Sie die Inhalt für die Methode schreiben und die Methode als Public anmelden.
  2. Wenn Sie eine Methode von Interface nicht implementieren, sollen Sie sie in der Klasse mit dem Schlüsselswort 'public abstract' anmelden und dürfen Sie keine Inhalt der Methode schreiben
Sehen Sie das Beispiel, die Klasse Animal implementiert die Interface  CanMove.
Animal.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractClassInterface
{

    // This class extends from Object.
    // And implements CanMove interface.
    // CanMove has 3 abstract methods.
    // This class implements only one abstract method of CanMove.
    // Therefore it must be declared as abstract.
    // The remaining methods must be declared with 'public abstract'.
    public abstract class Animal : CanMove
    {

        // Implements Run() method of CanMove.
        // You have to write the contents of the method.
        // Access modifier must be public.
        public void Run()
        {
            Console.WriteLine("Animal run...");
        }

        // If you do not implements a certain method of Interface
        // you have to rewrite it as an abstract method.
        // (Always is public abstract).
        public abstract void Back();


        // If you do not implements a certain method of Interface
        // you have to rewrite it as an abstract method.
        // (Always is public abstract).
        public abstract int GetVelocity();

    }


}
Die Klasse  Cat erbt von der Klasse  Animal und gleichzeitig führt sie 2 Interface  CanDrink, CanEat.durch
Cat.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractClassInterface
{

    // Cat extends from Animal and implements 2 interface CanEat, CanDrink.
    public class Cat : Animal, CanEat, CanDrink
    {

        private String name;

        public Cat(String name)
        {
            this.name = name;
        }

        public String getName()
        {
            return this.name;
        }

        // Implements abstract method of Animal.
        // (Must specify the 'override').
        public override void Back()
        {
            Console.WriteLine(name + " cat back ...");
        }

        // Implements abstract method of Animal.
        // (Must specify the 'override').
        public override int GetVelocity()
        {
            return 110;
        }

        // Implements abstract method of CanEat.
        public void Eat()
        {
            Console.WriteLine(name + " cat eat ...");
        }

        // Implements abstract method of CanDrink.
        public void Drink()
        {
            Console.WriteLine(name + " cat drink ...");
        }

    }

}
Mouse.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractClassInterface
{
    public class Mouse : Animal, CanEat, CanDrink
    {

        // Implements abstract method of Animal
        // (Must specify the 'override').
        public override void Back()
        {
            Console.WriteLine("Mouse back ...");
        }

        // Implements abstract method of Animal
        // (Must specify the 'override').
        public override int GetVelocity()
        {
            return 85;
        }

        // Implements abstract method of CanDrink.
        public void Drink()
        {
            Console.WriteLine("Mouse drink ...");
        }

        // Implements abstract method of CanEat.
        public void Eat()
        {
            Console.WriteLine("Mouse eat ...");
        }

    }

}
AnimalDemo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractClassInterface
{

    public class AnimalDemo
    {

        public static void Main(string[] args)
        {

            // Create a CanEat object.
            // An object declared as CanEat
            // Actually it is the Cat.
            CanEat canEat1 = new Cat("Tom");

            // An object declared as CanEat
            // Actually it is the Mouse.
            CanEat canEat2 = new Mouse();

            // Polymorphism shown here.
            // CSharp know the actual type of an object
            // ==> Tom cat eat ...
            canEat1.Eat();

            // ==> Mouse eat ...
            canEat2.Eat();

            bool isCat = canEat1 is Cat;// true

            Console.WriteLine("catEat1 is Cat? " + isCat);

            // Check 'canEat2' Is the mouse or not?.
            if (canEat2 is Mouse)
            {
                // Cast
                // Ép kiểu
                Mouse mouse = (Mouse)canEat2;

                // Call Drink (Inherited from CanDrink).
                mouse.Drink();
            }

            Console.ReadLine();
        }
    }

}
Das Ergebnis von der Durchführung des BEispiel