简体   繁体   English

成员函数在JAVA中显示错误(intelliJ IDEA)

[英]Member Functions showing error in JAVA (intelliJ IDEA)

I coded a program which is in intelliJ IDEA.But i dont know y all the functions (objects) are showing error saying.. 1. Cannot resolve method "MulCom(complex1)" 2. Cannot resolve method "SumCom(complex1)" 3. Cannot resolve method "SubCom(complex1)" 我编写了一个使用intelliJ IDEA编写的程序。但是,我不知道所有功能(对象)都显示错误提示。。1.无法解析方法“ MulCom(complex1)” 2.无法解析方法“ SumCom(complex1)” 3 。无法解析方法“ SubCom(complex1)”

The code is as follows: 代码如下:

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");
        }
    }

Im new to JAVA, so i need help for assignment. 我是JAVA的新手,所以我需要分配帮助。

You are creating instances of complex1 , but are trying to call methods defined in the class complex . 您正在创建complex1实例,但是正在尝试调用complex类中定义的方法。 So you are getting an error since complex1 does not have an input() or show() method. 因此,由于complex1没有input()show()方法,您会遇到错误。

So if you change this: 因此,如果您更改此设置:

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

to this: 对此:

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

it will work. 它会工作。

This is where using more meaningful and distinct names, as @JFPicard suggested, will help. 如@JFPicard所建议的,在这里使用更有意义和独特的名称会有所帮助。 You won't get things mixed up. 您不会混淆。

Also, as a side note, the convention in Java is that class names start with an uppercase letter. 另外,请注意,Java中的约定是类名以大写字母开头。 So those classes should be named Complex and Complex1 . 因此,这些类应命名为ComplexComplex1 But again, you should give them more distinct names. 但同样,您应该给它们起更多不同的名称。

SumCom takes a complex parameter which is not convertible to complex1. SumCom采用一个复杂参数,该参数不能转换为complex1。

Also the arg1.input call is invalid. 同样,arg1.input调用无效。

Just change your arg1 parameters to complex from complex1. 只需将arg1参数从complex1更改为complex。

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

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