• 抽象类


    抽象类

    由关键字abstract 声明;

    使用

     

    1.不能直接创建一个抽象类对象;

    2.必须用子类继承一个抽象父类;

    3.子类必须覆盖重写所有的抽象方法

    4.创建子类对象进行使用;

     

    注意事项

    1.抽象类中可以有构造方法是供子类创建对象时使用初始化父类使用的;

    (子类构造方法中有默认的super())

    2.抽象类中不一定含有抽象方法,但是含有抽象方法的类必须是抽象类

    3.子类必须覆盖重写所有的抽象方法若没有,则子类应该是一个抽象类

     

    例子

     

    抽象类,其中有两个抽象方法和一个构造方法;

     public abstract class Student {
         public  Student (){
                 System.out.println("抽象父类构造方法!");
        };
     
         public   abstract  void study();
         public  abstract double three_avescore(double chinese,double math,double english);

    Student02类继承Student类,但是仅覆盖重写一个抽象方法,所以本类也必须是一个抽象类

     //
     public abstract  class Student02 extends Student{
         @Override
         public double three_avescore(double chinese,double math,double english){
     
             double avescore = 0;
             avescore = (chinese+math+english)/3;
             return avescore;
        }
     }

    创建一个Primary继承Student02类,间接继承Student类,因为Student02 已经覆盖重写Student类的一个抽象方法,所以只需重写另一个即可。

    (若直接继承Student类则需覆盖重写Student类中所有抽象方法)

     public class Primary extends Student02 {
     
         @Override
         public void study() {
             System.out.println("学习基础的语文、数学、英语");
        }
     
         /*@Override
         public double three_avescore(double chinese,double math,double english){
     
             double avescore = 0;
             avescore = (chinese+math+english)/3;
             return avescore;
         }*/
     
         public  Primary(){
     
        }
     }
     
     //与Primary类似,新增加一个特有方法
     
     public class Middle extends Student02 {
         @Override
         public void study(){
             System.out.println("学习进阶的语文、数学、英语以及物理、化学、生物课程");
        }
     
        /* @Override
         public double three_avescore(double chinese, double math, double english) {
             double avescore = 0;
             avescore = (chinese+math+english)/3;
             return avescore;
         }*/
     
         public double other_avescore(double a,double b,double c){
             double avescore = 0;
             avescore = (a+b+c)/3;
             return avescore;
        }
     
     
     }

    进行测试:

     public static void main (String[] args){
     
             Primary ls = new Primary();
       //创建子类对象使用(同时执行了抽象父类构造方法)
             System.out.println("==========");
             ls.study();
             double score=ls.three_avescore(88,89,76);
         //通过子类对象调用方法
            System.out.println(String.format("%.2f",score));
     
             System.out.println("==========");
     
             Middle ls2= new Middle();
             System.out.println("==========");
             ls2.study();
             double score01=ls2.three_avescore(75,64,89);
             double score02= ls2.other_avescore(66,94,56);
         //调用自己特有的方法
             System.out.println(String.format("%.2f",score01));
             System.out.println(String.format("%.2f",score02));
        }
     }

    输出结果:

    抽象父类构造方法!

    学习基础的语文、数学、英语

    84.33

    抽象父类构造方法!

    学习进阶的语文、数学、英语以及物理、化学、生物课程 76.00 72.00

  • 相关阅读:
    selenium+phantomjs爬取bilibili
    使用 python 开发 Web Service
    OBIEE 立方刷新的问题
    解析OracleOLAP使用MView刷新Cube
    Codeforces Round #755 (Div. 2, based on Technocup 2022 Elimination Round 2)(CF1589)题解
    Codeforces Round #754 (Div. 2)(CF1605)题解
    完美解读Linux中文件系统的目录结构
    C#中获取程序当前路径的集中方法
    30个优秀.net在线学习资源站点
    如何删除windows service(转帖)
  • 原文地址:https://www.cnblogs.com/susexuexi011/p/13779508.html
Copyright © 2020-2023  润新知