Java抽象类和接口

目录

抽象类

注意事项: 

抽象类作用

接口

语法使用

实现多个接口


抽象类

class Shape {
    public void draw(){
        System.out.println("Shape::draw()");
    }
}
class Rect extends Shape{
    public void draw() {
        System.out.println("♦");
    }
}
class Flower extends Shape{
    @Override
    public void draw() {
        System.out.println("❀");
    }
}
class Triangle extends Shape {
    @Override
    public void draw() {
        System.out.println("🔺");
    }
}
class Cycle extends Shape {
    @Override
    public void draw() {
        System.out.println("●");
    }
}
public class Test {
    public static void main(String[] args) {
        Rect rect = new Rect();
        Flower flower = new Flower();
        Triangle triangle = new Triangle();
        Shape[] shapes = {triangle,rect,triangle,rect,flower,new Cycle()};
        for (Shape s: shapes) {
            s.draw();
        }
    }
}
在刚才的打印图形例子中 , 我们发现 , 父类 Shape 中的 draw 方法好像并没有什么实际工作 , 主要的绘制图形都是由 Shape 的各种子类的 draw 方法来完成的 . 像这种没有实际工作的方法 , 我们可以把它设计成一个 抽象方法 (abstract method) , 包含抽象方法的类我们称为 抽象类 (abstract class)
abstract class Shape { 
 abstract public void draw(); 
}
draw 方法前加上 abstract 关键字 , 表示这是一个抽象方法 . 同时抽象方法没有方法体 ( 没有 { }, 不能执行具体 代码). 对于包含抽象方法的类, 必须加上 abstract 关键字表示这是一个抽象类 .

注意事项: 

1.抽象类不能实例化

public static void main(String[] args) {
    Shape shape = new Shape();
}

会报错:Error:(30, 23) java: Shape是抽象的; 无法实例化

2.抽象方法不能是 private

abstract class Shape { 
 abstract private void draw(); 
}
报错:Error:(4, 27) java: 非法的修饰符组合 : abstract private
3. 抽象类中可以包含其他的非抽象方法,也可以包含字段,这里的非抽象方法和普通方法的挥着都是一样的,可以被重写,也可以被子类直接调用,但是一个普通类要继承抽象类,那么必须重写抽象类当中的所有抽象方法。
abstract class Shape{
    public void func() {
        System.out.println("func");
    }
    public abstract void draw();、
}
class Rect extends Shape {
    @Override
    public void draw() {
        System.out.println("♦"+a);
        super.func();
    }
}
public class Test {
    public static void drawMap(Shape shape) {
        shape.draw();
    }
    public static void main(String[] args) {
        Shape shape = new Rect();
        shape.func();
    }
}

抽象类作用

抽象类存在的最大意义就是为了被继承,抽象类本身并不能被实例化,要想使用,只能创建该抽象类的子类,然后让子类重写抽象类中的抽象方法。在使用的时候,会多一重编译器的校验。因为直接使用父类的时候就会报错误。

接口

语法使用

interface IShape {
    void draw();
}
class Cycle implements IShape {
    @Override
    public void draw() {
        System.out.println("○");
    }
}
public class Test5 {
    public static void main(String[] args) {
        IShape shape = new Cycle();
        shape.draw();
    }
}
1.使用 interface 定义一个接口
2.接口中的方法一定是抽象方法 , 因此可以省略 abstract
3.接口中的方法一定是 public, 因此可以省略 public
4.Cycle 使用 implements 继承接口 . 此时表达的含义不再是 " 扩展 ", 而是 " 实现 "
5.在调用的时候同样可以创建一个接口的引用 , 对应到一个子类的实例 .
6.接口不能单独被实例化

实现多个接口

有些时候我们需要让一个类同时继承多个父类,但是 Java 实现不了多继承。不过可以通过同时实现多个接口来达到多继承类似的效果。通过类来表示一组动物(通过接口来调用就不用关心引用是谁了):

class Animal {
    protected String name;

    public Animal(String name) {
        this.name = name;
    }

}
interface IFlying {
    void fly();
}
interface IRunning{
    void run();
}
interface ISwimming{
    void swimming();
}
class Bird extends Animal implements IFlying{
    public Bird(String name) {
        super(name);
    }
    @Override
    public void fly() {
        System.out.println(this.name+"正在飞");
    }
}
class Frog extends Animal implements IRunning,ISwimming{
    public Frog(String name) {
        super(name);
    }
    @Override
    public void run() {
        System.out.println(this.name + "正在跑");
    }
    @Override
    public void swimming() {
        System.out.println(this.name+"在游泳");
    }
}
class Duck extends Animal implements IRunning,ISwimming,IFlying{
    public Duck(String name) {
        super(name);
    }
    @Override
    public void fly() {
        System.out.println(this.name+"正在飞");
    }
    @Override
    public void run() {
        System.out.println((this.name+"正在跑"));
    }
    @Override
    public void swimming() {
        System.out.println(this.name+"在游泳");
    }
}
class Roobot implements IRunning{
    @Override
    public void run() {
        System.out.println("机器人在跑");
    }
}
public class Test4 {
    public static void runFunc(IRunning iRunning){
        iRunning.run();
    }
    public static void swimmingFunc(ISwimming iSwimming){
        iSwimming.swimming();
    }
    public static void flyingFunc(IFlying iFlying){
        iFlying.fly();
    }

    public static void main(String[] args) {
        runFunc(new Duck("鸭子"));
        runFunc(new Frog("青蛙"));
        runFunc(new Roobot());
    }
}

//打印结果
鸭子正在跑
青蛙正在跑
机器人在跑

在这里就可以利用接口来完成需要的功能,通过同时实现多个接口来完成功能。 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值