相关疑难解决方法(0)

在接口中受保护

为什么interface定义中的所有方法都是隐含的public?为什么它不允许protected方法?

java interface

101
推荐指数
8
解决办法
4万
查看次数

C#接口的非公共成员

在C#中,当您实现接口时,所有成员都是隐式公共的.那岂不是更好,如果我们可以指定可访问修饰符(protected,internal,除了private当然的),或者我们应该使用抽象类呢?

.net c# interface

30
推荐指数
4
解决办法
3万
查看次数

为什么界面成员没有访问修饰符?

可能重复:
为什么我不能拥有受保护的接口成员?

作为标题,在C#中.是否有人可能想要受保护或内部接口?

c# interface

22
推荐指数
2
解决办法
2万
查看次数

C#中的接口

我是OOP的新手并且有一些问题.

  1. 为什么接口中声明的方法不能具有修饰符(public,private等).

  2. 在这段代码中:

class Program
{
    static void Main(string[] args)
    {
        X ob = new Y();
        ob.add(4, 5);
        Z ob1 = new Y();
        ob1.mull(2, 3);
        Console.Read();
    }
}

public interface X
{
    void add(int x, int y);
}
public interface Z
{
    void mull(int x, int y);
}

class Y : X, Z
{
    void X.add(int x, int y)//here we are not decalring it as public ,why?
    {
        Console.WriteLine("sum of X and y is " + (x + y));
    } …
Run Code Online (Sandbox Code Playgroud)

c# oop interface

8
推荐指数
2
解决办法
788
查看次数

隐藏IL中接口的公共成员

考虑以下代码行:

ConcurrentDictionary<string, object> error = new ConcurrentDictionary<string, object>();
error.Add("hello", "world"); //actually throws a compiler error
Run Code Online (Sandbox Code Playgroud)

IDictionary<string, object> noError = new ConcurrentDictionary<string, object>();
noError.Add("hello", "world");
Run Code Online (Sandbox Code Playgroud)

我最终发现你所要做的就是改变IL以使Add函数变为私有.

现在本着解耦代码的精神,我很可能会使用接口,但似乎并没有找到Concurrent字典的Add方法.

真正使用它是否安全Add(我无法查看IL,因此我不知道它是否真的是线程安全的.)?或者我应该使用具体类型ConcurrentDictionary<TKey, TValue>并明确使用TryAdd.

c# il

4
推荐指数
2
解决办法
127
查看次数

基类实现接口

  1. 基类实现接口的缺点/风险是什么?
  2. 总是在子类上实现接口更好吗?
  3. 你什么时候使用其中一个?

    public interface IFriendly
    {
        string GetFriendly();
    }
    
    
    public abstract class Person: IFriendly
    {
        public abstract string GetFriendly(); 
    }
    
    Run Code Online (Sandbox Code Playgroud)

    VS.

    public interface IFriendly
    {
        string GetFriendly();
    }
    
    public abstract class Person
    {
       // some other stuff i would like subclasses to have
    }
    
    public abstract class Employee : Person, IFriendly
    {
        public string GetFriendly()
        {
            return "friendly";
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

.net c# oop

4
推荐指数
1
解决办法
4719
查看次数

为什么在定义接口的方法和属性前面没有修饰符(public,private,protected)?

可能重复:您
是否有理由无法在方法或界面中定义访问修饰符?

你好,

我对接口感到好奇.假设我有以下界面的定义

public interface IPersone
{
  string FirstName { get; set; }
  string LastName { get; set; }
  int CalculateAge(int YearOfBirth);
}
Run Code Online (Sandbox Code Playgroud)

为什么在定义接口的方法和属性前面没有修饰符(public,private,protected)?有什么理由吗?

谢谢你的帮助

c#

3
推荐指数
1
解决办法
1505
查看次数

C# 接口保护方法 - 它是如何工作的?

我无法意识到接口的受保护方法是如何工作的。我有带有受保护方法的接口和类:Platform - .Net Core 5

public interface ISomeInterface
{
    protected void Method_InterfaceRealization()
    {
        Console.WriteLine("JUST Inside interface realization PROTECTED");
    }

    protected void Method1();
}

public class SomeClass: ISomeInterface
{
    void ISomeInterface.Method1()
    {
        Console.WriteLine("Method_PROTECTED_NoInterfaceRealization");
    }
}
Run Code Online (Sandbox Code Playgroud)
  1. 如何在接口之外调用 Method_InterfaceRealization 方法?
  2. 我怎样才能在任何地方调用这个Method1?多谢!

c#

1
推荐指数
1
解决办法
1364
查看次数

标签 统计

c# ×7

interface ×4

.net ×2

oop ×2

il ×1

java ×1