java类”a”在创建时运行方法”a”。

huangapple go评论72阅读模式
英文:

java class "a" run method when class "a" created

问题

如何在创建类a的对象时运行"print()"方法?我想在行"a obj = new a();"之后立即运行"print()"方法。

我的意思是,只调用类,而不是方法。这样它将在调用后立即运行。

class a {

    public static void print() {
        System.out.println("Hey!");
    }
    

}


public class MyClass {
    public static void main(String[] args) {
        a obj = new a();
        a.print(); // 我想在这之后立即运行print()方法

    }
}
英文:

how to run "print()" method when class a created as object? I want to run "print()" method right after the line "a obj = new a();"

I mean, just call the class, not the method. That it will operate immediately after the calling

class a {

    public static void print() {
        System.out.println("Hey!");
    }
    

}


public class MyClass {
    public static void main(String[] args) {
        a obj = new a(); // I want to run print() method right after it

    }
}

答案1

得分: 1

我猜这可能是某种奇怪的Java难题(为什么你不直接在构造函数中放置print()呢?),你可以将它放在初始化程序中:

class a {
    { print(); }
    public static void print() {
        System.out.println("Hey!");
    }
}

这将完成任务。但这是一个非常愚蠢的想法 - 在类文件级别上,无论你是这样做还是将print()语句放在构造函数中,都是一样的。我们只是在吹毛求疵语言特性。

也许退一步。你遇到了一些未知的问题。你想:我知道!我只需要以某种方式使得构造该类的对象会导致print方法运行,但又不用在构造函数中调用print!哦,但是,我怎么做呢 - 最好问问Stack Overflow。

那个想法是错误的。所以陈述一下未知的问题,而不是问一个关于错误“解决方案”的问题。

注:你还可以创建静态初始化程序,例如:

class a {
    static { print(); }
    public static void print() {
        System.out.println("Hey!");
    }
}

现在print()实际上会在那个构造函数之前运行。它也只会运行一次,就是静态初始化程序的目的:它们在你对那个a类稍微有点怪异的操作时就开始运行(无论你对它做了什么),在运行一次之后就不会再运行。

你的问题相当不清楚,所以我只是随意猜测。

英文:

I guess if this is some sort of weird java puzzler (why the heck wouldn't you just put print() in the constructor?), you can put it in an initializer:

class a {
    { print(); }
    public static void print() {
        System.out.println("Hey!");
    }
}

will get the job done. But this is a very silly idea - at the class file level it's all the same thing, whether you do this or put the print() statement in a constructor. We're just nitpicking on language features at this point.

Perhaps take a step back. You had some unknown problem. You thought: I know! I'll just somehow make construction of objects of this class cause the print method to run, but without putting a call to print in the constructor! Oh, but, how do I do that - better ask SO.

That was the wrong thought. So state the unknown problem instead of a question about a bad 'solution'.

NB: You can also make static initializers, e.g:

class a {
    static { print(); }
    public static void print() {
        System.out.println("Hey!");
    }
}

And now print() will in fact run before that constructor. It'll also run only once, ever, that's the point of static initializers: They run the moment you so much as look funny at that a class (do anything with it at all), and after they've run they are never run again.

Your question is quite unclear, so I'm just taking wild stabs here.

huangapple
  • 本文由 发表于 2020年10月12日 21:09:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64318551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定