내가 슈퍼 클래스가, 그 다음과 같습니다내 수퍼 클래스에서 서브 클래스의 이름을 사용할 수 있습니까?
public abstract class Fish {
protected int size;
protected float weight;
//constructor
public Fish(int s, float w) {
size = aSize;weight = aWeight;}
나는이 슈퍼 클래스의 두 서브 클래스가 있습니다. 먼저 하나
public class SomeFish extends Fish{
//constructor
public SomeFish(int s, float w) {
super(s, w) }
두 번째, • 12cm 0.5 :
public class AnotherFish extends Fish{
//constructor
public AnotherFish(int s, float w) {
super(s, w) }
은 내가해야 할 일은처럼 뭔가를 반환 할 것 물고기 클래스의 String의 toString 메소드를 작성하는 것입니다 여기 물고기의 적절한 유형이어야합니다 (AnotherFish 또는 SomeFish). 문자열 toString 메서드를 추상화하지 않고 그것을 할 수 있고 SomeFish 및 AnotherFish 클래스 둘 다에서 문자열 toString 메서드를 구현할 수 있습니까?
불투명 질문 –
toString이 Object 클래스에 있으므로 추상화 할 필요가 없습니다. 필요한 경우 모든 클래스에서이를 재정의하십시오. – slipperyseal
@KugathasanAbimaran 물고기의 이름을 가진 문자열을 AnotherFish 또는 SomeFish로 반환해야합니다. – user2926550