注:这是本人笔试期间总结的一些java知识点,太简单的没写,太深奥的疏漏较多,主要是针对较难易错点的的一些探讨,错误之处还望留言斧正。
class Pair {
private T min;
public Pair() {
this.min = null;
}
public Pair(T min) {
this.min = min;
}
public T getMin() {
return min;
}
public void setMin(T min) {
this.min = min;
}
}
public static T getMiddle(T... ts) {
return ts[ts.length / 2];
}
ArrayAlg.getMiddle(1, 2);
ArrayAlg.getMiddle(1, 2);
Number n = ArrayAlg.getMiddle(1, 2, 2.2);
Object o = ArrayAlg.getMiddle(1, 2.2, "hello");
public static > T min(T[] ts) {
if (ts == null || ts.length == 0)
return null;
T min = null;
for (T t : ts)
min = min.compareTo(t) > 0 ? t : min;
return min;
}
public class Main {
public static void main(String[] args) {
Utils.printEmployee(new Pair(new Manager("Job"), new Manager("Ketty")));
}
}
class Utils {
// 方法一:不能接收Manager
public static void printEmployee(Pair e) {
System.out.println("first:" + e.getFirst().getName());
System.out.println("second:" + e.getSeconde().getName());
}
// 方法二:能接收Manager
public static void printEmployee(Pair extends Employee> e) {
System.out.println("first:" + e.getFirst().getName());
System.out.println("second:" + e.getSeconde().getName());
}
}
class Pair {
private T first;
private T seconde;
public Pair(T first, T second) {
this.first = first;
this.seconde = second;
}
// getter and setter...
}
class Employee {
private String name;
public Employee(String name) {
this.name = name;
}
// getter and setter...
}
class Manager extends Employee {
private int rank;
public Manager(String name) {
super(name);
}
// getter and setter...
}
public class Main {
public static void main(String[] args) {
System.out.println(test02());
}
public static int test02() {
int[] nums = new int[] {1,2};
try {
System.out.println("try...");
System.out.println(nums[1]);
// System.out.println(nums[2]);
return 0;
} catch (Exception e) {
System.out.println("catch...");
return 1;
}finally {
System.out.println("finally...");
return 2;
}
}
}
public static T[] copyOf(U[] original, int newLength, Class extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class) ? (T[]) new Object[newLength] :(T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
return copy;
}
public static Runtime getRuntime() {
return currentRuntime;
}