java - 成员函数在 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 新手,所以我需要分配帮助。
解决方案
您正在创建 的实例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 中的约定是类名以大写字母开头。所以这些类应该命名为Complex
and Complex1
。但同样,你应该给他们更多不同的名字。
推荐阅读
- python - 设置多个全局变量时,“并非所有参数都在字符串格式化期间转换”
- ios - 将 Int 数据库值转换为 CoreData 问题
- php - 如何根据主机名在 .htaccess 上设置 php_value?
- discord.js - Discord.js v12 角色添加到提到的用户问题
- c - GCC:没有这样的文件或目录
- javascript - 如何在 Java Script 中硬关闭 websocket 客户端
- r - 在 R 中创建一个包含嵌套列的表
- python - 如何从 JupyterLab 工作簿中的 python 代码将输出图像数组保存到计算机上的特定文件夹中?
- forms - Zxing条码扫描器连续扫描问题
- laravel - Laravel 7.x 无法连接到 Oracle 或 Sqlsrv