本文介绍了抽象类的非静态方法和静态方法有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在Abstract类中使用静态方法不是最佳实践,但是如果在抽象类中同时使用静态方法和非静态方法,会有什么区别。





除了关键字静态之外,它们之间是否还有其他区别?



例如:
抽象类:

 抽象公共类AbstractClass 
{
#region构造函数
受保护的AbstractClass (args args):this(args)
{
}
#endregion

#region公共静态方法

公共静态对象FormLoad (args args)
{
//逻辑
}

公共静态对象Sample(Args args)
{
//逻辑
}

#endregion


#region公共非静态方法

public AbstractClass CreateResponse(Args args)
{
//逻辑
}

public void ClearDialog()
{
//逻辑
}

#endregion

#region抽象方法
abstract protected void Responses();
#endregion

}

派生类:

  public class DerivedClass:AbstractClass 
{
#region公共构造函数

public DerivedClass( args args)
:base(args)
{
}

#endregion

#region受保护的方法

受保护的覆盖无效void Responses()
{
//逻辑
}

#endregion
}


解决方案

您可以像其他任何公共静态方法一样使用抽象类的公共静态方法。例如:

  Logger.Configure(); 

您不需要 Logger 的实例以这种方式调用 Configure 。而且,您不能在不创建该类型实例的情况下调用该类型的非静态方法。因此, Logger.NonStaticMethod()不起作用。



公共或受保护的静态方法可以在类内调用超载。您不能覆盖它们,因此它们通常用作某种实用程序方法或 template 方法的不可覆盖部分:

  public class DerivedClass:AbstractClass 
{
public DerivedClass(Args args)
:base(args)
{
}

受保护的覆盖无效BuildResponses()
{
FormLoad(args);
}
}

此外,公共静态方法有时也用作工厂方法:

 公共抽象类Logger 
{
public static Logger NLog()
{
返回新的NLogLogger();
}
}

...

var logger = Logger.NLog();
logger.Log( Message);

您可以在BCL中的许多地方看到这样的用法示例: WebRequest。例如,使用Create(...)创建 HttpWebRequest FtpWebRequest 以及其他源自 WebRequest


I know it is not a best practice to use Static method in Abstract class, but what is the difference If I use both Static and non static method in abstract class.

I am assuming there is no difference in calling these methods, because we can't create instance for Abstract Class so we can call both Static and Non-static method using class name only.

Is there any other difference between them apart from the keyword 'Static'?

Ex:Abstract Class:

abstract public class AbstractClass
{
    #region Constructor
    protected AbstractClass(Args args): this(args)
    {
    }
    #endregion

    #region public static methods

    public static object FormLoad(Args args)
    {
        //Logic
    }

    public static object Sample(Args args)
    {
        //Logic
    }

    #endregion


    #region Public non-static methods

    public AbstractClass CreateResponse(Args args)
    {
        //Logic
    }

    public void ClearDialog()
    {
        //Logic
    }

    #endregion

    #region Abstract Method
    abstract protected void Responses();
    #endregion

}

Derived Class:

   public class DerivedClass : AbstractClass
   {
    #region Public Constructors

    public DerivedClass(Args args)
        : base(args)
    {
    }

    #endregion

    #region Protected Methods

    protected override void Responses()
    {
        //logic
    }

    #endregion
    }
解决方案

You can use public static method of an abstract class as any other public static method. For example:

Logger.Configure();

You don't need instance of Logger to call Configure that way. And you can't call non-static methods of type without creating instance of that type. So, Logger.NonStaticMethod() won't work.

public or protected static methods can be called within class overload. You can't override them so they usually used as some kind of utility methods or as a non-overridable part of template method:

public class DerivedClass: AbstractClass
{
    public DerivedClass(Args args)
        : base(args)
    {
    }

    protected override void BuildResponses()
    {
        FormLoad(args);
    }
}

Also, public static methods sometimes used as factory methods:

public abstract class Logger
{
    public static Logger NLog()
    {
        return new NLogLogger();
    }
}

...

var logger = Logger.NLog();
logger.Log("Message");

You can see example of such usage in many places inside BCL: WebRequest.Create(...), for example, creates HttpWebRequest, FtpWebRequest and others which are derived from WebRequest.

这篇关于抽象类的非静态方法和静态方法有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:41