多态/抽象类

图形形状

package day0803;

 

import java.util.Random;

import java.util.Scanner;

 

public class Test1 {

    public static void main(String[] args) {

        System.out.println("按回车继续");

        while(true)

        {

            int i = new Random().nextInt(4);

            switch (i)

            {

                case 0: f(new Shape());break;

                case 1: f(new Line());break;

                case 2: f(new Square());break;

                case 3: f(new Circle());break;

            }

        }

    }

 

    private static void f(Shape o) {

        o.draw();

        //对line类型特殊处理

        if(o instanceof Line)

        {

            Line l = (Line) o;

            l.length();

        }

 

        new Scanner(System.in).nextLine();

        o.clear();

    }

}

 

package day0803;

 

public class Shape {

    public void draw()

    {

        System.out.println("图形形状");

    }

 

    public void clear()

    {

        System.out.println("\n\n\n\n\n\n\n\n");

    }

}

 

package day0803;

 

public class Line extends Shape {

 

    @Override

    public void draw()

    {

        System.out.println("-");

    }

 

    public void length()

    {

        System.out.println("线段的长度");

    }

}

 

package day0803;

 

public class Square extends Shape {

    @Override

    public void draw() {

        System.out.println("▲");

    }

}

 

package day0803;

 

public class Circle extends Shape {

    @Override

    public void draw() {

        System.out.println("O");

    }

}

什么是面向对象

封装/继承/多态

 

抽象类

没有写完的类,半成品类

抽象类不能创建对象

抽象方法:只有方法定义没有方法体

Abstract

 

抽象方法的作用:

作为通用方法,在父类中定义

要求具体子类,必须实现这个方法

 

猜游戏

已经随机产生了5个不重复的大写字符

猜这5个字母

package day0804;

 

import java.util.Scanner;

 

public class Test1 {

    public static void main(String[] args) {

        System.out.println("1.猜数字");

        System.out.println("2.猜字母");

        System.out.print("选择:");

 

        int i = new Scanner(System.in).nextInt();

        GuessGame game;

        if(i == 1) game = new GuessNumberGame();

        else game = new GuessLetterGame();

        game.start();

    }

}

 

package day0804;

 

import java.util.Scanner;

 

abstract public class GuessGame {

    public void start()

    {

        String r = suiji();

        tishi();

        while (true)

        {

            System.out.print("猜:");

            String s = new Scanner(System.in).nextLine();

            String result = bijiao(r,s);

            System.out.println(result);

            if(caidui(result)) break;

        }

    }

 

    public abstract String suiji();

    public abstract void tishi();

    public abstract String bijiao(String a,String b);

    public abstract boolean caidui(String result);

}

 

package day0804;

 

import java.util.Random;

 

public class GuessNumberGame extends GuessGame {

 

    @Override

    public String suiji() {

        int r = 1 + new Random().nextInt(1000);

        return String.valueOf(r);

    }

 

    @Override

    public void tishi() {

        System.out.println("已经随机产生了一个[1,1001)范围内的数字");

    }

 

    @Override

    public String bijiao(String a, String b) {

        //字符串解析为int

        int intA = Integer.parseInt(a);

        int intB = Integer.parseInt(b);

        if(intA>intB) return "小";

        else if(intA < intB) return "大";

        else return "对";

 

    }

 

    @Override

    public boolean caidui(String result) {

        return result.equals("对");

    }

}

 

package day0804;

 

public class GuessLetterGame extends GuessGame {

    @Override

    public String suiji() {

        return null;

    }

 

    @Override

    public void tishi() {

        System.out.println("已经产生了5个不同的大写字母");

    }

 

    @Override

    public String bijiao(String a, String b) {

        return null;

    }

 

    @Override

    public boolean caidui(String result) {

        return result.equals("5A0B");

    }

}