java中有抽象变量吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3562926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:53:00  来源:igfitidea点击:

Is there any abstract variables in java?

javavariablesabstract

提问by Vinoth Kumar

Can variables be abstractin Java? Do constructor support abstractvariables? I am not sure but I think the constructor supports static variables. Please clarify my doubt.

变量可以abstract在Java中吗?构造函数是否支持abstract变量?我不确定,但我认为构造函数支持静态变量。请澄清我的疑问。

回答by naikus

In java only classes and methods can be abstract. Variable declarations cannot. However you can have variable declarations whose types are abstract. See example:

在 Java 中,只有类和方法可以是抽象的。变量声明不能。但是,您可以使用抽象类型的变量声明。见示例:

public abstract class MyClass { // allowed
   public abstract myMethod(); // allowed
   public MyClass instance; // allowed

   public abstract MyClass instance; // NOT ALLOWED!!
}

回答by Andreas Dolk

The language specifacation lists 7 types of variables:

语言规范列出了 7 种类型的变量:

  1. class variables- declared as static within a class declaration
  2. instance variables- declared within a class declaration without using the static keyword
  3. array components- like i[2]when we create an array like int[] i= new int[5]
  4. method parameters- name argument values passed to a method
  5. constructor parameters- name argument values passed to a constructor
  6. exception-handler parameter- created each time an exception is caught
  7. local variables- declared in a block ({ }) or for statement
  1. 类变量- 在类声明中声明为静态
  2. 实例变量- 在类声明中声明,不使用 static 关键字
  3. 数组组件- 就像i[2]我们创建一个数组一样int[] i= new int[5]
  4. 方法参数- 传递给方法的名称参数值
  5. 构造函数参数- 传递给构造函数的名称参数值
  6. 异常处理程序参数- 每次捕获异常时创建
  7. 局部变量- 在块 ( { }) 或 for 语句中声明

You can use all variable types (except #4) in a constructor:

您可以在构造函数中使用所有变量类型(#4 除外):

class Demo {
   static int demo1 = 0;               // class variable
   int[] demo2 = new int[5];           // instance variable
   Demo(int demo3) {                   // constructor parameter
     try {
       int demo4 =                     // local variable
                   demo2[2];           // array component
     } catch(RuntimeException demo5) { // exception-handler parameter
     }
     demo2 = new int[]{Demo.demo1};    // using class and instance variable
                                       // in a constructor
   }
   int method(int demo6) {             // method parameter
   }
}

The abstractkeyword is not allowed for variable declaration.

abstract关键字是不允许的变量声明。

回答by J. Lily

abstract is a non-access modifier in java applicable for classes, methods but not variables. It is used to achieve abstraction which is one of the pillar of Object Oriented Programming.

abstract 是 Java 中的非访问修饰符,适用于类、方法但不适用于变量。它用于实现抽象,这是面向对象编程的支柱之一。