Java:抽象类中的最终变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6557800/
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-10-30 16:21:09  来源:igfitidea点击:

Java: Final variables in abstract classes

javaconstructorabstractfinal

提问by imacake

In Java, I can't create instances of abstract classes. So why doesn't eclipse scream about the following code?

在 Java 中,我无法创建抽象类的实例。那么为什么 eclipse 不会对以下代码大喊大叫呢?

public abstract class FooType {
    private final int myvar;

    public FooType() {
        myvar = 1;
    }
}

回答by Hendrik Brummermann

The code is fine, the final variable is initialized in the constructor of FooType.

代码没问题,final变量在FooType.

You cannot instantiate FooTypebecause of it being abstract. But if you create a non abstract subclass of FooType, the constructor will be called.

你不能实例化,FooType因为它是抽象的。但是,如果您创建 的非抽象子类FooType,则会调用构造函数。

If you do not have an explicit call to super(...)in a constructor, the Java Compiler will add it automatically. Therefore it is ensured that a constructor of every class in the inheritance chain is called.

如果super(...)在构造函数中没有显式调用,Java 编译器会自动添加它。因此可以确保调用继承链中每个类的构造函数。

回答by yankee

You can have constructors, methods, properties, everything in abstract classes that you can have in non-abstract classes as well. You just can't instantiate the class. So there is nothing wrong with this code.

您可以在抽象类中拥有构造函数、方法、属性以及非抽象类中的所有内容。你只是不能实例化这个类。所以这段代码没有任何问题。

In a deriving class you can call the constructor and set the final property:

在派生类中,您可以调用构造函数并设置最终属性:

public class Foo extends FooType
{
  public Foo()
  {
    super(); // <-- Call constructor of FooType
  }
}

if you don't specify a call to super(), it will be inserted anyway by the compiler.

如果您没有指定对 super() 的调用,编译器无论如何都会插入它。

回答by aps

Ok. See, an abstract class can have a constructor. It's always there-implicit or explicit. In fact when you create an object of a subclass of an abstract class, the first thing that the constructor of the subclass does is call the constructor of its abstract superclass by using super(). It is just understood, that's why you don't have to write super()explicitly unless you use parameterized constructors. Every class even if it is abstract, has an implicit constructor which you cannot see. It is called unless you create some constructor of your own. so long you created abstract classes without creating any custom constructor in it, so you didn't know about the existence of the implicit constructor.

行。看,抽象类可以有一个构造函数。它总是隐含的或显式的。实际上,当您创建抽象类的子类的对象时,子类的构造函数所做的第一件事就是使用 super() 调用其抽象超类的构造函数。只是理解,这就是为什么super()除非您使用参数化构造函数,否则您不必显式编写。每个类即使是抽象的,也有一个你看不到的隐式构造函数。除非您创建自己的构造函数,否则它会被调用。长期以来,您创建了抽象类而没有在其中创建任何自定义构造函数,因此您不知道隐式构造函数的存在。

回答by chamzz.dot

No you can't declare final variables inside an Abstract class. Check Below example.

不,您不能在 Abstract 类中声明最终变量。检查下面的例子。

public abstract class AbstractEx {
    final int x=10;
    public abstract void AbstractEx();
}

public class newClass extends AbstractEx{

    public void AbstractEx(){
        System.out.println("abc");
        }
    }  

public class declareClass{
   public static void main(String[] args) {
       AbstractEx obj = new newClass ();
       obj.AbstractEx();
      // System.out.println(x);
    }
}

This code runs correct and produce output as

此代码运行正确并产生输出

abc

美国广播公司

But if we remove comment symbol of

但是如果我们去掉注释符号

System.out.println(x);

System.out.println(x);

it will produce error.

它会产生错误。

回答by Peter Lawrey

You can create concrete sub-classes of FooType and they will all have a final field called myvar.

您可以创建 FooType 的具体子类,它们都有一个名为 myvar 的最终字段。

BTW: A publicconstructor in an abstract class is the same as a protectedone as it can only be called from a sub-class.

顺便说一句:public抽象类中的构造函数与构造函数相同,protected因为它只能从子类中调用。

What is your doubt?

你有什么疑问?