使用方法
如果一个抽象类没有字段,所有方法全部都是抽象方法:
abstract class Person {
public abstract void run();
public abstract String getName();
}
就可以把该抽象类改写为接口:interface。
接口的表示方式
interface Person { //接口的源头(父类):interface 父类名
void run(); //public abstract被省略了
String getName();
}
当一个具体的class去实现一个interface时,需要使用implements关键字。
class Student implements Person {
//class 类名 implements 接口类名
private String name;
public Student(String name) {
this.name = name;
}
@Override
public void run() {
System.out.println(this.name + " run");
}
@Override
public String getName() {
return this.name;
}
}
abstract和interface
abstract:一个子类只能继承一个父类的,无法从多个父类继承;
interface:一个子类可以实现多个接口。
我们知道,在Java中,一个类只能继承自另一个类,不能从多个类继承。但是,一个类可以实现多个interface,例如:
// 实现了两个interface
class Student implements Person, Hello {...}
接口继承
一个interface可以继承自另一个interface。interface继承自interface使用extends,它相当于扩展了接口的方法。例如:
interface Hello {
void hello(); //抽象方法1
}
interface Person extends Hello {
void run(); //抽象方法2
String getName(); //抽象方法3
}
此时,Person接口继承自Hello接口,因此,Person接口现在实际上有3个抽象方法签名,抽象方法1来自继承的Hello接口。
继承关系
合理设计接口(interface)和抽象类(abstract class)的继承关系,可以充分复用代码。
一般来说,公共逻辑适合放在abstract class中,具体逻辑放到各个子类,而接口层次代表抽象程度。
//用List接口引用具体子类的实例
List list = new ArrayList();
//类名 对象名=new 类名
//向上转型为Collection接口
Collection coll = list;
//向上转型为Iterable接口
Iterable it = coll;
default方法
在接口中,可以定义default方法。例如,把Person接口的run()方法改为default方法:
// interface
public class Main {
public static void main(String[] args) {
Person p = new Student("Xiao Ming");
p.run();
}
}
interface Person {
String getName();
default void run() {
//在接口中,修改default方法
System.out.println(getName() + " run");
}
}
class Student implements Person {
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
实现类可以不必覆写default方法。
default方法的目的是,当我们需要给接口新增一个方法时,会涉及到修改全部子类。
如果新增的是default方法,那么子类就不必全部修改,只需要在需要覆写的地方去覆写新增方法。
注意点
default方法和抽象类的普通方法是有所不同的。
因为interface没有字段,default方法无法访问字段,而抽象类的普通方法可以访问实例字段。
实例
将链接中的该为用接口算税https://blog.csdn.net/weixin_53934729/article/details/115843021
public class Main(){
public static void main(String[] args){
Income[] incomes = new Income[] {
//type[] array
new BaseIncome(3000),
new SalaryIncome(7500),
new RoyaltyIncome(12000)
};
System.out.println(totalTax(incomes));
}
public static double totalTax(){
double total=0;
for(Income income:incomes){
total = total + income.getTax();
}
return total;
}
//接口
public interface Income(){
//接口内没有字段
//省略public abstract
double getTax();
}
//BaseIncome
public class BaseIncome implements Income{
protected double income;
public BaseIncome(double income) {
this.income = income;
//不能用父类继承的super了,需要重新定义构造方法
}
@Override
public double getTax(){
return income*0.1;
}
}
//SalaryIncome
public class SalaryIncome extends Income(){
protected double income;
public SalaryIncome(double income) {
this.income= income;
}
@Override
public double getTax(){
if(income<=5000){
return 0;
}
return (income-5000)*0.2;
}
}
//RoyaltyIncome
public class RoyaltyIncome extends Income(){
protected double income;
public RoyaltyIncome(double income) {
this.income = income;
}
@Override
public double getTax(){
return income*0.1;
}
}
参考链接
https://www.liaoxuefeng.com/wiki/1252599548343 744/1260456790454816
https://blog.csdn.net/weixin_53934729/article/details/115843021