Home Backend Development C#.Net Tutorial Detailed explanation of solutions to c# interface problems

Detailed explanation of solutions to c# interface problems

Sep 19, 2017 am 11:17 AM
.net Detailed explanation

During this period, the project used interfaces. I didn’t particularly understand interfaces at first. I just knew that the interface definition was very simple. I even felt that this interface was just superfluous (when I was developing personally). Now that I started team development, I discovered how important and convenient the interface is!

Next, let me talk about my rough opinions on the use of interfaces during this period. I hope everyone will like what I said is right. I hope everyone will forgive me and give me suggestions if I am wrong!

READY GO!

I won’t go into details about the definition of the interface. It has a very important knowledge point, that is, all those that inherit this interface class must implement the definition in the interface. Speaking of This is necessary. In team development, as long as we agree on the interface, will our code be unified? ! !

This is the first point I think is important about interfaces: it allows us to unify project regulations and facilitate team code management!

Let’s use another example to illustrate:

Company A decided to develop an animal system, which contains many animals. The company decided to realize the shouting behavior of each animal...

Speaking of this, we usually have each programmer start working hard after getting the animal class they want to implement! ! !

Programmer {……}

M programmer implements the pig class and writes a shout method void Shout(string content){……}

………………

Okay, now that everyone has completed the animals they need to complete, Lao Wang next door starts to realize the roar of the beasts! ! ! ! &¥%¥*%¥¥%¥ A meal of foul language broke out! How to write this? Call them one by one? ? ?

Let’s take a look. Programmer The M programmer also needs to deliver the content of animal cries! ! ! ! !

Now that Mr. Wang next door wants to call all the animals, he has to call the method one by one...

OK, let’s hold a meeting to discuss, and Mr. Wang next door defines an animal interface. All animal classes must inherit this interface. This interface only defines one void Shout(); (I don’t want to write too much, it’s lazy)

After X, Y, M programmers inherit, X, M immediately discovered that there was a problem, and then began to change the class in his hand

At this time, Lao Wang started to roar with all the beasts! Hahahahaha

The code will be posted next for everyone to see

Interface

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InterfaceProject
{    /// <summary>
    /// 动物接口    /// </summary>
    interface IAnimal
    {        /// <summary>
        /// 动物叫喊        /// </summary>
        void Shout();
    }
}
Copy after login

dog

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InterfaceProject
{    /// <summary>
    /// 狗    /// </summary>
    public class Dog:IAnimal
    {        public void Shout()
        {
            Console.WriteLine("汪汪汪");
        }
    }
}
Copy after login

CAT

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InterfaceProject
{    /// <summary>
    /// 猫    /// </summary>
    public class Cat:IAnimal
    {        public void Shout()
        {
            Console.WriteLine("喵喵喵");
        }
    }
}
Copy after login

PIG

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InterfaceProject
{    /// <summary>
    /// 猪    /// </summary>
    public class Pig:IAnimal
    {        public void Shout()
        {
            Console.WriteLine("猪怎么叫来着??猪叫");
        }
    }
}
Copy after login

Lao Wang next door comes to realize the roar of all beasts ( Defeat the existence of a character like Lao Wang)

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace InterfaceProject
{    class Program
    {        static void Main(string[] args)
        {            //百兽齐鸣(这里可以使用反射来初始化所有继承IAnimal的所有动物,我就不写这个了,主要看接口)
            List<IAnimal> animals = new List<IAnimal>();
            IAnimal dog = new Dog();
            animals.Add(dog);
            IAnimal cat = new Cat();
            animals.Add(cat);
            IAnimal pig = new Pig();
            animals.Add(pig);            //所有动物都叫一遍
            for (int i = 0; i < animals.Count; i++)
            {
                animals[i].Shout();
            }

            
        }
    }
}
Copy after login

That’s it for my rough insights into this interface! Although the interface is very simple to use, we still need to understand the role of this interface. I hope that this article of mine will enable more novices like me to take the first step towards the interface.

The above is the detailed content of Detailed explanation of solutions to c# interface problems. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of obtaining administrator rights in Win11 Detailed explanation of obtaining administrator rights in Win11 Mar 08, 2024 pm 03:06 PM

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of the mode function in C++ Detailed explanation of the mode function in C++ Nov 18, 2023 pm 03:08 PM

Detailed explanation of the mode function in C++ In statistics, the mode refers to the value that appears most frequently in a set of data. In C++ language, we can find the mode in any set of data by writing a mode function. The mode function can be implemented in many different ways, two of the commonly used methods will be introduced in detail below. The first method is to use a hash table to count the number of occurrences of each number. First, we need to define a hash table with each number as the key and the number of occurrences as the value. Then, for a given data set, we run

Detailed explanation of division operation in Oracle SQL Detailed explanation of division operation in Oracle SQL Mar 10, 2024 am 09:51 AM

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

Detailed explanation of remainder function in C++ Detailed explanation of remainder function in C++ Nov 18, 2023 pm 02:41 PM

Detailed explanation of the remainder function in C++ In C++, the remainder operator (%) is used to calculate the remainder of the division of two numbers. It is a binary operator whose operands can be any integer type (including char, short, int, long, etc.) or a floating-point number type (such as float, double). The remainder operator returns a result with the same sign as the dividend. For example, for the remainder operation of integers, we can use the following code to implement: inta=10;intb=3;

What are the employment prospects of C#? What are the employment prospects of C#? Oct 19, 2023 am 11:02 AM

Whether you are a beginner or an experienced professional, mastering C# will pave the way for your career.

Detailed explanation of the role and usage of PHP modulo operator Detailed explanation of the role and usage of PHP modulo operator Mar 19, 2024 pm 04:33 PM

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Detailed explanation of the linux system call system() function Detailed explanation of the linux system call system() function Feb 22, 2024 pm 08:21 PM

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

The development of artificial intelligence (AI) technologies is in full swing today, and they have shown great potential and influence in various fields. Today Dayao will share with you 4 .NET open source AI model LLM related project frameworks, hoping to provide you with some reference. https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.mdSemanticKernelSemanticKernel is an open source software development kit (SDK) designed to integrate large language models (LLM) such as OpenAI, Azure

See all articles