Java 如何在不使用getter和setter的情况下显示另一个类中的变量?
我是一名一年级学生,我在这里真的很挣扎。这是我必须为我的任务做的问题之一(我可以使用StackOverflow作为指导) 创建一个名为Java 如何在不使用getter和setter的情况下显示另一个类中的变量?,java,Java,我是一名一年级学生,我在这里真的很挣扎。这是我必须为我的任务做的问题之一(我可以使用StackOverflow作为指导) 创建一个名为Customer的类,该类将确定客户赊购产品的每月还款金额。该类有五个字段:客户名称,联系电话,产品价格,月数,以及每月还款金额 为每个字段(每月还款金额字段除外)编写get和set方法。设置方法必须提示用户输入以下字段的值:客户名称,联系电话,产品价格,以及月数 此类还需要一种方法来计算每月还款金额(产品价格除以月数) 添加名为Finance\u Period的
Customer
的类,该类将确定客户赊购产品的每月还款金额。该类有五个字段:客户名称
,联系电话
,产品价格
,月数
,以及每月还款金额
为每个字段(每月还款金额字段除外)编写get和set方法。设置方法必须提示用户输入以下字段的值:客户名称
,联系电话
,产品价格
,以及月数
此类还需要一种方法来计算每月还款金额(产品价格除以月数)
添加名为Finance\u Period
的子类,该子类将确定客户是否支付利息
如果支付产品的月数大于三个月,客户将支付25%的利息,否则不适用利息
支付产品的最长月数为12个月
通过确定客户是否支付利息并计算每月还款金额,覆盖计算还款()方法
创建一个名为Customer\u Finance
的类,该类包含测试这两个类的逻辑
提示用户输入不感兴趣的第一个对象的数据,并显示结果;然后提示用户输入感兴趣的数据,并显示结果
我正在努力调用amtRepay
,而不使用getters
和setters
我甚至不确定我是否正确理解了这个问题,任何指导或建议都将不胜感激
另外,我还有另一门课叫《金融周期》
,那里还没有什么,我还不能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;
}
}
谢谢。更改构造函数:保留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字段,因为每次调用CalCamtreath()
时都会计算它的值,这是一个好主意,因为否则您还必须在setMonths
和setPrice
中重新计算它
public double calcAmtRepay () {
return price / months;
}
JOptionPane.showMessageDialog(null, c.calcAmtRepay());
在类
main.main
中,您指的是类main.Customer
,但根据您发布的代码,您只有类main.Customer
更改<代码>主包装代码>至<代码>主包装在Customer.java
中使用code>,它应该可以工作
编辑:
我没有Netbeans,所以我使用安装了Java扩展的Visual Studio代码
我创建了一个空目录。在该目录中,我创建了文件build.xml
和子目录src
。在src
内部,我创建了包main
并复制了您的代码。我还创建了空类main.Finance\u Period
我的IDE通知我类main.Customer
有方法calamtreaty()
,但在类main.main
中调用calcAmtRepay()
-注意calc
和Amt
之间的附加小写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>
运行应用程序
编译和执行都运行良好
我使用了OpenJDK 11.0.8 2020-07-14
请让我知道这是否有帮助
编辑2:
我打开了工作代码。您可以忽略
Dockerfile
和对Docker的引用,其余的我已经解释过了。主类的输出是什么?线程“main”java.lang.RuntimeException中的异常:不可编译的源代码-错误的树类型:main.Customer at main.main(main.java:25)它只是在输出amtRepay@jaSnomFirst时给了我这个错误。首先,我认为您需要定义另一个构造函数,因为您的默认构造函数需要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就足够了,因为你不想从外部更改它。好吧,但我不能使用getter和setter?我已经从我的建设者那里删除了amtRepay。你说的“不工作”是什么意思?你犯了什么错误?编译错误,运行时错误?线程“main”java.lang.RuntimeException中的异常:不可编译的源代码-错误树类型:main.main.main中的main.Customer(main.java:25)这是我得到的。请在您的问题中添加财务期间的代码。目前还没有,我仍然需要弄清楚-它是一个空类,没有任何metgoods或字段,或者该类根本不存在?该类在那里,但绝对没有任何内容。它在包main
或main
中吗?您使用什么来构建和运行此代码?
java -cp dist/lib/java-assignment-20200901.jar main.Main