如何在界面中使用抽象? [英] How Can I Use Abstract In The Interface?

查看:142
本文介绍了如何在界面中使用抽象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下:



The code is as follows:

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

namespace InplicitWithAbstract
{
    class Program
    {
        static void Main(string[] args)
        {
            B b = new B();
            int r2=b.m2(8,2);
            Console.WriteLine(r2);
            A a = b; //UPCASTING
            int r1=a.m1(4,7);
            Console.WriteLine(r1);

            Console.ReadLine();
        }
    }

        public abstract interface I1
        {
            int m1(int x, int y);
            abstract int m2(int a, int b);
        }


    public abstract class A : I1
    {
        public int m1(int x, int y)
        {
            return x + y;
        }
    }
    public class B : A
    {
        public override int m2(int a, int b)
        {
            return a - b;
        }
    }
}

推荐答案

你不能。即使它被C#语法所允许,也没有任何意义。如果你愿意的话,所有接口都是按定义抽象。



对于一些想法,请参阅我在过去的答案和这两个引用的答案:

为什么我们需要为接口创建实例而不是实现吗? [ ^ ],

对接口的怀疑 [ ^ ]。



完全有很多答案,对不起,有些要点会重复,但是你最好看看这些答案,它们可以真正帮助你理解事物。



-SA
You cannot. Even if it was allowed by C# syntax, it would make no sense at all. If you will, all interfaces are "abstract by definitions".

For some ideas, please see my past answers and answers referenced in these two:
Why we need to create instance for interface not for implementation?[^],
Doubts on Interfaces[^].

It's many answers altogether, and, sorry, some points are repeated, but you better look through these answers, they can really help you to understand things.

—SA


抽象接口没有任何意义。抽象接口没有属性,非抽象接口不会使抽象关键字冗余,它只是一个接口。
An abstract interface would make no sense. There are no properties an abstract interface would have that a non-abstract interface wouldn't so the abstract keyword is redundant, it is just an interface.


这篇关于如何在界面中使用抽象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆