Is there a Java equivalent to Javascript's with statement?

This question already has an answer here:

  • WITH statement in Java 7 answers

  • If the class of obj is under your control, you could provide a Fluent interface, basically returning this in every function. This would let you chain method calls like this-

    obj.getHomeworkAverage().getTestAverage().getAttendance();


    不,在Java中没有声明或类似的构造。


    There is no direct equivalent of "with".

    If the methods are instance methods, you can give the target object reference a short identifier for use in a block:

    {
      Student s = student;
      s.getHomeworkAverage();
      s.getTestAverage();
      s.getAttendance();
    }
    

    If the methods are static, you can use "import static":

    import static java.lang.Math.*;
    
    public class Test {
      public static void main(String[] args) {
        System.out.println(sqrt(2));
      }
    }
    
    链接地址: http://www.djcxy.com/p/51690.html

    上一篇: jQuery和$问题

    下一篇: 是否有一个Java语言与JavaScript相当?