Java 抽象类与接口

本文详细介绍了抽象类和接口在面向对象编程中的概念、语法、特性及应用。抽象类用于定义类的公共行为,不允许实例化,子类必须实现其抽象方法。接口则定义了公共规范,提供了一种多继承的方式,实现接口的类需要覆盖接口的所有方法。通过实例,展示了抽象类和接口在实际编程中的用法,强调了它们在代码可读性和多态性中的重要性。
摘要由CSDN通过智能技术生成

1.抽象类

1.1抽象类的概念

在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,一个类中没有包含足够的信息来描绘一个具体的对象,这个类就是抽象类

比如:Animal是动物类 具有叫的方法 但是因为Animal不是一个具体的动物 因此其bark()方法就无法具体实现 因此应该把Animal设置为抽象类

1.2抽象类语法

abstract class shape{
    public static  int b;
    public int a;
    public  void functionA()

    {
        System.out.println("A-Function");
    }
    //抽象类可以 定义普通方法 属性等
    public abstract void draw();//抽象方法
}

抽象类语法比较简单 就是class前加了abstract关键字‘

1.3抽象类的一些属性

//Shape shape = new Shape()  会报错 抽象类是不可以实例化的
abstract class Shape{
    abstract private void draw();
}
//编译出错
Error:(4,27)java:非法的修饰符组合:abstract和private

抽象方法亦不可被finalstatic修饰  因为抽象方法要被子类重写的

抽象类必须被继承 并且继承后子类要重写父类中的所有抽象方法 否则子类也必须是抽象类

一个例子

​
abstract class shape{
    public static  int b;
    public int a;
    public  void functionA()

    {
        System.out.println("A-Function");
    }
    //抽象类可以 定义普通方法 属性等
    public abstract void draw();//抽象方法
}
class Rect extends shape{
    @Override
    public void draw(){
        System.out.println("画一个矩形");
    }
}
class Flower extends shape{
    @Override
    public void draw() {
        System.out.println("画一朵❀");
    }
}
public class Test {
    //Shape shape = new Shape()  会报错 抽象类是不可以实例化的
    public static void drawMap(shape shape){
        shape.draw();
    }
    public static void main(String[] args) {
        drawMap(new Rect());
        drawMap(new Flower());
    }
}

​

1.4抽象类作用

多了一层编译器的校验 且有利于代码的可读性 等

2.接口

2.1接口的概念

直观地理解接口 可以理解为 统一的规范 比如

无论是笔记本上的Usb接口 或者是插孔上的接口 都是符合人为规定的一个规范  

接口就是公共的行为规范标准 大家在实现时 只要符合规范标准就可以通用 

在Java中 接口可以看成时 多个类的公共规范 是一种引用数据类型

--使用 interface 修饰接口

--在接口中可以定义成员变量 且默认为被 public static final 修饰

--接口中的成员方法 默认被 public abstract 修饰

--接口本身不可实例化

--类和接口的关系是使用 implements 来关联的 意为当前类实现了接口

 2.2语法

package IntefercrTest;
interface Shape{
    void draw();
}
class Rect implements Shape{

    @Override
    public void draw() {
        System.out.println("画矩形");
    }
}

class Flower implements Shape{

    @Override
    public void draw() {
        System.out.println("画❀");
    }
}

public class Test {
    public static void drawMap(Shape shape){
        shape.draw();
    }

    public static void main(String[] args) {
        drawMap(new Flower());
        drawMap(new Rect());//向上转型
        Shape shape = new Flower();
        shape.draw();
    }
}

 

--接口可以引用 具体实现类 相当于向上转型

2.3例子

  一个例子

package Test;
abstract class Animal{               //抽象类Animal
    public String name;
    public int age;

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

    public abstract void eat();//抽象方法 eat()
}
interface Irunning{
    void run();
}

class Dog extends Animal implements Irunning{
    public Dog(String name , int age){
        super(name,age);
    }

    @Override
    public void eat() {
        System.out.println(name+"正在吃狗粮");
    }

    @Override
    public void run() {
        System.out.println(name+"正在摇着尾巴跑");
    }
}

public class Test {
    public static void func(Animal animal){
        animal.eat();
    }

    public static void main(String[] args) {
        func(new Dog("二哈",3));
    }
}

 

一个例子

​
package Test02;
class Student implements Comparable{      //实现了Comparable接口
    String name;
    int age;
    double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Object o) {          //重写了compareTo方法

       if (this.age>((Student) o).age){
           return 1;
       }else if (this.age==((Student) o).age){
           return 0;
       }else {
           return -1;
       }
    }


}
public class Test01 {
    public static void main(String[] args) {
        Student student1 = new Student("x",18,87);
        Student student2 = new Student("y",28,67);
        if (student1.compareTo(student2)>0){
            System.out.println(student1.name+"年长于"+student2.name);
        }else if (student1.compareTo(student2)==0){
            System.out.println("二人同岁");
        }else {
            System.out.println(student2.name+"年长于"+student1.name);
        }
    }
}

​

 一个例子

public class Test01 {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0]= new Student("Eric",18,66);
        students[1]= new Student("Hoke",15,66);
        students[2]= new Student("Joe",32,66);
        System.out.println("排序前"+Arrays.toString(students));

        Arrays.sort(students);

        System.out.println("排序后"+Arrays.toString(students));
    }
}

 总结一下:

 后期Java的学习在很多方面也会使用接口 有一种说法是 面向接口编程 在工作中 会大量使用

 未完待续。。。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值