接口

接口:接口是比抽象类更为抽象的类,interface关键字提供了一个完全抽象的类,包含了类要实现的方法。一个接口的实现类必须实现接口所描述的所有方法,否则就必须声明为抽象类。

1、接口的特点:

成员变量:必须是静态的,且必须要赋初值,改制不能再被修改。默认修饰符为:public static final

成员方法:为抽象的,public abstarct 可以省略,接口中不能有一般方法。

定义Shape接口:

interface Shape {
    public static final double pi = Math.PI;

    public abstract double getArea();

    public abstract double getL();
}

定义Circle类,实现Shape接口的功能:

public class Circle implements Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getArea() {
        return radius * radius * pi;
    }

    public double getL() {
        return 2 * pi * radius;
    }

}

定义Rectangle类,实现Shape接口的功能:

public class Rectangle implements Shape {
    private double width;
    private double Length;

    public Rectangle(double width, double Length) {
        this.width = width;
        this.Length = Length;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getLength() {
        return Length;
    }

    public void setLength(double length) {
        Length = length;
    }

    public double getArea() {
        return width * Length;

    }

    public double getL() {
        return 2 * (width + Length);
    }

}

定义测试类:

public class Test {
    public static void main(String[] args) {
        Circle c = new Circle(2);
        System.out.println("圆的面积为" + c.getArea());
        System.out.println("圆的周长为" + c.getL());

        Rectangle r = new Rectangle(1, 2);
        System.out.println("长方形的面积为" + r.getArea());
        System.out.println("长方形的周长为" + r.getL());

    }
}

运行结果:

2、接口的继承

类的继承(一个类只能有一个父类):

 

 接口的多重继承:

定义第一个接口Shape1:

package 多重继承;

interface Shape1 {
    public static final double pi = Math.PI;

    public abstract double getArea();

}

定义第二个接口Shape2:

package 多重继承;

interface Shape2 {
    public abstract double getL();
}

定义Circle类,实现接口Shape1和Shape2:

package 多重继承;

public class Circle implements Shape1, Shape2 {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getArea() {
        return radius * radius * pi;
    }

    public double getL() {
        return 2 * pi * radius;
    }

}

测试类:

package 多重继承;

public class Test {
    public static void main(String[] args) {
        Circle c = new Circle(2);
        System.out.println("圆的面积为" + c.getArea());
        System.out.println("圆的周长为" + c.getL());
    }
}

运行结果:

3、接口的好处

 实现多重继承。

接口可以降低耦合性(功能能够重复利用)。

 

转载于:https://www.cnblogs.com/zhai1997/p/11343805.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值