构造函数和类型转换

时间:2015-04-13 09:35:23

标签: java casting

  

我从网站上看到了关于隐式cast的误解。我在编译器上运行了这个代码。它显示错误为"方法的返回类型丢失"在构造函数的第7,11,15,19行中(但构造函数不应该有任何返回类型)而且错误类似于" TypeTest无法解析为类型"在第24行。这里有什么不对?   怎么纠正这个?

[http://mrbool.com/java-data-type-conversion/29257]

(对代码的常见误解)

public class TypeText {

TypeTest(double a, double b, short c) {
    System.out.println("1 (dbldbl short)");
}

TypeTest(float a, byte b, long c) {
    System.out.println("2 (float byte long)");
}


TypeTest(long a, long b, long c) {
    System.out.println("3 (long long long)");
}

TypeTest(float a, long b, short c) {
    System.out.println("4 (float long short)");
}

TypeTest(double a, double b, double c) {
    System.out.println("5 (dbldbldbl)");
}

public static void main(String[] args) {
    TypeTest t = new TypeTest();
    t.TypeTest(3.4, 3L, 3);
}

1 个答案:

答案 0 :(得分:3)

错字:

更改

public class TypeText

public class TypeTest

另外:

TypeTest t = new TypeTest(); // this is invalid since you have no parameterless
                             // constructor
t.TypeTest(3.4, 3L, 3); // this is invalid since you have no TypeTest method in
                        // your class

也许你打算尝试:

TypeTest t = new TypeTest(3.4, 3L, 3);
相关问题