在Effective Java的第4章中,分别讨了接口优于抽象类、为后代设计接口和接口仅用来定义类型。本篇学习记录将总结并扩展这些主题;
一、接口优于抽象类
在Java中,接口和抽象类都用于定义类的行为和功能。然而,接口具有一些优点,使其在某些情况下更适合使用。接口更灵活、更可扩展,而抽象类主要用于类继承。
以下是使用接口优于抽象类的示例代码:
public interface Animal {
void makeSound();
}
public class Dog implements Animal {
@Override
public void makeSound() {
System.out.println("Woof!");
}
}
public class Cat implements Animal {
@Override
public void makeSound() {
System.out.println("Meow!");
}
}
在上述代码中,我们定义了Animal接口,并让Dog和Cat类实现该接口。通过接口的形式,我们可以让类具备相似的行为,同时实现其他接口,并获得更大的灵活性。
二、为后代设计接口
在设计接口时,我们应该考虑到它的后代类。我们应该尽量在定义接口时提供清晰且易于理解的文档,以确保后代类正确地实现接口。我们还可以使用默认方法来为接口的后代提供默认的实现。
以下是为后代设计接口的示例代码:
public interface Animal {
void makeSound();
default void eat() {
System.out.println("Eating...");
}
}
public class Dog implements Animal {
@Override
public void makeSound() {
System.out.println("Woof!");
}
}
public class Cat implements Animal {
@Override
public void makeSound() {
System.out.println("Meow!");
}
}
在上述代码中,Animal接口定义了makeSound方法,并提供了一个默认的eat方法的实现。通过这种方式,我们为接口的后代类提供了默认的行为,同时留出了makeSound方法供具体实现。
三、接口仅用来定义类型
在设计接口时,应将其用于定义类型,而不应添加实际的实现代码。接口应该聚焦于提供行为和功能的定义,而不是具体的实现。
以下是接口仅用来定义类型的示例代码:
public interface Animal {
void makeSound();
}
public class Dog implements Animal {
@Override
public void makeSound() {
System.out.println("Woof!");
}
}
public class Cat implements Animal {
@Override
public void makeSound() {
System.out.println("Meow!");
}
}
在上述代码中,Animal接口仅包含makeSound方法的定义,没有提供任何实际的实现。这样,我们将接口的责任限制在了类型的定义上,而将具体的实现放到了具体的类中。