Home > Backend Development > C#.Net Tutorial > Detailed introduction to the method of instantiating interface objects in C#

Detailed introduction to the method of instantiating interface objects in C#

黄舟
Release: 2017-03-21 11:32:53
Original
2258 people have browsed it

The following editor will bring you an articleC# How to instantiate interface objects. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.

Many, many interface objects are used in the head first design pattern

First of all, let’s clarify an issue, that is, interfaces can not only declare objects, but also instantiate objects. , can also be passed in as a parameter.

1. Interface callback

This is the upward transformation in inheritance. Parent class FL=new subclass (); but the parent class here is the interface interface. (Personally, I think the usage is the same whether it is override of class or rewrite of interface)

You can assign the reference of the object created by the class that implements a certain interface to the interface variable declared by the interface, then the Interface variables can call methods in the interface implemented by the class. In fact, when the interface variable calls a method in the interface implemented by the class, the corresponding object is notified to call the interface method

Directly go to the code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
  interface Itemp
  {
    double plus();
  }
  public class num : Itemp
  {
    double aa, bb;
    public num(double a, double b)
    {
      this.bb = b;
      this.aa = a;
    }
    public double plus()
    {
      return (aa * bb);
    }
  }
  class Program
  {
    static void Main(string[] args)
    {
      num n = null;//声明类对象引用
      Itemp tm = null;//声明接口对象引用
      tm = new num(1.1, 2.2);//接口回调(向上转型)
      Console.WriteLine(tm.plus());
      Console.ReadKey();
    }
  }
}
Copy after login

Through the above example, it is not difficult to see that the instantiation of an interface object is actually an interface object as a reference, pointing to all methods in the class that implements its method. This is very similar to ## in C++ #Function pointer (similar to delegation in C#), but there is a difference. Interface object instantiation in C# is actually one-to-many, while function pointers in C++ are one-to-one.

But it should be noted that the instantiation of the interface object must be instantiated with the class that implements it, and cannot be instantiated with the interface itself. Using an interface itself to instantiate its own objects is not allowed in C#.

The above is the detailed content of Detailed introduction to the method of instantiating interface objects in C#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template