簡體   English   中英

靜態類中的接口實現

[英]Interface implementation in static class

我正在嘗試理解一個Java代碼,這是代碼

 package RestClient;

    import java.io.OutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.io.PrintStream;
    import java.io.BufferedWriter;
    import java.io.Writer;
    import java.io.OutputStreamWriter;
    import java.util.InputMismatchException;
    import java.io.IOException;
    import java.io.InputStream;


    public class Test {
        public static void main(String[] args) {
            InputStream inputStream = System.in;
            System.out.println("Its done");
            OutputStream outputStream = System.out;
            InputReader in = new InputReader(inputStream);
            OutputWriter out = new OutputWriter(outputStream);
            TaskE solver = new TaskE();
            solver.solve(1, in, out);
            out.close();
        }

        static class TaskE {
            static long[] dp;
            static long[] sum;
            static int n;
            static SegmentTreeRMQ tree;

            public void solve(int testNumber, InputReader in, OutputWriter out) {
                System.out.println("2");
                n = in.nextInt();
                System.out.println("It always come Here");
                int c = in.nextInt();
                if (c == 1) {
                    out.println(0);
                    return;
                }
                int[] a = in.nextIntArray(n);
                dp = new long[n];
                sum = new long[n + 1];
                for (int i = 1; i <= n; i++) {
                    sum[i] = sum[i - 1] + a[i - 1];
                }
                tree = new SegmentTreeRMQ();
                tree.constructST(a, n);
                out.println(comp(a, 0, n - 1, c));
            }

            private long comp(int[] a, int l, int r, int c) {
                //System.out.println(l+" "+r);
                if (r < l) return 0;
                if (r == l && c == 1) return 0;
                else if (r == l) return a[l];
                if (dp[l] != 0) return dp[l];
                int min = a[l];
                long val = sum[r + 1] - sum[l];
                val = Math.min(val, (a[l] + comp(a, l + 1, r, c)));
                if (l + c <= n)
                    val = Math.min(val, sum[l + c] - sum[l] - tree.RMQ(n, l, l + c - 1) + comp(a, l + c, r, c));
                //System.out.println("val:"+l+" "+val);
                dp[l] = val;
                return val;
            }

        }

        static class InputReader {
            private InputStream stream;
            private byte[] buf = new byte[1024];
            private int curChar;
            private int numChars;
            private InputReader.SpaceCharFilter filter;

            public InputReader(InputStream stream) {
                System.out.println("1");
                this.stream = stream;
            }

            public int read() {
                System.out.println("Its4");

                System.out.println("numChars Near 4: " +numChars);
                System.out.println("curChar Near 4: " +curChar);
                if (numChars == -1) {
                    throw new InputMismatchException();
                }
                if (curChar >= numChars) {
                    curChar = 0;
                    try {
                        System.out.println("5");
                        numChars = stream.read(buf);
                        System.out.println("numChars: "+numChars);
                        System.out.println("Print: "+new String(new byte[]{ (buf[curChar]) }, "US-ASCII"));

                    } catch (IOException e) {
                        throw new InputMismatchException();
                    }
                    if (numChars <= 0) {
                        return -1;
                    }
                }
                System.out.println("curChar++: "+curChar);
                return buf[curChar++];

            }

            public int nextInt() {
                System.out.println("3");
                int c = read();
                System.out.println("c: "+c);
                while (isSpaceChar(c)) {
                    System.out.println("6");    
                    c = read();
                }
                int sgn = 1;
                System.out.println("7");
                if (c == '-') {
                    System.out.println("8");
                    sgn = -1;
                    c = read();
                }
                int res = 0;
                do {
                    if (c < '0' || c > '9') {
                        throw new InputMismatchException();
                    }
                    System.out.println("9");
                    res = res*10;
                    res = res+c - '0';
                    c = read();
                } while (!isSpaceChar(c));
                return res * sgn;
            }

            public boolean isSpaceChar(int c) {
                if (filter != null) {
                    System.out.println("here");
                    return filter.isSpaceChar(c);
                }
                return isWhitespace(c);
            }

            public static boolean isWhitespace(int c) {
                return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
            }

            public int[] nextIntArray(int n) {
                int[] array = new int[n];
                for (int i = 0; i < n; ++i) array[i] = nextInt();
                return array;
            }

            public interface SpaceCharFilter {
                public boolean isSpaceChar(int ch);

            }

        }

        static class SegmentTreeRMQ {
            int[] st;

            int minVal(int x, int y) {
                return (x < y) ? x : y;
            }

            int getMid(int s, int e) {
                return s + (e - s) / 2;
            }

            int RMQUtil(int ss, int se, int qs, int qe, int index) {
                // If segment of this node is a part of given range, then
                // return the min of the segment
                if (qs <= ss && qe >= se)
                    return st[index];

                // If segment of this node is outside the given range
                if (se < qs || ss > qe)
                    return Integer.MAX_VALUE;

                // If a part of this segment overlaps with the given range
                int mid = getMid(ss, se);
                return minVal(RMQUtil(ss, mid, qs, qe, 2 * index + 1),
                        RMQUtil(mid + 1, se, qs, qe, 2 * index + 2));
            }

            int RMQ(int n, int qs, int qe) {
                // Check for erroneous input values
                if (qs < 0 || qe > n - 1 || qs > qe) {
                    System.out.println("Invalid Input");
                    return -1;
                }

                return RMQUtil(0, n - 1, qs, qe, 0);
            }

            int constructSTUtil(int arr[], int ss, int se, int si) {
                // If there is one element in array, store it in current
                //  node of segment tree and return
                if (ss == se) {
                    st[si] = arr[ss];
                    return arr[ss];
                }

                // If there are more than one elements, then recur for left and
                // right subtrees and store the minimum of two values in this node
                int mid = getMid(ss, se);
                st[si] = minVal(constructSTUtil(arr, ss, mid, si * 2 + 1),
                        constructSTUtil(arr, mid + 1, se, si * 2 + 2));
                return st[si];
            }

            void constructST(int arr[], int n) {
                // Allocate memory for segment tree

                //Height of segment tree
                int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));

                //Maximum size of segment tree
                int max_size = 2 * (int) Math.pow(2, x) - 1;
                st = new int[max_size]; // allocate memory

                // Fill the allocated memory st
                constructSTUtil(arr, 0, n - 1, 0);
            }

        }

        static class OutputWriter {
            private final PrintWriter writer;

            public OutputWriter(OutputStream outputStream) {
                writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
            }

            public OutputWriter(Writer writer) {
                this.writer = new PrintWriter(writer);
            }

            public void close() {
                writer.close();
            }

            public void println(long i) {
                writer.println(i);
            }

            public void println(int i) {
                writer.println(i);
            }

        }
    }

疑問:

1)InputReader是Test類的內部類,其中SpaceCharFilter接口是使用聲明的一個方法創建的。 我只看到可以通過在類上使用實現來使用接口的地方,例如,類實現接口{實現方法},但是在這種情況下,它是使用類的變量過濾器設置的。 我可以知道該功能在Java中如何工作嗎?

2)在isSpaceChar(int c)方法中,過濾器變量的意義是什么? 過濾器變量中可以包含哪些類型的值?

3)我正在使用此輸入“ 10 2”運行代碼,然后輸入。 理想情況下,它應該從is的輸入字符“ 10 2”中讀取輸入的“ 0”中的第二個字符時,在isSpaceChar方法中使用filter!= null條件,但它沒有進行,請問為什么?

誰能幫助我消除這些疑問?

  1. 這就是多態性的含義 :我們定義一個接口並調用其中定義的方法,而不關心實際的實現是什么(但希望實現與接口協定相匹配,后者由接口名稱,名稱來定義)其中定義的方法及其可能包含的任何注釋)。

  2. 它可以保存一個實現SpaceCharFilter的對象。

  3. 這是因為變量filter 從不分配對象。 因此,接口和變量在此代碼中均無用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM