抽象类和接口

文章目录

前言

一、今日回顾

1.《高等数学》

2.阅读:

3.英语:

二、编程的那些事

1.引入库

2.读入数据

总结


前言


一、今日回顾

1.《高等数学》

2.阅读:

3.英语:

二、编程的那些事

1.抽象类的描述

在java中,一个没有方法体的方法应该被定义为抽象方法,而类中如果有抽象方法,该类必须定义为抽象类。

public abstract class Animal {

    public abstract void eat();
}

2.

//抽象类
public abstract class Animal {

    public abstract void eat();
 //抽象类里面可以有具体的方法体。//抽象类可以有具体的方法体,没有抽象方法,不过一般不那么做
    public void sleep(){
        System.out.println("睡觉");
    }

}
//Cat类继承Animal类,重写抽象方法

public class Cat extends Animal{
    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
}

//测试类
public class AnimalDemo {
    public static void main(String[] args) {
     Animal a =new Cat();
     a.eat();//eat方法再Animal里是抽象的,但在Cat类中被重写了
   //编译看左边Animal,执行看右边Cat
     a.sleep();
    }
}

总结

抽象类的特点

  • 抽象类和抽象方法必需使用abstract关键字修饰

   public abstract class类名{}

   public abstract void eat();

  • 抽象类中不一定有抽象方法,有抽象方法的类一定是抽象类
  • 抽象类不能被实例化,那抽象类如何被实例化呢?参照多态的方式,通过子类对象实例化,这叫抽象类多态。
  • 抽象类的子类

   要么重写抽象类中的所有抽象方法

   要么是抽象类


 案例2:

public abstract class Animal {

    private  int age=20;
    private final String city="北京";//city被final修饰,变为一常量
    public Animal(){

    }
    public Animal(int age){
       this.age=age;
    }
    public abstract void eat();

    public void show(){
        age=40;
        System.out.println(age);
        System.out.println(city);
    }

}
public class Cat extends Animal{
    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
}
public class AnimalDemo {
    public static void main(String[] args) {
     Animal a =new Cat();
     a.eat();//eat方法再Animal里是抽象的,但在Cat类中被重写了
   //编译看左边Animal,执行看右边Cat
   a.show();
    }
}

 

 小结:抽象类的成员特点

  • 成员变量

可以是变量,也可以是常量

  • 构造方法

有构造方法,但不能被实例化,那么,构造方法的作用是什么呢?用于子类访问父类数据的初始化

  • 成员方法

可以有抽象方法:限定子类必须完成某些动作

也可以有非抽象方法;提高代码的复用性

public abstract class Animal {


    private  String name;
    private  int age;
    public Animal(){

    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public abstract void eat();//不写方法体,用abstract
}
public class Cat extends Animal {
    public Cat() {
    }

    public Cat(String name, int age) {
        super(name, age);
    }

    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
}
public class Dog extends Animal{

    public Dog() {
    }

    public Dog(String name, int age) {
        super(name, age);
    }

    @Override
    public void eat() {
        System.out.println("狗吃骨头");
    }
}
public class AnimalDemo {
    public static void main(String[] args) {
        //创建对象,按照多态的形式
        Animal a=new Cat();
        a.setName("卡菲猫");
        a.setAge(5);
        System.out.println(a.getName()+","+a.getAge());
        a.eat();
        System.out.println("……");
        a=new Cat("加菲猫",5);
        System.out.println(a.getName()+","+a.getAge());
        a.eat();
        System.out.println("……");
        Animal dog=new Dog();
        dog.setAge(10);
        dog.setName("小防鼠");
        System.out.println(dog.getName()+","+dog.getAge());
        dog.eat();
        System.out.println("……");
        dog=new Dog("小防鼠",10);
        System.out.println(dog.getName()+","+dog.getAge());
        dog.eat();

    }
}

 接口

public interface Jumpping {
    void jump();
}
public class Cat implements Jumpping{
    public void jump(){
        System.out.println("猫可以调高了");
    }

}
public class JumpingDemo {
    public static void main(String[] args) {

        Jumpping j=new Cat();

        j.jump();
    }
}
public interface Inter {
    public int num=10;
    public final int num2=20;
}

 

public class InterImpl implements Inter{
    public int num=11;

}
public class InterfaceDemo {
    public static void main(String[] args) {
        Inter i=new InterImpl();
        System.out.println(i.num);
        System.out.println(Inter.num2);

    }
}

 

 

接口的成员特点

  • 成员变量

只能是常量

默认修饰符:public static final

  • 构造方法

接口没有构造方法,因为接口主要对行为进行抽象,是没有具体存在的。一个类如果没有父类,默认继承自Object类。

成员方法

只能是抽象方法

默认修饰符:public abstract

猫狗案例(接口版)

public interface Jumpping {
    void jump();
}

public abstract class Animal {
    private  String name;
    private  int age;

    public Animal() {
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public abstract void eat();
}
public class Cat extends Animal implements Jumpping {

    public Cat() {
    }

    public Cat(String name, int age) {
        super(name, age);
    }

    public void jump(){
        System.out.println("猫可以调高了");
    }

    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
}
public class AnimalDemo {
    public static void main(String[] args) {
        Jumpping j=new Cat();
        j.jump();
        Animal a=new Cat();
        a.setName("加菲猫");
        a.setAge(8);
        System.out.println(  a.getAge()+a.getName());
        a.eat();
        System.out.println("-----");

        a=new Cat("加菲",8);
        System.out.println(  a.getAge()+a.getName());
        a.eat();
        System.out.println("-----");

        Cat c=new Cat();
        c.setName("加菲猫");
        c.setAge(8);
        System.out.println(  c.getAge()+c.getName());
        c.eat();
        c.jump();
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天真小巫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值