为避免这种方法名称多样的情况,用一种约束叫抽象类
public abstract class Office {public abstract void print();
}
class Word extends Office {
@Override
public void print() {
System.out.println("Word打印");
}
}
class Excle extends Office{
@Override
public void print() {
System.out.println("Excel打印");
}
}
class Ppt extends Office{
@Override
public void print() {
System.out.println("PPT打印");
}
}
class Test{
public static void main(String[] args) {
Office o = new Ppt();//父类引用指向子类对象,子类重写父类方法,父类引用调用子类重写后的方法,执行的结果是子类重写后的方法
o.print();
}
}
运行结果:
PPT打印
接口是完全抽象的抽象类,并且可以多实现
接口默认为:public class abstract interface Office {
public abstract void print();
}
---------------------------------------------------------
public interface Office {
void print();
}
class Word implements Office {
@Override
public void print() {
System.out.println("Word打印");
}
}
class Excle implements Office{
@Override
public void print() {
System.out.println("Excel打印");
}
}
class Ppt implements Office{
@Override
public void print() {
System.out.println("PPT打印");
}
}
class Test{
public static void main(String[] args) {
Office o = new Ppt();
o.print();
}
}
运行结果为:
PPT打印
10、内部类
成员内部类