簡體   English   中英

關於Java中的多個構造函數

[英]Concerning multiple constructors in Java

我有一個關於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)
    {

    }
}

你打電話時:

Rational r1 = new Rational(num1, den1);
Rational r2 = new Rational(num2, den2);

在程序的主方法中,您將創建兩個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類的主體內的任何地方,您都應該引用成員變量firstNumfirstDen ,而不是不屬於該類實例的變量。

我假設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對象。 一個用於存儲num1den1 ,另一個用於存儲num2den2 您已經在執行此操作:

Rational r1 = new Rational(num1,den1);
Rational r2 = new Rational(num2,den2);

Rational存儲兩個分子和兩個分母是沒有意義的。 一個有理數只有一個。

總結: r1存儲num1den1 ,而r2存儲另外兩個。 創建新的Rationalnd指的是您要創建的特定實例的分子和分母。

我不確定您對Rational的實現是否是您想要的,但是構造函數並不限於本地變量,它可以訪問它可以訪問的其他類中的任何靜態變量。

public Rational(int n, int d)
{
    n = TextLab05.num1;
    d = TextLab05.den1;
}

nd是局部變量, num1den1是類TextLab05中的靜態變量。

因此,您要為局部變量分配來自其他類的靜態值。

代碼沒有意義,因為在將值分配給在方法結束時釋放的局部變量之后,您無需對值進行任何處理。

最重要的事情是理解概念。 您將在您的Rational類中存儲一個有理數。 執行此操作時:

Rational r1 = new Rational(num1,den1);

您正在創建Rational的單個實例並將其命名為r1。 r1現在應該包含一個分子和一個分母(在本例中為num1den1 )。

假設您要將數字設為一半或1/2。 您可以這樣做:

Rational oneHalf = new Rational(1,2);

意識到new Rational(1,2)正在調用您的Rational類的構造函數。 在構造函數中,您需要將numden分配給傳遞的值(在本例中為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;

我給了一半答案,剩下的我讓你做。 不要只是復制您在這里找到的內容,請考慮您在做什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM