,所以我正在编写使用封装(我是个TAFE学生)一些已经工作的代码和方法是在第一梅索德越来越但不是在第二Java的getter方法第一个GET后获得,但不是第二
首先了Methode:
public void getDetails() throws IOException
{
do
{
String fullName = JOptionPane.showInputDialog(null, "Enter your full name", "Input Request", JOptionPane.QUESTION_MESSAGE);
f = true;
if (fullName == null)
{
System.out.println("Program has been aborted!");
System.exit(0);
} else
{
f = true;
}
Pattern numbers = Pattern.compile("[1-9,[email protected]#$%^&*()_+=]");
Matcher match = numbers.matcher(fullName);
if (match.find())
{
JOptionPane.showMessageDialog(null, "Incorrect!\nLetters Only", "Error", JOptionPane.ERROR_MESSAGE);
f = false;
} else if (fullName.isEmpty())
{
JOptionPane.showMessageDialog(null, "pelase enter a name!", "Error", JOptionPane.ERROR_MESSAGE);
f = false;
} else
{
f = true;
FullName fn = new FullName();
fn.setFullName(fullName);
System.out.println(fn.getFullName());
}
} while (f == false);
}
第二种方法:
public void print()
{
DecimalFormat df, df1;
df = new DecimalFormat("0.00");
FullName fn = new FullName();
System.out.println(fn.getFullName());
JOptionPane.showMessageDialog(null, fn.getFullName()+ "\nYour tax outcome is: " + "\nIncome Tax $" + incomeTax + "\nMedicare Levy $" + df.format(medicareLevy)
+ "\nTax Paid $" + taxWitheld + sReturn + "\nActual Tax Rate " + df.format(actualTaxRate) + "%");
}
setter和getter类:
public class FullName
{
private String fullName;
public FullName()
{
fullName = null;
}
public FullName(String n1)
{
fullName = n1;
}
public String getFullName()
{
return fullName;
}
public void setFullName(String name)
{
this.fullName = name;
}
}
你可能会看到的任何东西将是惊人的
是为了显示'getDetails()'中给出的名称(在'print()')中? – kiheru
它只是我的测试方法,我想从获取详细信息中获取名称并在最后显示它,我还有其他4种方法,但我还没有将其更改为getter setter方法 – Callum
当我不知道什么时候很难提供帮助正是你想要实现的。在print()中''fn.getFullName()''返回'null',因为对于那个FullName实例*没有调用setFullName()。你可能会想'getDetails()'返回'FullName',否则那个方法的本地实例。 – kiheru