Home >Backend Development >C#.Net Tutorial >Little knowledge of C# (5): Abstract classes & interfaces

Little knowledge of C# (5): Abstract classes & interfaces

黄舟
黄舟Original
2017-02-07 15:18:221290browse

Abstract class abstract:

Abstract classes and abstract methods can be identified with the abstract keyword. There is no fully defined class or method. Therefore, the instantiation operation cannot be directly performed.

Because it is not fully defined, it cannot be sealed with the sealed keyword.

Abstract methods do not contain the program body:

 public abstract class Student
    {
        //抽象方法,不含程序体
public abstract void GetStudentID();
//子类可访问字段
prodected int i;
//定义i的属性
public int I
{
    get
    {
        return i;
    }
}
}

Abstract methods that implement abstract classes in inherited classes

 public class ah:Student
    {
        public ah(int a)
        {
            this.i=a;
        }
        Public override void GetStudentID()
        {
            Console.writeline(i.ToString());
        }
    }

Interface interface:

Unified planned interface. Used to define specifications that need to be followed in subclasses (such as method identification).

The same abstract class abstract cannot be directly instantiated.

The interface can define the identification of methods, properties or indexers.

All members in the interface have the default attributes of public and abstract. All methods in the interface must be implemented in subclasses.

A class can ":" inherit multiple interfaces, and one interface can inherit multiple interfaces.

   public interface first
     {
     //索引器
     string this[int i]
    {
         get;
         set;
    }
    //方法
    int fun(int t);
    //属性
    string j
    {
        get;
        set;
    }
    }

The above is the little knowledge of C# (5): the content of abstract classes & interfaces. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn