我听到很多新的编程语言都是动态输入的,但是当我们说一种语言是动态类型而非静态类型时,它实际意味着什么呢?
computer-science programming-languages static-typing dynamic-typing
给定一组类,找到最近的公共超类的最佳方法是什么?
例如,给出以下内容:
interface A {}
interface B {}
interface AB extends A, B {}
interface C {}
class AImpl implements A {}
class ABImpl implements AB {}
class ABImpl2 implements A, B {}
class BCImpl implements B, C {}
Run Code Online (Sandbox Code Playgroud)
我希望以下(不详尽):
commonSuperclass(A, AImpl) == A
commonSuperclass(A, B, C) == Object or null, I'm not picky
commonSuperclass(A, AB) == A
commonSuperclass(AImpl, ABImpl) == A
commonSuperclass(ABImpl, ABImpl2) == either A or B or both, I'm not picky
commonSuperclass(AImpl, ABImpl, ABImpl2) == A
commonSuperclass(ABImpl, …Run Code Online (Sandbox Code Playgroud) 此方法应采用两个相同类型的对象,并随机返回其中一个对象:
public static <T> T random(T o1, T o2)
{
return Math.random() < 0.5 ? o1 : o2;
}
Run Code Online (Sandbox Code Playgroud)
现在,为什么编译器接受具有不同类型的两个参数?
random("string1", new Integer(10)); // Compiles without errors
Run Code Online (Sandbox Code Playgroud)
编辑: 既然我知道这两个参数都被隐式上升,我想知道为什么编译器在调用以下方法时会抱怨:
public static <T> List<T> randomList(List<T> l1, List<T> l2) {
return Math.random() < 0.5 ? l1 : l2;
}
Run Code Online (Sandbox Code Playgroud)
呼叫:
randomList(new ArrayList<String>(), new ArrayList<Integer>()); // Does not Compile
Run Code Online (Sandbox Code Playgroud)
如果那些ArrayList参数也被上传到Object,为什么这次会给我一个错误?