Java Design Issue: Enforce method call sequence

后端 未结 12 594
青春惊慌失措
青春惊慌失措 2021-02-02 05:27

There is a question which was recently asked to me in an interview.

Problem: There is a class meant to profile the execution time of the code. The class

12条回答
  •  悲&欢浪女
    2021-02-02 06:06

    As Per Interview question ,It seems to like this

    Class StopWatch {
    
        long startTime;
        long stopTime;
        public StopWatch() {
        start();
        }
    
        void start() {// set startTime}
        void stop() { // set stopTime}
        long getTime() {
    stop();
    // return difference
    
    }
    
    }
    

    So now All user need to create object of StopWatch class at beginning and getTime() need to call at End

    For e.g

    StopWatch stopWatch=new StopWatch();
    //do Some stuff
     stopWatch.getTime()
    

提交回复
热议问题