我对Java中的构造函数的多个实例有疑问。
我的任务是接收两个分数,然后乘以并除以这些分数。
我不确定如何为类对象本身的实例设置单独的值。
以下是我遇到问题的示例代码:
import java.util.Scanner;
public class TextLab05
{
static int num1, den1; // numerator and denominator of the 1st rational number
static int num2, den2; // numerator and denominator of the 2nd rational number
public static void main (String args[])
{
enterData();
Rational r1 = new Rational(num1,den1);
Rational r2 = new Rational(num2,den2);
}
}
class Rational
{
private int firstNum; // entered numerator
private int firstDen; // entered denominator
private int num; // reduced numerator
private int den; // reduced denominator
public Rational()
{
}
public Rational(int n, int d)
{
n = TextLab05.num1;
d = TextLab05.den1;
//Here specifically is where I am having comprehension issues. How can I include num2 and den2 if I only have int n and int d?
}
}
如果在上下文中难以理解,这里是我给出的完整起始代码:
import java.util.Scanner;
public class TextLab05
{
static int num1, den1; // numerator and denominator of the 1st rational number
static int num2, den2; // numerator and denominator of the 2nd rational number
public static void main (String args[])
{
enterData();
Rational r1 = new Rational(num1,den1);
Rational r2 = new Rational(num2,den2);
Rational r3 = new Rational();
r3.multiply(r1,r2);
System.out.println("\n\n" + r1.getOriginal() + " * " + r2.getOriginal() + " = " + r3.getRational());
r3.divide(r1,r2);
System.out.println("\n" + r1.getOriginal() + " / " + r2.getOriginal() + " = " + r3.getRational());
// 100 Point Version Only
// r3.add(r1,r2);
// System.out.println("\n" + r1.getOriginal() + " + " + r2.getOriginal() + " = " + r3.getRational());
// r3.subtract(r1,r2);
// System.out.println("\n" + r1.getOriginal() + " - " + r2.getOriginal() + " = " + r3.getRational());
System.out.println();
}
public static void enterData()
{
Scanner input = new Scanner(System.in);
System.out.print("\nEnter the 1st numerator ----> ");
num1 = input.nextInt();
System.out.print("\nEnter the 1st denominator --> ");
den1 = input.nextInt();
System.out.print("\nEnter the 2nd numerator ----> ");
num2 = input.nextInt();
System.out.print("\nEnter the 2nd denominator --> ");
den2 = input.nextInt();
}
}
class Rational
{
private int firstNum; // entered numerator
private int firstDen; // entered denominator
private int num; // reduced numerator
private int den; // reduced denominator
public Rational()
{
}
public Rational(int n, int d)
{
n = TextLab05.num1;
d = TextLab05.den1;
}
private int getGCF(int n1,int n2)
{
int rem = 0;
int gcf = 0;
do
{
rem = n1 % n2;
if (rem == 0)
gcf = n2;
else
{
n1 = n2;
n2 = rem;
}
}
while (rem != 0);
return gcf;
}
public int getNum()
{
return TextLab05.num1;
}
public int getDen()
{
return TextLab05.den1;
}
public double getDecimal()
{
return (double)TextLab05.num1 / TextLab05.den1;
}
public String getRational()
{
String rational = "" + TextLab05.num1 + "/" + TextLab05.den1;
return rational;
}
public String getOriginal()
{
String original = "" + TextLab05.num1 + "/" + TextLab05.den1;
return original;
}
public void reduce()
{
}
public void multiply(Rational r1, Rational r2)
{
}
public void divide(Rational r1, Rational r2)
{
}
public void add(Rational r1, Rational r2)
{
}
public void subtract(Rational r1, Rational r2)
{
}
}
答案 0 :(得分:5)
致电时:
Rational r1 = new Rational(num1, den1);
Rational r2 = new Rational(num2, den2);
在程序的main方法中,您将创建两个Rational类实例,一个名为r1,另一个名为r2。因为要将int值传递给Rational构造函数,所以将调用的构造函数是需要两个整数参数的构造函数:
public Rational(int n, int d)
{
...
}
编译器知道这一点,因为它匹配构造函数的名称以及传递的参数类型(称为匹配构造函数的“签名”)。
在您提供的代码中,Rational Constructor代码实际上没有意义 - 这段代码:
public Rational(int n, int d)
{
n = TextLab05.num1;
d = TextLab05.den1;
}
应该看起来像这样:
public Rational(int n, int d)
{
this.firstNum = n;
this.firstDen = d;
}
将值n和d传递给构造函数,然后在构造函数的主体中实例变量firstNum和firstDen(它们在Rational类的私有部分中声明,并且实际上“属于”正在创建的实例然后将初始化为n和d的值。
Rational类体内的任何地方都应该引用成员变量firstNum
和firstDen
,而不是那些不属于类实例的变量。
答案 1 :(得分:1)
我假设Rational
类应该代表一个有理数。你说:
//Here specifically is where I am having comprehension issues. How can I include num2 and den2 if I only have int n and int d?
你不需要在Rational
类中存储两个分子和两个分母。您只需要创建两个Rational
个对象。一个用于存储num1
和den1
,另一个用于存储num2
和den2
。你已经这样做了:
Rational r1 = new Rational(num1,den1);
Rational r2 = new Rational(num2,den2);
在Rational
中存储两个分子和两个分母是没有意义的。有理数只有一个。
总结:r1
存储num1
和den1
,而r2
存储其他两个。当您创建新的Rational
时,n
和d
会引用您正在创建的特定实例的分子和分母。
答案 2 :(得分:1)
我不确定您的Rational实现是否符合您的意图,但构造函数不仅限于本地变量,它可以访问其可以访问的其他类中的任何静态变量。
public Rational(int n, int d)
{
n = TextLab05.num1;
d = TextLab05.den1;
}
n
和d
是局部变量,num1
和den1
是类TextLab05
中的静态变量。
因此,您要使用来自另一个类的静态值分配局部变量。
代码没有意义,因为在将值分配给方法结束时处置的局部变量后,您不会对值执行任何操作。
答案 3 :(得分:0)
最重要的是要理解这个概念。您将在Rational
班级中存储有理数。当你这样做时:
Rational r1 = new Rational(num1,den1);
您正在创建Rational
的单个实例并将其命名为r1。 r1
现在应该包含分子和分母(在这种情况下为num1
和den1
)。
假设您要将数字设为一半,或1/2。你可以这样做:
Rational oneHalf = new Rational(1,2);
意识到new Rational(1,2)
正在调用Rational
类的构造函数。在构造函数中,您需要将num
和den
分配给传递的值(在本例中为1和2)。所以你需要这样的东西:
this.num = num1;
this.den = den1;
因此,如果您希望能够将一个Rational
与另一个Rational
相乘,则需要一个方法或函数来执行此操作。在Rational
课程中,创建一个名为multiply(Rational anotherRational)
的方法。
该功能将执行以下操作:
this.num = this.num * anotherRational.num;
this.den = this.den * anotherRational.den;
我放弃了一半答案,我会让你做其余的事。不要只是复制你在这里找到的东西,想想你正在做什么。