首页 > 解决方案 > 为什么我收到错误“无法解析“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,所以如果事情看起来很明显或简单,请不要烤我。谢谢您的帮助。

标签: 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();

推荐阅读