简体   繁体   English

在抽象类中使用非抽象方法和构造函数

[英]Use of non-abstract method and constructor in an abstract class

I am beginner to Programming language. 我是编程语言的初学者。 I have a query regarding abstract class. 我有一个关于抽象类的查询。

Abstract class can't be instantiated then what is the use of nonabstract method and constructor in an abstract class .....? 无法实例化抽象类,那么抽象类中非抽象方法和构造函数的用途是什么?

please help.... 请帮忙....

It will make sense only when there are derived classes. 仅在存在派生类时才有意义。

You may want some common methods to be implemented in the base class. 您可能希望在基类中实现一些常用方法。 Also, you may want to initialize a few thinhgs in the abstract class so you need a constructor. 另外,您可能希望在抽象类中初始化一些thinhg,因此您需要一个构造函数。

Look at this example: 看这个例子:

abstract class Fruit
{
    private Logger log;

    // init private stuff
    public Fruit() 
    {
        this.log = new Logger("C:\\Temp");
    }

    // do common functionality
    public int CalculatePrice(double weight)
    {
       return weight * GetPricePerKilo();
    }

    // specific stuff must be implemented by derived classes
    public abstract double GetPricePerKilo();
}

class Apple : Fruit
{
    // must implement in non-abstract derived class
    public override double GetPricePerKilo()
    {
        return 4.30;
    }
}

Abstract classes are there to abstract certain behaviour, while also forcing that you add at least some sort of behaviour to make it an actual object. 这里有抽象类来抽象某些行为,同时还强制您添加至少某种行为以使其成为实际对象。 Think of it as road vehicles. 可以将其视为公路车辆。 You could have an abstract class RoadVehicle that contains a non-abstract function Drive . 您可以有一个包含非抽象函数Drive RoadVehicle抽象类。 If you then inherit from the abstract function, let's say a concrete class Car , we will already have access to the Drive behaviour. 如果您随后从抽象函数继承,比如说一个具体的类Car ,我们将已经可以访问Drive行为。

If you don't want to predefine behaviour, you are better off using interfaces which only contain definitions of methods (and properties) every class implementing that interface should have. 如果您不想预先定义行为,最好使用仅包含实现该接口的每个类应包含的方法(和属性)定义的接口。 Ie we could have an IVehicle interface that has a method Move() . 也就是说,我们可以有一个IVehicle方法Move()IVehicle接口。 Well, land vehicles, air vehicles and water vehicles all move in a different way and thus it makes no sense to predefine behaviour, while in our previous case the behaviour was the same and we could already add that behaviour to the abstract class. 好吧,陆地车辆,飞机和水上车辆都以不同的方式移动,因此预先定义行为是没有意义的,而在我们以前的情况下,行为是相同的,我们已经可以将该行为添加到抽象类中。

Abstract classes are most useful in situations where some member will have behavior which every implementation will handle independently. 抽象类在某些成员将具有每个实现将独立处理的行为的情况下最有用。 Consider, for example, a ReadableDoubleMatrix class, with three properties: 例如,考虑具有三个属性的ReadableDoubleMatrix类:

int Rows {get;};
int Columns {get;}
double this[int row, int column] {get;}

The simplest general-purpose implementation might be an ImmutableArrayBackedMatrix class which contains a double[,] which is filled in when the class is constructed, but other implementations would include IdentityMatrix which contains a size value [ Rows and Columns both return size , while this[row, column] will return row==column ? 1.0 : 0.0 最简单的通用实现可能是ImmutableArrayBackedMatrix类,该类包含一个double[,] ,在构造该类时将其填充,但其他实现将包括IdentityMatrix ,该IdentityMatrix包含一个size值[ RowsColumns都返回size ,而this[row, column]将返回row==column ? 1.0 : 0.0 row==column ? 1.0 : 0.0 ]. row==column ? 1.0 : 0.0 ]。 Because those derived classes have no fields in common, it would be senseless for the base class to include either. 因为那些派生类没有共同的字段,所以基类中的任何一个都将是毫无意义的。 Without any fields, however, there's nothing meaningful a base-class instance could do with those properties. 但是,如果没有任何字段,则基类实例对这些属性没有任何意义。

Note that even if though one could eg have the base class implement Rows and Columns to return 1, and this to return 0.0, and code might in some cases find such an object useful, there wouldn't be any reason code should want to have multiple instances of such an item. 请注意,即使尽管有人可以例如具有基础类实现RowsColumns返回1,而this回0.0,代码可能会在某些情况下,找到这样的对象是有用的,不会有任何理由代码应该想拥有此类项目的多个实例。 Even if code had frequent need for such a matrix, and wanted it to be a different type from all the other matrices, it would generally be better to use for that purpose a sealed type. 即使代码经常需要这样的矩阵,并希望它与所有其他矩阵都不同,通常最好还是使用密封类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在抽象类内的非抽象方法中调用抽象方法 - Calling an abstract method in a non-abstract method within an abstract class 非抽象C#类中的无体构造函数 - Bodyless constructor in non-abstract C# class T必须是具有公共无参数构造函数的非抽象类型,以便在泛型类型或方法中将其用作参数“TModel” - T must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TModel' in the generic type or method C#:抽象类中的抽象和非抽象方法? - C#: abstract and non-abstract methods in an abstract class? 从非抽象类继承 - Inherit from a non-abstract class 从非抽象类派生抽象类 - Derive abstract class from non-abstract class 获取“T”必须是具有公共无参数构造函数的非抽象类型,以便在泛型类型或方法中将其用作参数“T” - Getting 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 如何访问预定义抽象类的非抽象属性? - How Can A Non-Abstract Property of a Predefined Abstract Class Be Accessed? 类映射错误:“ T”必须是具有公共无参数构造函数的非抽象类型 - Class Mapping Error: 'T' must be a non-abstract type with a public parameterless constructor 通用 Class 加载程序:“T”必须是具有公共无参数构造函数的非抽象类型 - Generic Class Loader: 'T' must be a non-abstract type with a public parameterless constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM