为什么我不能有私人抽象方法?

时间:2015-04-20 01:46:40

标签: java oop inheritance abstract

让我说我有我的经典之作:

public abstract class Mammal {
    private int numLegs;
    private String voice;
    private Coat coat;

    public abstract void eat();

    public abstract void speak();
    public abstract void sleep();  


    private abstract void ingestFood(Food f);  //The abstract method ingestFood in type Mammal can only set a visibility modifier, one of public or protected
}

通过以下具体实施:

public class Dog extends Mammal {
     private int numLegs = 4;
     private String voice = "Woof!";
     private Coat coat = new Coat(COATTYPE.FUR, COATCOLOR.BROWN);

     @Override
     public void eat()
     { 
          Rabbit r = chaseRabbits(); 
          if (r != null) ingest(r); 
          else {
                   Garbage g = raidBin(); 
                   if (g!= null) ingest(g); 
               }


     }

     @Override
     private void ingest(Food f)
     {
         gobbleItAllUpInFiveSeconds(f); 
         stillFeelHungry(); 
     }
}

public class Human extends Mammal {
     private int numLegs = 2;
     private String voice = "Howdy!!";
     private Coat coat = new Coat(COATTYPE.SKIN, COATCOLOR.PINK);

     @Override
     public void eat()
     { 
          Food f = gotoSuperMarket();
          ingest(f); 


     }

     @Override
     private void ingest(Food f)
     {
         burp();  
     }
}

现在,我想在Mammal类中使用一个可被所有哺乳动物实例调用的方法,例如

public String describe() {
     return "This mammal has " + this.numLegs + "legs, a " + this.coat.getColor() + " " this.coat.getCoatType() + " and says " + this.voice;
}

我的问题是,通过使Mammal类不抽象,是否有可能自己制造哺乳动物? E.g。

 Mammal me = new Mammal();

你不应该这样做。

但是,我确实想要一些由所有子类调用的父类实现的公共方法,但每个方法都调用它们自己的私有方法。

3 个答案:

答案 0 :(得分:3)

您可以完全在抽象类中实现方法:

“抽象类与接口类似。您无法实例化它们,它们可能包含使用或不使用实现声明的方法组合。”

https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

答案 1 :(得分:2)

回答标题中的问题(" 为什么我没有私人抽象方法?"):

您不能使用private abstract方法,因为需要在子类中实现abstract方法。但是private方法在子类中不可见。

(如果你想拥有一个只在子类中可见而不是公开的方法,那么你需要制作方法protected

所以你无法实现private abstract方法。这就是为什么Java不允许它们 - 它们没有意义。

答案 2 :(得分:0)

如果您希望它们在子类上的行为方式不同,则将未实现的方法声明为抽象方法,并且要继承的方法将它们保留为普通方法。也可以使用protected而不是private来访问继承的类:

public abstract class Mammal
{
  protected int numLegs;
  protected String voice;
  protected Coat coat;

  abstract void eat();
  abstract void speak();
  abstract void sleep();

  public String describe()
  {
     return "This mammal has " + this.numLegs + "legs, a " 
     + this.coat.getColor() + " " this.coat.getCoatType() + " and says " + this.voice;
  }
}

使用构造函数初始化变量并实现抽象方法:

public class Dog extends Mammal
{
  public Dog(){
     this.numLegs = 4;
     this.voice = "Woof!";
     this.coat = new Coat(COATTYPE.FUR, COATCOLOR.BROWN);
  }
  void eat(){
    System.out.println("eating like a dog");
  }
  void speak(){
    System.out.println("speaking like a dog");
  }
  void sleep(){
    System.out.println("sleeping like a dog");
  }
}

public class Human extends Mammal
{
  public Human(){
     this.numLegs = 2;
     this.voice = "Howdy!!";
     this.coat = new Coat(COATTYPE.SKIN, COATCOLOR.PINK);
  }
  void eat(){
    System.out.println("eating like a human");
  }
  void speak(){
    System.out.println("speaking like a human");
  }
  void sleep(){
    System.out.println("sleeping like a human");
  }
}