接口:当一个抽象类,如果抽象类中的所有方法都是抽象的,
那么我们就可以把它定义为一个接口,接口是对行为的抽象。类是对属性和行为的抽象。
接口无法被直接实例化和抽象类一样。但是可以通过对象上转型来间接实例化
在接口中可以定义常量不可定义变量,这个常量默认是:final static float PI=3.14f;
它默认用final static 修饰
public interface Animal {
float PI=3.14f;
public abstract void print();
}
接口是无法直接被类继承它只能被类实现,
但是接口可以被其他接口继承(单继承或者多继承[jdk1.7以后])
接口可以被类多实现
使用接口计算圆和正方形的周长和面积
public interface Image{
final static float PI=3.14f;
public double caculateL(double r);
public double caculateS(double r);
}
class Circle implements Image{
private double r;
public Circle(double r) {
this.r = r;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
@Override
public double caculateL(double r) {
return 2*PI*r;
}
@Override
public double caculateS(double r) {
return PI*r*r;
}
}
class Square implements Image{
private double l;
public Square(double l) {
this.l = l;
}
public double getL() {
return l;
}
public void setL(double l) {
this.l = l;
}
@Override
public double caculateL(double r) {
return 4*r;
}
@Override
public double caculateS(double r) {
return r*r;
}
}
class Test{
public static void main(String[] args){
Circle circle = new Circle(5);
double r = circle.getR();
System.out.println("圆的周长是:" + " " + circle.caculateL(r) + " " + "圆的面积是:" + circle.caculateS(r));
System.out.println("=================================");
Square square = new Square(9);
double l = square.getL();
System.out.println("正方形的周长是:" + square.caculateL(l) + " " + "正方形的周长是:" + square.caculateS(l));
}
}
接口和抽象类的区别:
1、接口的所有方法都是抽象的,抽象类里面的方法可以使抽象的也可以是具体的。
2、接口和抽象类都不能实例化,接口需要类来实现后实例化实现类,
抽象类需要类来继承然后实例化子类。
3、抽象类只能单继承,接口可以多继承(jdk1.7),可以多实现。
4、接口中的属性是static final类型,抽象类中的属性跟普通类中的属性没有区别。
5、接口中的方法默认就是抽象的不需要加abstract,抽象类中的抽象方法需要加abstract
多态是同有一个行为具有多个不同的表现形式或形态的能力
多态就是同一个接口,使用不同的实例而执行不同操作。
多态存在的三个必要条件:继承、重写、父类引用指向子类对象(对象上转型)
多态的例子:
public abstract class Teacher{
String name;
public abstract void teach();
}
class JavaTeacher extends Teacher{
@Override
public void teach(){
System.out.println(this.name+"teacher is teaching java");
}
}
class PHPTeacher extends Teacher{
@Override
public void teach(){
System.out.println(this.name+"teacher is teaching PHP");
}
}
class Leader{
public void checkTeacher(Teacher teacher){
System.out.println("begin check");
teacher.teach();
System.out.println("ennd check");
}
}
class Test{
public static void main(String[] args){
Teacher teacher=new JavaTeacher();
Teacher teacher1=new PHPTeacher();
Leader leader=new Leader();
teacher.name="zzq";
leader.checkTeacher(teacher);
System.out.println("==================================================================");
teacher1.name="gaga";
leader.checkTeacher(teacher1);
}
}
public interface IHouse {
String description();
double cost();
}
class House implements IHouse{
@Override
public String description() {
return "一个毛坯房";
}
@Override
public double cost() {
return 85;
}
}
class HouseDecorator implements IHouse{
private IHouse iHouse;
public HouseDecorator(IHouse iHouse) {
this.iHouse = iHouse;
}
@Override
public String description() {
return iHouse.description();
}
@Override
public double cost() {
return iHouse.cost();
}
}
class FloorDecorator extends HouseDecorator{
public FloorDecorator(IHouse iHouse) {
super(iHouse);
}
@Override
public String description() {
return super.description()+floorDescription();
}
public String floorDescription(){
return "铺了地板";
}
@Override
public double cost() {
return super.cost()+floorCost();
}
public double floorCost(){
return 1;
}
}
class Client{
public static void main(String[] args) {
IHouse house=new House();
house=new FloorDecorator(house);
System.out.println(house.description());
System.out.println(house.cost()+"万");
}
}
例子2:
public interface Noodles{
public void description();
public double cost();
}
class OriginalNoodles implements Noodles{
@Override
public void description(){
System.out.println("这是一碗白皮面");
}
@Override
public double cost(){
return 3;
}
}
class Decorator implements Noodles{
private Noodles noodles;
public Decorator(Noodles noodles){
this.noodles=noodles;
}
@Override
public void description(){
noodles.description();
}
@Override
public double cost(){
return noodles.cost();
}
}
class BeefDecorator extends Decorator{
public BeefDecorator(Noodles noodles){
super(noodles);
}
@Override
public void description(){
super.description();
beefDecorator();
}
@Override
public double cost(){
return super.cost()+beefCost();
}
public void beefDecorator(){
System.out.println("加了牛肉");
}
public double beefCost(){
return 5;
}
}
class Client{
public static void main(String[] args){
Noodles noodles=new OriginalNoodles();
noodles=new Decorator(noodles);
noodles.description();
System.out.println(noodles.cost());
System.out.println("===========================================");
noodles=new BeefDecorator(noodles);
noodles.description();
System.out.println(noodles.cost());
}
}
重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变。即外壳 不变,核心重写!
对象上转型,子类继承的父类的方法父类可以调用,但是子类特有的方法不能通过父类来调用
实例代码:
public abstract class Teacher{
String name;
public abstract void teach();
}
class JavaTeacher extends Teacher{
@Override
public void teach(){
System.out.println(this.name+"teacher is teaching java");
}
}
class PHPTeacher extends Teacher{
@Override
public void teach(){
System.out.println(this.name+"teacher is teaching PHP");
}
}
class Leader{
public void checkTeacher(Teacher teacher){
System.out.println("begin check");
teacher.teach();
System.out.println("ennd check");
}
}
class Test{
public static void main(String[] args){
Teacher jt=new JavaTeacher();
Teacher pt=new PHPTeacher();
Leader leader=new Leader();
jt.name="zzq";
leader.checkTeacher(jt);
System.out.println("==================================================================");
pt.name="gaga";
leader.checkTeacher(pt);
}
}
我们可以通过instanceof来判断当前父类的引用的实例是什么类型
继承多态
特点:
1、必须要有继承关系,在抽血类中可以定义多态的抽象方法,通过子类来继承这个抽象类
然后 复写抽象类中的抽象方法 以达到多态的效果
2、多态子类的实例可以赋给父类的引用
10
接口多态基本上和类的继承的多态一致:不同的是类的继承使用的是继承关1系实现多态,接口采用实现的方式实现多态