是否可以在派生类或任何其他类中调用抽象类的方法

发布于 2024-11-24 14:53:53 字数 380 浏览 0 评论 0原文

是否可以在派生类或任何其他类中调用抽象类的方法。我的代码如下,我想在Program的Main方法中调用Abstr的Describe()方法。 是否可以?如果答案是肯定的怎么办?

class Program
{
    public void Main()
    {
        //I want to call the Describe() method here, how do i do that
        Console.ReadLine();
    }
}

public abstract class Abstr
{
    public void Describe()
    {
        //do something
    }
}

Is it possible to call method of an Abstract class in derived class or any other class. My code is below, I want to call Abstr's Describe() method in Program's Main method.
Is it possible? if answer is yes how?

class Program
{
    public void Main()
    {
        //I want to call the Describe() method here, how do i do that
        Console.ReadLine();
    }
}

public abstract class Abstr
{
    public void Describe()
    {
        //do something
    }
}

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

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

发布评论

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

评论(6

纵山崖 2024-12-01 14:53:53

由于您的方法不是静态的,因此您需要从该抽象类初始化一个变量并从中调用该方法。为此,您可以通过 concreate 类继承抽象类,然后调用该方法。请注意,抽象类无法初始化,抛出像 Abstr Abstr = new Abstr(); 这样的构造函数是无效的。所以:

public abstract class Abstr
{
    public void Describe()
    {
        //do something
    }
}

public class Concrete : Abstr
{
   /*Some other methods and properties..*/ 
}

class Program
{
    public void Main()
    {
        Abstr abstr = new Concrete();
        abstr.Describe();
        Console.ReadLine();
    }
}

Since your method is not static, you need to initialize a variable from that abstract class and call that method from it. To do that you may inherit the abstract class by concreate class and then call that method. Note that the abstract class can't be initialized throw a constructor like Abstr abstr = new Abstr(); is not valid. So:

public abstract class Abstr
{
    public void Describe()
    {
        //do something
    }
}

public class Concrete : Abstr
{
   /*Some other methods and properties..*/ 
}

class Program
{
    public void Main()
    {
        Abstr abstr = new Concrete();
        abstr.Describe();
        Console.ReadLine();
    }
}
情释 2024-12-01 14:53:53

您应该能够直接使用 Abstr.Describe()。它是一个静态方法,因此抽象类并不重要。

编辑

现在问题中的代码已被编辑,并且方法上的 static 关键字已被删除,该答案不再适用。

You should be able to use Abstr.Describe() directly. It is a static method, so the class being abstract shouldn't matter.

Edit

Now that the code in the question has been edited and the static keyword is removed on the methods, this answer does no longer apply.

初见你 2024-12-01 14:53:53

问:是否可以在派生类中调用抽象类的方法
类?

答:当然可以,只要它是公共的、受保护的(和同一个类或子类)或内部的(和同一个程序集)

问:还有其他课程吗?

答:当然,只要它是公共的或内部的(以及相同的程序集)

良好的链接:
http://agsmith.wordpress.com/2007/12/15/内部和保护/

Q: is it possible to call method of an Abstract class in derived
class?

A: Sure, as long as it's public, protected (and the same class or a subclass), or internal (and the same assembly)

Q: Any other class?

A: Sure, as long as it's public or internal (and the same assembly)

Good link:
http://agsmith.wordpress.com/2007/12/15/internal-and-protected/

迷你仙 2024-12-01 14:53:53

是的,可以使用代码:此处

这也适用于非静态方法

yes it is possible code : here

This works for non static method as well

白衬杉格子梦 2024-12-01 14:53:53

致电编辑就足够了

原始帖子已更改,因此该答案无效。

 Abstr.Describe();

问候。

It's enough to call

EDIT:

Original post was changed, so this answer is not valid.

 Abstr.Describe();

Regards.

躲猫猫 2024-12-01 14:53:53

是的,只要子类型(以及想要调用该方法的代码)可以访问超类型中的方法定义。

这是一些示例代码。是否调用子类型或超类型中的方法定义取决于重写的定义方式:

public abstract class AbstractSupertype
{
  public void Alpha()
  {
    Console.WriteLine( "AbstractSupertype.Alpha()" ) ;
    return ;
  }
  public abstract void Bravo() ;
  public virtual  void Charlie()
  {
    Console.WriteLine( "AbstractSupertype.Charlie()" ) ;
    return ;
  }
}

public class ConcreteSubtype : AbstractSupertype
{
  public new void Alpha()
  {
    Console.WriteLine( "ConcreteSubtype.Alpha()" ) ;
  }
  public override void Bravo()
  {
    Console.WriteLine( "ConcreteSubtype.Bravo()" ) ;
  }
  public override void Charlie()
  {
    Console.WriteLine( "ConcreteSubtype.Charlie()" ) ;
  }
}
class Program
{
  static void Main( string[] args )
  {
    ConcreteSubtype   subTypeInstanceReference   = new ConcreteSubtype() ;
    AbstractSupertype superTypeInstanceReference = subTypeInstanceReference ;

    subTypeInstanceReference.Alpha()     ; // invokes subtype's method
    superTypeInstanceReference.Alpha()   ; // invokes supertype's method

    subTypeInstanceReference.Bravo()     ; // invokes subtype's method
    superTypeInstanceReference.Bravo()   ; // invokes subtype's method

    subTypeInstanceReference.Charlie()   ; // invokes subtype's method
    superTypeInstanceReference.Charlie() ; // invokes subtype's method

    return ;
  }
}

Yes, so long as the method definition in the supertype is accessable to the subtype (and to the code that wants to invoke the method).

Here's some sample code. Whether the method definition in the subtype or supertype is invoked is dependent upon how the override is defined:

public abstract class AbstractSupertype
{
  public void Alpha()
  {
    Console.WriteLine( "AbstractSupertype.Alpha()" ) ;
    return ;
  }
  public abstract void Bravo() ;
  public virtual  void Charlie()
  {
    Console.WriteLine( "AbstractSupertype.Charlie()" ) ;
    return ;
  }
}

public class ConcreteSubtype : AbstractSupertype
{
  public new void Alpha()
  {
    Console.WriteLine( "ConcreteSubtype.Alpha()" ) ;
  }
  public override void Bravo()
  {
    Console.WriteLine( "ConcreteSubtype.Bravo()" ) ;
  }
  public override void Charlie()
  {
    Console.WriteLine( "ConcreteSubtype.Charlie()" ) ;
  }
}
class Program
{
  static void Main( string[] args )
  {
    ConcreteSubtype   subTypeInstanceReference   = new ConcreteSubtype() ;
    AbstractSupertype superTypeInstanceReference = subTypeInstanceReference ;

    subTypeInstanceReference.Alpha()     ; // invokes subtype's method
    superTypeInstanceReference.Alpha()   ; // invokes supertype's method

    subTypeInstanceReference.Bravo()     ; // invokes subtype's method
    superTypeInstanceReference.Bravo()   ; // invokes subtype's method

    subTypeInstanceReference.Charlie()   ; // invokes subtype's method
    superTypeInstanceReference.Charlie() ; // invokes subtype's method

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