简体   繁体   English

Java:通过主方法的抽象类调用

[英]Java: Abstract Class Invocation via Main Method

Okay, people are probably going to run to flag this as a duplicate, just by reading the title and without really reading the question. 好吧,人们可能只是通过阅读标题并且没有真正阅读问题而将其标记为副本。 So please know that I HAVE tried to look at other questions on this platform, but have not found something that clears my doubts exactly. 所以请知道我已经尝试在这个平台上查看其他问题,但是没有找到能够完全解决我怀疑的问题。 Kindly allow me to reach out and ask my question. 请允许我伸出手来问我的问题。 Thanks in advance. 提前致谢。

Interface is absolutely abstract and cannot be instantiated; 接口绝对是抽象的,无法实例化; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists. Java抽象类也无法实例化,但如果存在main(),则可以调用它。

I do not completely understand the latter part of the statement. 我不完全理解声明的后半部分。 Is this talking about the main method being directly within the abstract class itself ? 这是在谈论主要方法直接在抽象类本身内吗? Is it talking about invoking the abstract class via a child's main method ? 它是在谈论通过子主的方法调用抽象类吗? Or both ? 或两者 ?

Secondly, I have seen examples like the following. 其次,我看过如下例子。

abstract class Printer
{
     public void print() { … };
}

public class TestPrinter
{
     public static void main( String[] args )
     {
          // use of anonymous class
          final Printer p = new Printer()
          {
               @override
               public void print()
               {
                    ...
               }
          }
     }
}

And have been told that an anonymous class is at work. 并且已经被告知匿名课程在起作用。 But, I seriously do not understand how, since the variable 'p' is clearly being assigned to... and it's an abstract class variable!! 但是,我真的不明白如何,因为变量'p'显然被分配给...而且它是一个抽象类变量!! How is that even possible? 这怎么可能呢? I thought abstract classes can not be instantiated or initialized. 我认为抽象类不能被实例化或初始化。

Any help would be appreciated. 任何帮助,将不胜感激。

 final Printer p = new Printer()
      {
           @override
           public void print()
           {
                ...
           }
      }

This means that an anonymous class is created which extends Printer and the variable p is referring to subclass instance. 这意味着创建了一个extends Printer的匿名类,而变量p指的是子类实例。

This is simply polymorphism in action. 这只是行动中的多态性 By creating anonymous class here, you are creating a subclass of Printer and using polymorphism you are using the superclass reference variable p to refer to object of subclass which is anonymous but extends Printer because of the syntax below 通过在这里创建匿名类,您正在创建Printer的子类并使用多态,您正在使用超类引用变量p来引用子类的对象,对象是匿名的,但由于以下语法而扩展了Printer

Printer p = new Printer(){...}

and this is not only limited to abstract class, you can also create an anonymous class which implements and interface. 这不仅限于抽象类,还可以创建一个实现和接口的匿名类。 Consider below example. 考虑下面的例子。

package com.test;

public interface SomeInterface {

    public void SomeMethod();

}

and a class below package com.test; 以及com.test包下面的一个类;

public class TestAnonymous {

    public static void main(String[] args) {

        SomeInterface obj = new SomeInterface() {

            @Override
            public void SomeMethod() {

                System.out
                        .println("Yaayy!!! Creating an anonymous class which implements SomeInterface ");

            }
        };

        obj.SomeMethod();

    }

}

which prints out 打印出来的

Yaayy!!! Yaayy! Creating an anonymous class which implements SomeInterface 创建一个实现SomeInterface的匿名类

What is means that the syntax creates an anonymous class which either extends the abstract class or implements the interface of which reference variable is used to instantiate. 什么意味着语法创建一个匿名类,该类扩展抽象类或实现使用引用变量实例化的接口。 It calls method of the subclass. 它调用子类的方法。

You can refer jsl. 你可以参考jsl。 https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.9.1 https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.9.1

Now your question 现在你的问题

Interface is absolutely abstract and cannot be instantiated; 接口绝对是抽象的,无法实例化; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists Java抽象类也无法实例化,但如果存在main(),则可以调用它

What I understand from your questions is whether you want to know whether main method can be run in abstract class without instantiating it, The answer is YES 我从你的问题中理解的是你是否想知道主方法是否可以在抽象类中运行而不实例化它,答案是肯定的

package com.test;

public abstract class AbstractClass {

    public static void main(String[] args) {

        System.out.println("main method in abstract class");

    }

}

compile and invoke it using java AbstractClass and it should print 使用java AbstractClass编译和调用它,它应该打印

main method in abstract class

IF you want to know whether the abstract class constructor is invoked when instantiating the anynomous class or not, then also the answer is YES. 如果你想知道在实例化anynomous类时是否调用抽象类构造函数,那么答案也是YES。 Whenver a subclass constructor is called, it invokes super class constructor by making a super() call. 当调用子类构造函数时,它通过执行super()调用来调用超类构造函数。 So abstract class constructor will also get called. 所以抽象类构造函数也会被调用。 http://www.thejavageek.com/2013/07/21/initialization-blocks-constructors-and-their-order-of-execution/ http://www.thejavageek.com/2013/07/21/initialization-blocks-constructors-and-their-order-of-execution/

An abstract class is just like any other class - except for the fact that it cannot be instantiated directly. abstract类就像任何其他类一样 - 除了它不能直接实例化的事实。 I presume you know the use for such a facility. 我认为你知道这种设施的用途。 Hence it can very well have a main() , which is a static method, not an instance method 因此它很可能有一个main() ,它是一个static方法,而不是一个实例方法

The anonymous class in your example, extend s the abstract class (or implement s an interface, if one is specified). 在您的示例中,匿名类extend了抽象类(或implement了一个接口,如果指定了一个接口)。 So p is not assigned to an abstract class instance, but to an instance of a class which extends the abstract class 因此, p不会分配给abstract类实例,而是分配给扩展抽象类的类的实例

A Java abstract class also cannot be instantiated, but can be invoked if a main() exists. Java抽象类也无法实例化,但如果存在main(),则可以调用它。

Since main() method is static an can be invoked without instantiation. 由于main()方法是静态的,因此无需实例化即可调用。 eg 例如

abstract class AbstractClass{
    public static void main(String[] args){

    }
}

The same was not true for interface till Java7, With Java8 you can have static main method inside interface hence same would be true for Java8 对于Java7, interface也是如此,使用Java8,你可以在interface内部使用静态main方法,因此Java8也是如此。

You second question, while creating an instance of Printer you have defined the sub-class of Printer as well but you can not use this defined implementation of Printer again(since there is no name associated with the implementation), Hence, its anonymous. 你的第二个问题,同时创造的一个实例Printer已定义的子类的Printer ,以及,但你不能用这个定义实现Printer再次(因为不存在与实施相关的名称),因此,它的匿名。

A Java abstract class also cannot be instantiated, but can be invoked if a main() exists. Java抽象类也无法实例化,但如果存在main(),则可以调用它。

It is nonsense. 这是无稽之谈。 Any static method of an abstract class can be invoked. 可以调用抽象类的任何静态方法。 Not just main(). 不只是main().

Secondly, I have seen examples like the following ... and have been told that an anonymous class is at work. 其次,我看过以下示例......并且被告知匿名类正在运行。

That is correct. 那是对的。

But, I seriously do not understand how, since the variable 'p' is clearly being assigned to... and it's an abstract class variable!! 但是,我真的不明白如何,因为变量'p'显然被分配给...而且它是一个抽象类变量!! How is that even possible? 这怎么可能呢? I thought abstract classes can not be instantiated or initialized. 我认为抽象类不能被实例化或初始化。

It is the anonymous class that is being instantiated here, which extends the abstract class, and provides an implementation of the abstract method. 它是在这里实例化的匿名类,它扩展了抽象类,并提供了抽象方法的实现。 The reference to that class is being stored into Person p , because Person is a superclass of the anonymous class. 对该类的引用存储在Person p ,因为Person是匿名类的超类。 You can do that for any other class or interface. 您可以为任何其他类或接口执行此操作。 There's nothing new here. 这里没有什么新东西。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM