zoukankan      html  css  js  c++  java
  • 抽象类

    抽象类

    由关键字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

  • 相关阅读:
    解决任务计划程序未启动任务,因为相同任务的实例正在运行的问题
    推荐.Net、C# 逆向反编译四大工具利器
    The Win32 Rundll and Rundll32 Interface Related Topics
    delphi版本修改PE头源码
    排序算法总结
    Delphi XE中String、ANSIString、TBytes之间的转换
    得到WAV文件的长度
    在VC中使用SendInput函数实现中文的自动输入
    SendInput模拟键盘输入的问题
    delphi XE的字符串处理
  • 原文地址:https://www.cnblogs.com/susexuexi011/p/13779508.html
Copyright © 2011-2022 走看看