[英]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
值[ Rows
和Columns
都返回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. 请注意,即使尽管有人可以例如具有基础类实现Rows
和Columns
返回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.