java - 错误 - :不兼容的类型:从 int 到 short 的可能有损转换
问题描述
我知道当我们尝试向下转换值时会发生此错误,但在我的代码中我无法弄清楚我在哪里向下转换了值。
class TestClass {
public static void main(String args[] ) throws Exception {
TestDemo obj=new TestDemo();
TestDemo2 obj1= new TestDemo2();
obj.show(5);
obj1.show("helloworld");
}
}
class TestDemo{
public void show(short N){
System.out.println(N*2);
}
}
class TestDemo2{
public Void show(String S){
System.out.println(S);
}
}
解决方案
由于 obj.show(5) 而发生此错误。
两个修复`你可以做任何事情:
class TestClass {
public static void main(String args[] ) throws Exception {
TestDemo obj=new TestDemo();
TestDemo2 obj1= new TestDemo2();
obj.show((short)5);
obj1.show("helloworld");
}
}
class TestDemo{
public void show(short i){
System.out.println(i*2);
}
}
class TestDemo2{
public void show(String S){
System.out.println(S);
}
}
第二版
class TestClass {
public static void main(String args[] ) throws Exception {
TestDemo obj=new TestDemo();
TestDemo2 obj1= new TestDemo2();
obj.show(5);
obj1.show("helloworld");
}
}
class TestDemo{
public void show(int i){
System.out.println(i*2);
}
}
class TestDemo2{
public void show(String S){
System.out.println(S);
}
}
推荐阅读
- r - 我想根据产品列在 R 中编写串联代码
- node.js - 在 npm (laravel) 中编译 webpack 时出错
- android - 如何更改 xamarin.android 中默认显示警报的背景颜色?
- java - 无法在测试注释方法中传递驱动程序值
- python-3.x - 如何在 matplotlib ax.imshow() 中使用“extent”而不更改覆盖的 ax.text() 句柄的位置?
- python - 更改重复值索引
- actionscript-3 - 这是我的 AS3 AIR 桌面应用程序中的内存泄漏,还是我不应该担心?
- neo4j - Neo4j:启动 Neo4j 太慢并且失败
- python - 有没有办法逐步计算列的数据框的平均值?
- python - 我想在python中使用excel的v-lookup