首页 > 解决方案 > 成员函数在 JAVA 中显示错误(intelliJ IDEA)

问题描述

我编写了一个 intelliJ IDEA 中的程序。但我不知道所有函数(对象)都显示错误说.. 1. 无法解析方法“MulCom(complex1)” 2. 无法解析方法“SumCom(complex1)” 3 . 无法解析方法“SubCom(complex1)”

代码如下:

import java.util.Scanner;
public class complex1 {
    public static void main(String[] args) {
        complex1 arg1 = new complex1();
        complex1 arg2 = new complex1();
        arg1.input();
        arg1.show();
//  complex arg1;
        arg2.input();
        arg2.show();
        complex1 c = new complex1();
        System.out.println("Sum:");
        c.SumCom(arg2);
        c.show();
        System.out.println("Product:");
        c.MulCom(arg2);
        c.show1();
        System.out.println("difference:");
        c.SubCom(arg1);
        c.show();
    }
}

    class complex
    {
        double re, img;
        double a, b;

        complex() {
            re = 0;
            img = 0;
            a = 0;
            b = 0;
        }

        public void input() {
            System.out.println("Real:");
            Scanner re = new Scanner(System.in);
            System.out.println("Imagnary:");
            Scanner img = new Scanner(System.in);
        }

        public complex SumCom(complex arg1) {
            complex temp = new complex();
            temp.re = arg1.re + arg1.re;
            temp.img = arg1.img + arg1.img;
            return temp;
        }

        public complex SubCom(complex arg1) {
            complex temp = new complex();
            temp.re = arg1.re - arg1.re;
            temp.img = arg1.img - arg1.img;
            return temp;
        }

        public complex MulCom(complex arg1) {
            complex temp = new complex();
            temp.a = ((arg1.re) * (arg1.re)) - ((arg1.img) * (arg1.img));
            temp.b = ((arg1.re) * (arg1.img)) + ((arg1.re) * (arg1.img));
            return temp;

        }

        public void show() {
            System.out.println(re + "," + img + "i");
        }

        public void show1() {
            System.out.println(a + "," + b + "i");
        }
    }

我是 JAVA 新手,所以我需要分配帮助。

标签: javaintellij-idea

解决方案


您正在创建 的实例complex1,但正在尝试调用类中定义的方法complex。所以你得到一个错误,因为complex1没有input()orshow()方法。

所以如果你改变这个:

complex1 arg1 = new complex1();
complex1 arg2 = new complex1();
. . .
complex1 c = new complex1();

对此:

complex arg1 = new complex();
complex arg2 = new complex();
. . .
complex c = new complex();

它会起作用的。

正如@JFPicard 建议的那样,这就是使用更有意义和不同的名称将有所帮助的地方。你不会把事情搞混。

另外,作为旁注,Java 中的约定是类名以大写字母开头。所以这些类应该命名为Complexand Complex1。但同样,你应该给他们更多不同的名字。


推荐阅读