• 2020.7.24


    学习内容:

    1、abstract关键字

    abstract是抽象的意思,使用abstract修饰类,叫做抽象类。抽象类 中既有抽象方法也有具体实现的方法。

    抽象类必须由其子类继承,才能实例化对象。

    abstract修饰方法叫做抽象方法,该方法只有方法头,没有方法体

    (1)抽象类

    定义:abstract class 类名()

    (2)抽象方法

    abstract修饰方法,表示该方法是抽象方法,但前提该类一定是抽象类。

    抽象方法只声明方法头,没有方法体

     如果想使用这些方法,必须有抽象类的子类来实现,例如:

     抽象类和抽象方法总结如下:

    1. 抽象类中既有抽象方法,也有非抽象方法
    2. 有抽象方法的类一定是抽象类
    3. 抽象类中不一定有抽象方法

    2、综合实例

    父类:选择题

    public class Question {
        String text; //题干
        String[] options; //选项
        public void print() {
            System.out.println(this.text);
            for(int i=0;i<this.options.length;i++) {
                System.out.print(options[i]+'\t');
            }
            System.out.println();
        }
        public boolean check(char[] answers) {
            return false;
        }
    }

    子类:单项选择题

    public class SingleQuestion extends Question{
        char answer;
        public SingleQuestion(String text,String[] options,char answer) {
            this.text=text;
            this.options=options;
            this.answer=answer;
        }
         public boolean check(char[] answers) {
             if(answers==null||answers.length!=1) {
                 return false;
             }
             return this.answer==answers[0];
         }
    }

    子类:多项选择题

    import java.util.Arrays;
    public class MultiQuestion extends Question {
        char[] answers;
        public MultiQuestion(String text,String[] options,char[] answers) {
            this.text=text;
            this.options=options;
            this.answers=answers;
        }
        public boolean check(char[] answers) {
            return Arrays.equals(answers,this.answers);
        }
    }

    测试类:

    import java.util.Scanner;
    public class PaperDemo {
        public static void main(String[] args) {
            Question[] paper= {null,null};
            paper[0]=new SingleQuestion("如何买火车票靠谱?",
                    new String[]{"A.电话","B.网上","C.黄牛","D.画的"},'A');
            paper[1]=new MultiQuestion("哪几位是歌手?",
                    new String[]{"A.刘德华","B.张学友","C.郭富城","D.孙悟空"},
                    new char[]{'A','B','C'});
            Scanner console=new Scanner(System.in);
            for(int i=0;i<paper.length;i++) {
                Question q=paper[i];
                q.print();
                System.out.print("请选择:");
                String str=console.nextLine();
                char[] answers=str.toCharArray();
                if(q.check(answers)) {
                    System.out.println("给力呀!");
                }else {
                    System.out.println("亲,要努力呀!");
                }
            }      
        }
    }

    遇到的问题:

    综合实例理解不清楚

    明天要学习的内容:

    第六章:接口浅议

  • 相关阅读:
    jq获取地址中的参数的值
    移动设备分辨率
    随机生成颜色
    小程序轮播图
    JavaScript中的constructor、instanceof、isPrototypeOf、typeof以及hasOwnProperty
    call()和apply()函数
    行内元素和块级元素
    js跳出循环的方法区别(break,continue,return)
    js 的四种设计模式的优缺点
    常见浏览器兼容性问题与解决方案
  • 原文地址:https://www.cnblogs.com/ltw222/p/13395960.html
Copyright © 2020-2023  润新知