为什么我不能有私有抽象方法?
Why can't I have an private abstract method?
假设我有 classic:
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
class 中的一个方法,该方法可由哺乳动物的所有实例调用,例如
public String describe() {
return "This mammal has " + this.numLegs + "legs, a " + this.coat.getColor() + " " this.coat.getCoatType() + " and says " + this.voice;
}
我的问题是,通过使 Mammal
class 不抽象,是否可以单独创造哺乳动物?例如
Mammal me = new Mammal();
你不应该这样做。
但是,我确实想要一些由父 class 实现的 public 方法,所有子 class 都调用它们,但每个方法都调用自己的私有方法。
你完全可以在抽象中实现方法class:
"Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation."
https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
如果您希望未实现的方法在子 类 上有不同的行为,请将它们声明为抽象方法,并且要继承的方法像普通方法一样保留它们。还使用 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");
}
}
回答标题中的问题(“为什么我不能有私有抽象方法?”):
不能有private abstract
方法,因为abstract
方法需要在子类中实现。但是 private
方法在子类中不可见。
(如果你想要一个只在子类中可见的方法,而不是公开的,那么你需要使方法protected
)
所以你永远无法实现 private abstract
方法。这就是 Java 不允许它们的原因 - 它们没有意义。
假设我有 classic:
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
class 中的一个方法,该方法可由哺乳动物的所有实例调用,例如
public String describe() {
return "This mammal has " + this.numLegs + "legs, a " + this.coat.getColor() + " " this.coat.getCoatType() + " and says " + this.voice;
}
我的问题是,通过使 Mammal
class 不抽象,是否可以单独创造哺乳动物?例如
Mammal me = new Mammal();
你不应该这样做。
但是,我确实想要一些由父 class 实现的 public 方法,所有子 class 都调用它们,但每个方法都调用自己的私有方法。
你完全可以在抽象中实现方法class:
"Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation."
https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
如果您希望未实现的方法在子 类 上有不同的行为,请将它们声明为抽象方法,并且要继承的方法像普通方法一样保留它们。还使用 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");
}
}
回答标题中的问题(“为什么我不能有私有抽象方法?”):
不能有private abstract
方法,因为abstract
方法需要在子类中实现。但是 private
方法在子类中不可见。
(如果你想要一个只在子类中可见的方法,而不是公开的,那么你需要使方法protected
)
所以你永远无法实现 private abstract
方法。这就是 Java 不允许它们的原因 - 它们没有意义。