我们可以将变量声明为Abstract吗? [英] Can we declare a variable as Abstract ?

查看:306
本文介绍了我们可以将变量声明为Abstract吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以将变量声明为Abstract吗?

如果是,那么在Abstract Class中将变量定义为抽象有什么用??

解决方案

No。

您只能将类声明为抽象,将变量声明为类的引用(或作为值类型)。

您可以声明变量作为对抽象类的引用,没关系:



  public   abstract   class  MyBase {...} 
公开 MyDerived:MyBase {...}
private void MyMethod()
{
MyBase base = new MyDerived();
}

并且 base 可以保存对从MyBase派生的任何类的引用。



同样,在抽象基类中,你不能声明一个抽象字段:

  public   abstract   string  myString; 

因为派生类必须匹配签名完全 - 那么重点在哪里?该字段也可以在基类中定义。

然而,您可以声明抽象属性:

  public   abstract   class  MyBase 
{
public abstract string MyString {获得; 受保护 set ; }
}

然后派生类必须提供实现。


不,你不能。

当你声明一个变量你声明了你指定的类/结构的实例。

抽象类不能直接实例化。



目的是什么一个抽象变量?



http://en.wikipedia.org / wiki / Abstract_type [ ^ ]


是的,你可以,虽然你实际上定义了抽象属性,而不是VARIABLES。我不认为声明变量是可能的......但我承认我不确定这个。



使用:强制类的继承者实现特殊功能。



假设您有发票类,订单类和退货舱,所有这些都与您的货物有关(销售,从供应商处收货和买家退货) )。他们都有一些常见的事情要做,他们有两个人/公司细节(发件人,收件人),项目清单,总价等标题......



你想强迫每个人拥有的是totalAmount和totalTax字段......你会声明它们是抽象的,任何从你的抽象类继承的人都必须实现它(即使它返回的总是相同(比如tax = 0)实施

Can we declare a variable as Abstract ?
If Yes then What is the use of defining a variable as abstract in a Abstract Class..?

解决方案

No.
You can only declare classes as abstract, and variables as references to classes (or as value types).
You can declare a variable as a reference to an abstract class, that's fine:

public abstract class MyBase { ... }
public class MyDerived : MyBase { ... }
private void MyMethod()
   {
   MyBase base = new MyDerived();
   }

And base can hold a reference to any class derived from MyBase.

Similarly, within the abstract base class, you can't declare an abstract field:

public abstract string myString;

Because the derived class would have to match the signature exactly - so where is the point? The field may as well be defined in the base class.
You can however declare abstract properties:

public abstract class MyBase
    {
    public abstract string MyString { get; protected set; }
    }

And then the derived classes must provide the implementation.


No you cannot.
When you declare a variable you declare an instance of the class/struct you specified.
Abstract classes cannot be directly instantiated.

And what would be the purpose of an abstract variable?

http://en.wikipedia.org/wiki/Abstract_type[^]


Yes, you can, although you actually define abstract PROPERTIES, not VARIABLES. I don't think declaring variables is even possible...but I admit I am NOT sure about this.

Use: forcing the inheritor of the class to implement that particular functionality.

Say you have an invoice class, order class and return class,all of them dealing with your goods (selling, receiving from your supplier and returns from buyers). And they all have some common things to do, they have headers with two people / firm details (sender, receiver), item list, total price etc...

What you want to force everyone is to have totalAmount and totalTax fields...you would declare them Abstract and anyone inheriting from your abstract class would have to implement it (even if it returned always the same (say tax = 0) for that particular implementation.


这篇关于我们可以将变量声明为Abstract吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆