2013-10-06 2 views
0

그래서 내가하지만 두 번째자바 게터의 첫 번째 GET 후 얻는 방법이 아니라 두 번째

을의 방법은 첫째 메도에서 점점 일부 이미 작업을 캡슐화를 사용하여 코드 (임 TAFE 학생) 등을 쓰고 다시하고 먼저 메도 :

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) + "%"); 



} 

세터와 게터 클래스 :

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; 
    } 
} 

당신이 볼 수있는 것은 놀라운 것

+0

'getDetails()'에 주어진 이름을'print()'에 표시하겠습니까? – kiheru

+0

그것은 단지 내 테스트 방법입니다. 세부 정보를 얻고 그 결과를 표시하고 싶었습니다. 다른 방법으로는 getter setter 메서드로 바뀌지 않았습니다. – Callum

+1

무엇이 모를 때 도움이 어렵습니다. 정확히 당신이 성취하려고 노력하고 있습니다. print()에서'fn.getFullName()'은 해당 FullName 인스턴스에 대해'setFullName()'이 호출되지 않았기 때문에'null '을 반환합니다 *. 아마도'getDetails()'를'FullName'을 리턴하게 만들고 싶지 않을 것입니다. 그렇지 않으면 인스턴스에 메소드가 있습니다. – kiheru

답변

0

내가 당신의 문제는 두 번째 방법은 당신이 "새로운"귀하의 전체 이름 클래스에 생각하지만, 결코 setFullName ("어쩌구 저쩌구"); 따라서 fn.getFullName()은 null을 리턴합니다.

+0

하지만 getDetails() 메소드의 setFullName은 무엇입니까? – Callum

+0

예, 있습니다. 마지막으로 "else"에 설정합니다. 전체 이름 –