[You must know. NET] Second: To abstract programming: interfaces and abstract classes

Source: Internet
Author: User

Related articles:

[You must know. NET] The third time: Historical entanglements: attributes and attributes

[You must know. NET] The fourth time: from behind: Class and struct

1. Introduction

In my previous post, "Who is not the abstract class and interface", and colleagues Gao Wei discussion, get a lot of friends attention, because it is not the system of the way, so to understand the inconvenience, at the same time on the subject of the system theory, I think it is necessary to do to sum up, so have a fresh out of this article. At the same time, I will put the problem in the same way to explain.

2. Introduction of Concepts

What is an interface?

An interface is an abstract type that contains a set of virtual methods, each of which has its name, parameters, and return values. An interface method cannot contain any implementations, and the CLR allows an interface to contain events, properties, indexers, static methods, static fields, static constructors, and constants. Note, however, that C # cannot contain any static members. A class can implement multiple interfaces, and when a class inherits an interface, it implements not only all the methods that the interface defines, but all the methods that the interface inherits from other interfaces.

The method is defined as:

public interface System.IComparable
{
  int CompareTo(object o);
}
public class TestCls: IComparable
{
  public TestCls()
  {
  }
  private int _value;
  public int Value
  {
   get { return _value; }
   set { _value = value; }
  }
  public int CompareTo(object o)
  {
   //使用as模式进行转型判断
   TestCls aCls = o as TestCls;
   if (aCls != null)
   {
     //实现抽象方法
     return _value.CompareTo(aCls._value);
   }
  }
}

What is an abstract class?

An abstract class provides a common definition of the base class shared by multiple derived classes, and it can provide either abstract methods or Non-abstract methods. An abstract class cannot be instantiated and must implement its abstract methods by inheriting the derived class, so you cannot use the New keyword or be sealed on an abstract class. If the derived class does not implement all of the abstract methods, the derived class must also be declared as an abstract class. In addition, the implementation of abstract methods is implemented by the overriding method.

The method is defined as:

///
/// 定义抽象类
///
abstract public class Animal
{
  //定义静态字段
  static protected int _id;
  //定义属性
  public abstract static int Id
  {
   get;
   set;
  }
  //定义方法
  public abstract void Eat();
   //定义索引器
  public string this[int i]
  {
   get;
   set;
  }
  ///
  /// 实现抽象类
  ///
  public class Dog: Animal
  {
   public static override int Id
   {
     get {return _id;}
     set {_id = value;}
   }
   public override void Eat()
   {
     Console.Write("Dog Eats.")
   }
  }

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.