java - 为什么我收到错误“无法解析“x”中的方法“x””?
问题描述
这个程序的目的是为一个选择对象(在我的例子中是一个监视器)创建一个类和测试器类,至少有一个重载方法。在客户端类中,我必须实例化至少三个对象实例。到目前为止,我相信我已经完成了方法、getter 和 setter 以及构造函数的声明的第一个类。问题出现在我的测试器类中,我收到错误“无法在 'Monitor V82' 中解析方法 'MonitorV82'。我不确定为什么会收到此错误,有什么建议吗?
我的第一堂课是:
public class MonitorV82
{
private double l;
private double h;
//one parameter constructor, all private instance variables initialized
public MonitorV82(double monitor1Height) {
//2.When this gets name1(jupiter), it designates "jupiter" to the variable "n"
h = monitor1Height;
l = (monitor1Height * 1.77);
}
//two parameter constructor
public MonitorV82(double monitor1Height, double monitor1Length){
//3.When this gets name1(jupiter), and a double, it sets jupiter to "n" and the diameter to "d"
h = monitor1Height;
l = monitor1Length;
}
public double getMon1height() { return h; }
public double getMon1Length() {
return l;
}
public void setMon1height(double name) { h = name; }
public void setMon1Length(double diam) {
l = diam;
}
public String monType(int resolution)
{
String monitType = "";
if (resolution == 1080) {
monitType = "lenovo";
} else if (resolution == 4000) {
monitType = "samsung";
}
return monitType;
}
//overloaded method
public String monType(int pixLength,int pixHeight)
{
String monitType = "";
if (pixHeight == 1080) {
monitType = "lenovo";
} else if (pixHeight == 4000) {
monitType = "samsung";
}
return monitType;
}
}
我的测试人员类(错误所在)是:
public class V8Tester {
public static void main(String[] args) {
double length1 = 32.2;
double height1 = 51.8;
double length2 = 31.8;
double height2 = 50.6;
int resolution = 0;
MonitorV82 monit1 = new MonitorV82(length1);
resolution = monit1.MonitorV82(height1);
}
}
我还在学校学习 Java,所以如果事情看起来很明显或简单,请不要烤我。谢谢您的帮助。
解决方案
您收到此错误是因为没有方法MonitorV82
,只有构造函数。此外,您正在尝试int
使用对象实例化变量解析MonitorV82
,这是不可能的,因为编译器需要一个int
值。
如果您想要参考MonitorV82
具有已知像素高度的对象的像素数的分辨率,您首先需要找出它的像素长度。您可以通过使用您的getMon1length()
方法并按长度 * 高度计算分辨率来做到这一点。最终我认为你想要做的是:
int heightMonit1 = monit1.getMon1height();
int resolution = (int)length1 * (int)heightMonit1;
您需要键入 cast,因为您想通过计算双精度值来实例化int
变量。resolution
但是,您也可以使用第二个构造函数并执行以下操作:
MonitorV82 monit1 = new MonitorV82(length1, height1);
int resolution = (int)monit1.getMon1height() * (int)monit1.getMon1length();
推荐阅读
- php - 如何使用 FPDF PHP 类定义 SetXY 和 Cell?
- twilio - 使用 Twilio 一键式集成时,设置 Dialogflow ES 文本到语音语音不会改变
- python - 未转换的数据仍然是python
- java - IntelliJ IDEA 不同版本兼容性
- python - 有没有办法改变python中的错误语言?
- appendchild - 将 DOM HTML 块移动到元素中
- recursion - 递归谓词的例子
- bash - 如何在具有相似模式的文件中的同一行中查找多个字符串?
- haskell - 在执行模式匹配之前将函数应用于参数
- javascript - 从下拉列表中提取一个值。并在链接中间返回