简体   繁体   English

Java上的类型不匹配,并且无法实例化对象的类型

[英]Type mismatch on java and cannot instantiate the type of object

right now i'm learning about interface in Java and now i got confused when this error is pop out 现在我正在学习Java接口,现在弹出此错误时我很困惑

  • Type mismatch: cannot convert from Bola to bangunRuang 类型不匹配:无法从Bola转换为bangunRuang
  • Cannot instantiate the type Bola 无法实例化类型Bola

and the code is 和代码是

package Modul5no2;

public interface bangunRuang {

public double keliling();

public double luas();

public double volume();
}  *//this is the interface*

public class kotakBalok implements bangunRuang {
double keliling,luas,volume;
public double keliling(double p, double l, double t){
    keliling = 4*(p+l+t);
    return keliling;
}

public double luas(double p,double l, double t){
    luas = 2*((p*l)+(p*t)*(l*t));
    return luas;
}

public double volume(double p,double l, double t){
    volume = p*l*t;
    return volume;
}

} *//this is for collect from interface*


public class Driver {
public static void main(String[]args){

    bangunRuang br1 = new Bola();  //i assume this is the problem

    br1.keliling();
    br1.luas();
    br1.volume();
}
}

ok thank u for your attention hope you understand what i mean lol xD 好的,谢谢您的关注,希望您理解我的意思。

Its actually not clear what you want to achieve here. 实际上不清楚您要在这里实现什么。

Didn't you get compilation error in class kotakBalok because you have not provided body or implemented the methods of the interface bangunRuang as your class class kotakBalok implements bangunRuang . 您没有在kotakBalok类中得到编译错误,因为您没有在class kotakBalok implements bangunRuang提供正文或实现bangunRuang接口的方法。 Though you have methods with same name but have different signature than the methods of interface , So that doesn't count as providing implementation of methods of interface. 尽管您具有与接口方法同名的方法但具有不同的签名,所以这不算是提供接口方法的实现。

In your Driver class you are calling new on class Bola ie bangunRuang br1 = new Bola() but you in the code you provided the class Bola does not exist. 在您的Driver类中,您正在类Bola上调用new,即bangunRuang br1 = new Bola()但是在您提供的类Bola中的代码中您不存在。 perhaps you want to do bangunRuang br1 = new kotakBalok(); 也许您想做bangunRuang br1 = new kotakBalok();

At last this will not work if you have not provided the implementation of methods of interface bangunRuang in class kotakBalok 最后,如果您未在bangunRuang类中提供接口bangunRuang的方法的实现,则此操作将kotakBalok

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM