2014-07-08 3 views
0

선생님이 저에게이 과제를주었습니다. 전체 프로그램을 완벽하게 작동 시켰지만 4 단계에서 이해할 수없는 부분이 있습니다. 여기서는 declawed 값을 설정하는 방법을 과부하 상태로 설정하려고합니다. 이 프로그램에서 부울 값을 설정하기 위해 Cat 클래스의 set 메서드를 사용하여 잘못된 질문을 던지려고합니다. 왜 내가 과부하 방법이 필요할까요? 누군가가 주제에 대해 생각할만한 것을 제공 할 수 있다면 그것은 대단 할 것입니다. 감사!자바 부울 오버로드 된 설정 메서드

1.Create a new class called Cat that includes the functionality below 

2.The new class has the attributes of: 
name – type String 
age – type integer 
weight – type double 
breed - type String 
declawed - type boolean - true for has no claws, false for has claws 

3. Be sure your classes have a reasonable complement of constructor, accessor and 
mutator methods. Every member variable must have at least one independent accessor and 
one independent mutator. 

4. Example: 
    public void setName(String name) mutator used to set name 
    public void setBreed(String breed) mutator used to set the breed 
    public void set(Boolean declawed) used to set claws or not 
    ***(You must overload the set method to set deClawed value)**** 
    public String getName() accessor used to get name 
    public String getBreed() accessor used to get breed 
    public boolean getBoolean() access used to get the value of declawed 

다음은 올바른 출력입니다.

예 실행 : 고양이 1의 이름을 입력 : 샘

고양이 1의 나이를 입력하기 : 1

고양이 1의 무게를 입력하면 : 5

고양이 1의 품종을 입력 : fluffy1

고양이가 발톱을 가지고 있습니까? 참 또는 거짓? : 참

답변

0

맞아요, 그건 말이 안됩니다.

Java에서 overloading 메서드는 같은 이름으로 여러 번 정의하지만 다른 메서드 서명을 의미합니다.

같은 것을 볼 것이다 set() 방법을 오버로드의 예 : 자바 이후,

public void set(int age) { 
    this.age = age; 
} 
public void set(Boolean declawed) { 
    this.declawed = declawed; 
} 

당신은 이러한 요구 사항이하지 않는 것, 그리고 어떤 경우는 뮤 테이터 방법을 과부하 아무 의미를 메서드 setFoo()setBar()을 호출하여 foobar 변수의 값을 설정하는 명명 규칙이 있습니다.

아마도 교수님은 을 구현해야한다고 말했습니까? 설정 방법은 무엇입니까?

어쨌든 프로그램이 실수없이 작동하더라도 교사에게 이야기하여 프로그램 요구 사항을 명확히하도록 요청하는 것이 좋습니다.

+0

:

이 코드를 사용해보십시오. 저, 또는 벙어리 교수 (당연히 무례 함). –

0

나는 단지 연습을위한 것 같아. 부울 편리 즉, 그래서

그러나 기술적으로 부울! = 부울

별로 차이가 있지만, 래퍼 종류가 있어요는 null 값을 받아들입니다. null 값을 허용하는 열의 데이터베이스에서 읽은 데이터에 대해 set 메소드를 사용하는 경우.

프리미티브 부울은 메모리를 적게 사용합니다. 나는 우리가 표시되지 않은 어딘가에 자신의 골격 코드의 다른 설정 방법이 있으리라 믿고있어

public class Cat { 
boolean declawed; 
public void set(boolean declawed){ 
    System.out.println("boolean called"); 
    this.declawed=declawed; 
} 
public void set(Boolean declawed){ 
    System.out.println("Boolean called"); 
// if(declawed==null){declawed=false;} 
    this.declawed=declawed; 
} 
    public static void main(String[] args) { 
     Cat c=new Cat(); 
     c.set(Boolean.TRUE); 
     c.set(true); 
     c.set(null); 
    } 
} 
관련 문제