面向对象关于接口、抽象类、具体类的质疑

发布于 2024-11-18 09:28:15 字数 1403 浏览 4 评论 0原文

1) OnCreate 是 ClsLast 类实例化对象的公共方法。 但我想将 OnCreate 方法限制为受保护

interface InterFace {
    void OnCreate();
}

class ClsFirst implements InterFace {
    @Override
    public void OnCreate() { }

}

class ClsLast extends ClsFirst{ }

class Test {
    static void main(){
        ClsLast objClsAct = new ClsLast();
        objClsAct.OnCreate();
    }
}

2) 如果我在 InterFace 中将 OnCreate 方法设置为受保护,

interface InterFace {
    protected void OnCreate();
}   

则会收到如下错误: 接口方法OnCreate的修饰符非法;仅公开&允许抽象

3) 如果我在实现 InterFace 的 ClsFirst 类中设置 protected 方法,如下所示:

interface InterFace {
    void OnCreate();
}   

class ClsFirst implements InterFace {
    @Override
    protected void OnCreate() { }

}

我收到此错误: 无法降低从 InterFace 继承的方法的可见性

4) 当我将 ClsFirst 更改为抽象类并实现 InterFace 时,

interface InterFace {
    void OnCreate();
}   

abstract class ClsFirst implements InterFace {

}

我没有在 ClsFirst 类中实现 OnCreate 方法,但是Cls最后为什么?

总结

  1. 如何设置接口方法只能在派生类中使用?
  2. 为什么我无法在接口内设置具有受保护访问权限的方法?
  3. 为什么我实现InterFace后不能将Class的访问器类型设置为与public不同的类型?
  4. 抽象类,即使它们实现了接口,也不必添加未实现的方法本身,直到一个类派生抽象类。为什么?

非常感谢您从现在开始的友好答复。

1) OnCreate is public method of instantiated object from ClsLast class. But I wanted to restrict OnCreate method as protected.

interface InterFace {
    void OnCreate();
}

class ClsFirst implements InterFace {
    @Override
    public void OnCreate() { }

}

class ClsLast extends ClsFirst{ }

class Test {
    static void main(){
        ClsLast objClsAct = new ClsLast();
        objClsAct.OnCreate();
    }
}

2) If I set OnCreate method as protected in InterFace

interface InterFace {
    protected void OnCreate();
}   

I'm getting error like this:
Illegal modifier for the interface method OnCreate; only public & abstract are permitted

3) If I set protected the method inside the ClsFirst class which implements InterFace like this:

interface InterFace {
    void OnCreate();
}   

class ClsFirst implements InterFace {
    @Override
    protected void OnCreate() { }

}

I'm getting this error:
Cannot reduce the visibility of the inherited method from InterFace

4) When I change the ClsFirst as abstract class and implements InterFace

interface InterFace {
    void OnCreate();
}   

abstract class ClsFirst implements InterFace {

}

I haven't to implement OnCreate method inside the ClsFirst class but ClsLast why?

Summary

  1. How can I set Interface methods can only be used in derived classes?
  2. Why can't I set methods with protected access inside Interface?
  3. Why can't I set the accessor type of Class different than public after I implement the InterFace?
  4. abstract classes that even if they implements an interface don't have to add unimplemented methods itself until one class derives abstracted classes. Why?

Thank you so much for your kind answers from now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

望笑 2024-11-25 09:28:16

根据定义,接口是一个公共契约。通过实现一个接口,您承诺提供一组方法(以及其他语言的属性)。

接口与抽象类没有任何共同点,因为您可以在单个类上实现任意数量的接口,并且您可以实现而不是派生从它。

抽象类是一个基类,它就像一个部分功能的基类,用于在不同类之间共享实现细节并提供契约。然而,它更多的是所有派生类的内部契约。大多数时候,抽象类用于强制所有派生类采用通用模式,而不是共享公共契约。

抽象类可以强制派生类实现方法(或使用virtual关键字提供合适的默认实现)并使用此方法(这称为模板模式)。

如果在抽象类上实现接口,则不必提供所有接口方法,因为抽象类无法实例化。但是,如果您没有实现所有接口方法,则层次结构中的第一个非抽象类必须提供缺少的方法。

An interfaceis per definition a public contract. By implementing an interface you promise to provide a set of methods (and properties in other languages).

An interface has nothing in common with abstract classes because you can implement as many interfaces as you want on a single class and you implement instead of derive from it.

An abstract class is a base class, its like a partially functionally base which is used to share implementation details between different classes and provides a contract, too. However it's more an internal contract for all derived classes. Most times abstract classes are used to force all derived classes into a common pattern instead of sharing a public contract.

An abstract class can force a derived class to implement a method (or provide a suitable default implementation using the virtual keyword) and take use of this method (this is called Template Pattern).

If you implement an interface on an abstract class you don't have to provide all interface methods because the abstract class can not be instantiated. However if you don't implement all interface methods the first non abstract class in your hierarchy has to provide the missing methods.

夜唯美灬不弃 2024-11-25 09:28:16

接口方法始终是公共的。您无法缩小这些方法的范围。在抽象类中使用受保护的抽象方法。

Interface methods are always public. You can not reduce the scope of these methods. Use a protected abstract method instead in an abstract class.

○愚か者の日 2024-11-25 09:28:16
  1. 你做不到。如果你有一个只实现接口契约中定义的一部分的中间类,你可以将方法设置为虚拟或抽象。

  2. 你也不能这样做,原因很简单,接口定义了一个契约,一组要求实现必须具有的操作。您可以在抽象类中显式实现该接口,或者将整个接口限制为“内部”,至少在 C# 中,以防万一您想将其保留在“内部”。

  3. 你不能这样做,因为你承诺实现一个接口。

  4. 因为一旦将类标记为抽象,您间接指出的另一件事是您无法实例化它。因此,您将实现接口的要求传递给您可以创建实例的第一个类(我的意思是继承链)。

广告1。 (c# 示例1)

interface IDummyInterface
{
    void OnCreate();
    void Process();
    void OnFinish();
}

abstract class DummyAbstract : IDummyInterface
{
    public virtual void OnCreate()
    {
    }

    public abstract void Process();

    public void OnFinish()
    {
        OnFinishInternal();
    }

    protected abstract void OnFinishInternal();
}

class DummyImplementor : DummyAbstract
{
    public override void OnCreate()
    {
        // some other action here
        base.OnCreate();
    }

    public override void Process()
    {
    }

    protected override void OnFinishInternal()
    {
    }
}
  1. You cannot do it. You can set methods both as virtual or abstract if you have an middle class which implements only a part of defined in interface contract.

  2. You also cannot do it and the reason is simple, interface defines a contract, a set of operations which implementation is requested to have. You can implement the interface explicitly in your abstract class or restrict whole interface to be "internal", at least in c# in case if you would like to keep it 'in-house'.

  3. You cannot do it because you promised to implement an interface.

  4. Because another thing which you indirectly pointed once you mark class as an abstract is that you cannot instantiate it. Therefore you pass the requirement of implementing interface to the first class which you can create the instance of (I mean the chain of inheritance).

ad1. (c# example1)

interface IDummyInterface
{
    void OnCreate();
    void Process();
    void OnFinish();
}

abstract class DummyAbstract : IDummyInterface
{
    public virtual void OnCreate()
    {
    }

    public abstract void Process();

    public void OnFinish()
    {
        OnFinishInternal();
    }

    protected abstract void OnFinishInternal();
}

class DummyImplementor : DummyAbstract
{
    public override void OnCreate()
    {
        // some other action here
        base.OnCreate();
    }

    public override void Process()
    {
    }

    protected override void OnFinishInternal()
    {
    }
}
沦落红尘 2024-11-25 09:28:16

实际上,您可以隐藏该方法,直到通过显式实现接口将您的具体类强制转换为“接口”实例:

abstract class ClsFirst : Interface
{
     void Interface.OnCreate()
     {
          this.OnCreate();
     }

     protected abstract void OnCreate();
}

ClsFirst last = new ClsLast();
last.OnCreate(); // Compiler Error, since OnCreate() is protected
Interface lastAsInterface = last;
lastAsInterface.OnCreate(); // Works, and calls the abstract OnCreate() method. 

Actually you can hide the method, until the your concrete class is casted to an "Interface" instance by implementing the Interface explicitly:

abstract class ClsFirst : Interface
{
     void Interface.OnCreate()
     {
          this.OnCreate();
     }

     protected abstract void OnCreate();
}

ClsFirst last = new ClsLast();
last.OnCreate(); // Compiler Error, since OnCreate() is protected
Interface lastAsInterface = last;
lastAsInterface.OnCreate(); // Works, and calls the abstract OnCreate() method. 
我的影子我的梦 2024-11-25 09:28:16

1) 为什么要首先实现一个受保护的接口?它不会增加任何价值。您已经有办法确保所有派生类都遵守给定的契约:

 protected abstract void OnCreate();

2) 接口定义一个 public 契约。通过public我的意思是它具有与界面本身相同的可访问性。这就是为什么在定义接口成员时不允许使用辅助功能修饰符。

3) 您不能降低任何虚拟成员或界面成员的可访问性,否则会违反界面合同。在您的示例中 ((IOnCreate)ClsFirst).OnCreate() 是不可能的,因为 OnCreate() 不可访问,这意味着 ClsFirst 没有实现IOnCreate接口。

1) Why would you want to implement a protected interface to begin with? It does not add any value. You already have a way to make sure all derived classes comply with a given contract:

 protected abstract void OnCreate();

2) Interfaces define a public contract. By public I mean that it has the same accesibility as the interface itselft. That is why accesibility modifiers are not permitted when defining interface members.

3) You can not decrease accesibility of any virtual member or interface members as you would be breaking the interface's contract. In your example ((IOnCreate)ClsFirst).OnCreate() would not be possible as OnCreate() is not accesible, which would mean that ClsFirst does not implement IOnCreate interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文