【问题标题】:How can I display a variable from another class without using getters and setters?如何在不使用 getter 和 setter 的情况下显示另一个类的变量?
【发布时间】:2020-12-20 11:42:38
【问题描述】:

我是一年级学生,我在这里真的很挣扎。这是我必须为我的作业做的问题之一(我可以使用 StackOverflow 作为指导)。

创建一个名为Customer 的类,该类将确定客户对赊购产品的每月还款金额。该类有五个字段:customer namecontact numberproduct pricenumber of monthsthe monthly repayment amount

为每个字段编写get和set方法,每月还款金额字段除外。设置方法必须提示用户输入以下字段的值:customer namecontact numberproduct pricenumber of months

这个类还需要一个计算每月还款额(产品价格除以月数)的方法。

添加一个名为Finance_Period 的子类,它将确定客户是否会支付利息。

如果支付产品的月数大于三个月,客户将支付 25% 的利息,否则不计利息。

为产品付款的最长月数为 12 个月。

通过确定客户是否支付利息并计算每月还款额来覆盖calculate_repayment() 方法。

创建一个名为 Customer_Finance 的类,其中包含测试这两个类的逻辑。

提示用户输入第一个不感兴趣的对象的数据并显示结果;然后提示用户提供感兴趣的数据并显示结果。

在不使用getterssetters 的情况下,我很难将amtRepay 调用到我的主目录中。

我什至不确定我是否正确理解了这个问题,任何指导或建议将不胜感激。

另外,我还有一个名为 Finace_Period 的课程,还没有任何内容,我还不能 100% 确定我在做什么。

这是我的主要课程,我想在其中显示amtRepay

package main;

import javax.swing.JOptionPane;


public class Main {

    
    public static void main(String[] args) {
        //Variables
        String name;
        int cNumber, months;
        double price;
        
        //Input
        name = JOptionPane.showInputDialog(null, "Please enter the customer's name:");
        cNumber = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the customer's contact number:"));
        price = Double.parseDouble(JOptionPane.showInputDialog(null, "Please enter the price of the product:"));
        months = Integer.parseInt(JOptionPane.showInputDialog(null, "Please enter the number of repayment months:"));
        
        Customer c = new Customer(name, cNumber, months, price);
        
        JOptionPane.showMessageDialog(null, c.calcaAmtRepay());
                  
        }
    
    }

这是我的中学课程,计算amtRepay

package Main;

public class Customer extends Finance_Period {
    
    //Atributes
    private String name;
    private int cNumber, months;
    private double price, amtRepay;
    
    //Constructors
    public Customer (String name, int cNumber, int months, double price) {
        this.name = name;
        this.cNumber = cNumber;
        this.months = months;
        this.price = price;
       
        
    }

    //Getters
    public String getName() {
        return name;
    }

    public int getcNumber() {
        return cNumber;
    }
    
    public int getMonths() {
        return months;
    }
    
     public double getPrice() {
        return price;
    }
     
    //Setter
    public void setName(String name) {
        this.name = name;
    }

    public void setcNumber(int cNumber) {
        this.cNumber = cNumber;
    }

    public void setMonths(int months) {
        this.months = months;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    //Calculation of monthly repayments
    public double calcAmtRepay () {
       
        amtRepay = price / months;
        
        return price / months;
    }
}

谢谢。

【问题讨论】:

  • 主课的输出是什么?
  • 线程“main”中的异常 java.lang.RuntimeException:无法编译的源代码 - 错误的树类型:main.Main.main(Main.java:25)处的 main.Customer 它只是给了我这个错误当谈到输出 amtRepay @jaSnom
  • 首先,我认为您需要定义另一个构造函数,因为您的默认构造函数需要 AmtRepay,而您没有传递。因此您可以定义一个新的构造函数帽子,只包含您传递的参数:public Customer (String name, int cNumber, int months, double price) { this.name = name; this.cNumber = cNumber; this.months = months; this.price = price; }
  • 也不能直接访问私有变量。您需要将它们公开或定义一个吸气剂。在这种情况下,getter 就足够了,因为您不想从外部更改它,如果我是正确的。
  • 好的,但是我不能使用 getter 和 setter?我已从构造函数中删除了 amtRepay。

标签: java


【解决方案1】:

更改您的构造函数:保留 amtRepay 参数。

//Constructors
public Customer (String name, int cNumber, int months, double price) {
    this.name = name;
    this.cNumber = cNumber;
    this.months = months;
    this.price = price;
}

请注意,调用此构造函数后,仍不计算 amtRepay 字段。更好:您甚至不需要 Customer 中的 amtRepay 字段,因为每次调用 calcAmtRepay() 时都会计算其值,这是一个好主意,否则您还必须在 setMonthssetPrice 中重新计算它。

public double calcAmtRepay () {
    return price / months;    
}

JOptionPane.showMessageDialog(null, c.calcAmtRepay());

【讨论】:

  • “不起作用”是什么意思?你得到的错误是什么?编译错误,运行时错误?
  • 线程“main”java.lang.RuntimeException 中的异常:无法编译的源代码 - 错误的树类型:main.Main.main(Main.java:25)处的 main.Customer 这就是我得到的
【解决方案2】:

在您的班级main.Main 中,您指的是班级main.Customer,但根据您发布的代码,您只有班级Main.Customer

在您的Customer.java 中将package Main; 更改为package main;,它应该可以工作。

编辑:

我没有 Netbeans,所以我使用安装了 Java 扩展的 Visual Studio Code。

我创建了一个空目录。在该目录中,我创建了文件build.xml 和子目录src。在src 内部,我创建了包main 并复制了您的代码。我还创建了空类main.Finance_Period

我的 IDE 通知我 main.Customer 类有 calcAmtRepay() 方法,但在 main.Main 类中你调用 calcaAmtRepay() - 注意在 calcAmt 之间附加的小写 a。我修复了这个问题,然后将以下内容添加到我的build.xml

<project name="MyProject" default="dist" basedir=".">
  <description>
    Java assignment build file
  </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist" location="dist"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source">
    <!-- Compile the Java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile"
        description="generate the distribution">
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the java-assignment-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/lib/java-assignment-${DSTAMP}.jar" basedir="${build}"/>
  </target>

  <target name="clean"
        description="clean up">
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>

然后我在shell中运行ant dist,最后执行:

java -cp dist/lib/java-assignment-20200901.jar main.Main

运行应用程序。

编译和执行都运行良好。

我使用的是 OpenJDK 11.0.8 2020-07-14。

如果有任何帮助,请告诉我。

编辑 2:

我将工作代码放在GitLab 上。你可以忽略Dockerfile 和对 Docker 的引用,其余的我已经解释过了。

【讨论】:

  • 请将 Finance_Period 的代码添加到您的问题中。
  • 目前还没有,我还得去找它
  • 要明确 - 这是一个没有任何metgods或字段的空类,或者该类根本不存在?
  • 课程在那里,但那里什么都没有
  • 是在包main 还是Main 中?你用什么来构建和运行这段代码?
猜你喜欢
  • 1970-01-01
  • 2018-09-21
  • 1970-01-01
  • 2017-06-11
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多