gpt4 book ai didi

java - 静态类中的接口(interface)实现

转载 作者:行者123 更新时间:2023-12-02 02:17:12 25 4
gpt4 key购买 nike

我正在尝试理解一段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接口(interface)并声明了一个方法。我只看到可以通过在类上使用实现来使用接口(interface)的地方,例如类实现接口(interface){实现方法},但在这种情况下,它是使用类的变量过滤器设置的。请问这个功能在 Java 中是如何工作的?

2) isSpaceChar(int c)方法中过滤变量的意义是什么?过滤器变量中可以包含什么类型的值?

3)我正在运行代码,输入“10 2”,然后输入。理想情况下,它应该在 isSpaceChar 方法中进入 filter!=null 条件,同时从“10 2”中的输入“0”读取第二个字符,但它不会,我可以知道为什么吗?

谁能帮我解答这些疑惑吗?

最佳答案

  1. 这就是多态的含义:我们定义一个接口(interface)并调用其中定义的方法,而不关心实际的实现是什么(但期望实现与接口(interface)相匹配) 契约由接口(interface)的名称、其中定义的方法的名称以及它可能具有的任何注释来定义)。

  2. 它可以容纳一个实现 SpaceCharFilter 的对象。

  3. 这是因为变量 filter 永远不会分配对象。因此,接口(interface)和变量在这段代码中都是无用

关于java - 静态类中的接口(interface)实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49147852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com