简体   繁体   English

棘手的Java程序

[英]Tricky Java program

Look at the code below and please help me solve the trick. 看看下面的代码,请帮我解决这个问题。

class TestTrick{

    public static void main(String args[])
    {

    }

    static marker()
    {

    System.out.println("programe executed");

    }

}

The result required from this program is that the program should print program executed , meaning that the marker method should be executed. 该程序所需的结果是程序应该打印program executed ,这意味着应该执行marker方法。 But there are some rules: 但是有一些规则:

  1. Nothing should be written in both the methods. 这两种方法都不应该写出来。
  2. No other class can be added to the program. 没有其他类可以添加到该程序。
  3. The program must execute the output statement in the marker method. 程序必须在marker方法中执行输出语句。

It's been three days and I am unable to solve the problem because I am not a Java programmer. 这是三天,我无法解决问题,因为我不是Java程序员。 I have searched everything on internet to get a clue but I failed. 我已经在互联网上搜索了所有内容以获得线索,但我失败了。 Please someone help me run this program by strictly following the rules. 请有人帮我严格遵守规则来运行这个程序。

I think what they're looking for is a static initializer . 我认为他们正在寻找的是静态初始化器

static {
    marker();
}

This gets run when the class is loaded. 这在加载类时运行。

The problem is that you don't want a method for your marker - you just want a static initialization block: 问题是您不需要标记的方法 - 您只需要一个静态初始化块:

class Trick {
    // not a method, just something to execute when the class is loaded
    static { System.out.println("executed"); }

    public static void main(String[] args) {

    }

}

Results: http://ideone.com/bQAgT 结果: http//ideone.com/bQAgT

If you've been provided the code already (which I'm not sure you have because static marker() isn't valid code) then you can simply call marker() from your static block. 如果你已经提供了代码(我不确定你有,因为static marker()不是有效的代码),那么你可以简单地从static块中调用marker()

you can use static initializer, that will be executed before the Main method can be run. 您可以使用静态初始化程序,它将在Main方法运行之前执行。

static {
     marker();
}

The static block will do the trick for you. static块将为您提供帮助。 But you have a syntax error in your program. 但是您的程序中存在语法错误。 Since marker is a method, it must have a return type. 由于marker是一种方法,因此它必须具有返回类型。 I am assuming void . 我假设void

class testTrick {

    public static void main(String args[]) {
    }

    static void marker() {
        System.out.println("programe executed");
    }

    static {
        marker();
    }
}

This is easy. 这很容易。 In the class add a static constructor to call the marker method and then in the constructor put in a system.exit 在类中添加一个静态构造函数来调用marker方法,然后在构造函数中放入system.exit

An even trickier program is 一个更棘手的程序是

class testTrick {
    static { marker(); System.exit(0); } static void marker() {
        System.out.println("program executed");
    }
}

Note: marker has to provide a return type eg void or it won't compile. 注意:marker必须提供返回类型,例如void ,否则它将无法编译。 You can keep the main() method if you like, but it is never called. 如果愿意,可以保留main()方法,但永远不会调用它。

The static initialiser is called before the main method so it can call another method and it can exit before you see a message saying no main() method found. 静态初始化程序在main方法之前调用,因此它可以调用另一个方法,它可以在您看到没有找到main()方法的消息之前退出。

does this follow your rules ?, creating a static block will always execute as it gets stored along with the class files and it doen need a main method to call 这是否遵循您的规则?,创建静态块将始终执行,因为它与类文件一起存储,并且它需要一个主方法来调用

class TestTrick{

    public static void main(String args[])
    {

    }

    static marker()
    {

    System.out.println("programe executed");

    }

}
public class M
{
    public static void main(String[] args)
    {

    }
    static
    {
        marker();
    }
    public static void marker()
    {
         System.out.println("Done");
    }
}
class TestTrick{
public static void main(String args[])
{

}

static{
    marker();}
static void marker(){

System.out.println("programe executed");

}

Look at the requirements. 看看要求。 You can do anything you want as long as you do not alter the methods. 只要不改变方法,你就可以做任何你想做的事。

So a static block(which will always execute in a program) is needed. 因此需要一个静态块(它总是在程序中执行)。 Its not a method, and you can call marker() from there(which btw needs to be assigned a return type) 它不是一个方法,你可以从那里调用marker() (需要为btw分配一个返回类型)

class testTrick
{

    public static void main(String args[])
    {
    }

    static 
    {
        marker();
    }

    static void marker()
    {
        System.out.println("programe executed");
    }
}
int i = 10 + + 11 - - 12 + + 13 - - 14 + + 15;

System.out.println(i);

You would need to call the marker method in the main method ie; 你需要在main方法中调用marker方法,即; something like this: 这样的事情:

class testTrick {

    public static void main(String args[]) {
        marker();
    }

    static void marker() {
        System.out.println("programe executed");
    }

}

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

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