Java学习笔记 - 第012天

每日要点

杂项

  • 模板方法模式
    模板方法模式(GoF设计模式)
    使用了JAVA的继承机制,在抽象类中定义一个模板方法,该方法引用了若干个抽象方法(由子类实现)或具体方法(子类可以覆盖重写)
  • 多态
    用同样类型的引用,调用相同的方法,做了不同的事情 - 多态

昨日作业讲解

  • 1.设计一个绘图系统
    图形:中心点坐标、颜色
    算周长、面积、画图
    支持画矩形、圆、等边三角形

    图形类:
/**
 * 图形(抽象类)
 * @author Kygo
 *
 */
public abstract class Shape {
    protected int centerX;
    protected int centerY;
    protected Color color;
    
    /**
     * 构造器
     * @param centerX 中心点的横坐标
     * @param centerY 中心点的纵坐标
     * @param color 颜色
     */
    public Shape(int centerX, int centerY, Color color) {
        this.centerX = centerX;
        this.centerY = centerY;
        this.color = color;
    }

    /**
     * 计算周长
     * @return 周长
     */
    public abstract double perimeter();
    
    /**
     * 计算面积
     * @return 面积
     */
    public abstract double area();
    
    /**
     * 绘图
     * @param g 画笔(绘图的上下文环境)
     */
    public void draw(Graphics g) {
        g.setColor(color);
        g.drawString(String.format("周长: %.2f", perimeter()), centerX, centerY);
        g.drawString(String.format("面积: %.2f", area()), centerX, centerY + 20);
    }
}

圆类:

/**
 * 圆
 * @author Kygo
 *
 */
public class Circle extends Shape {
    private int radius;
    
    public Circle(int centerX, int centerY, Color color, int radius) {
        super(centerX, centerY, color);
        this.radius = radius;
    }

    @Override
    public double perimeter() {
        return 2 * Math.PI * radius;
    }

    @Override
    public double area() {
        return Math.PI * radius * radius;
    }

    @Override
    public void draw(Graphics g) {
        super.draw(g);
        g.drawOval(centerX - radius, centerY - radius, 2 * radius, 2 * radius);
    }
}

矩形类:

public class Rectangle extends Shape{
    private int width;
    private int height;
    
    public Rectangle(int centerX, int centerY, Color color, int width, int height) {
        super(centerX, centerY, color);
        this.width = width;
        this.height = height;
    }

    @Override
    public double perimeter() {
        return 2 * (width + height);
    }

    @Override
    public double area() {
        return width * height;
    }

    @Override
    public void draw(Graphics g) {
        super.draw(g);
        g.drawRect(centerX - width / 2, centerY - height / 2, width, height);
    }
}

等边三角形类:

public class EquilateralTriangle extends Shape {
    private int length;
    
    public EquilateralTriangle(int centerX, int centerY, Color color, int length) {
        super(centerX, centerY, color);
        this.length = length;
    }

    @Override
    public double perimeter() {
        return 3 * length;
    }

    @Override
    public double area() {
        //return Math.sqrt(3) / 4 * length * length;
/*      double half = perimeter() / 2.0;
        return Math.sqrt(half * Math.pow(half - length, 3));*/
        return length * length * Math.cos(Math.PI / 6) / 2;
    }

    @Override
    public void draw(Graphics g) {
        super.draw(g);
        int x1 =centerX;
        int y1 =(int) (centerY - length / 2.0 / Math.cos(Math.PI / 6));
        int x2 = centerX - length / 2;
        int y2 = (int) (centerY + Math.tan(Math.PI / 6) * length / 2.0);
        int x3 = centerX + length / 2;
        int y3 = y2;
        g.setColor(color);
        g.drawLine(x1, y1, x2, y2);
        g.drawLine(x2, y2, x3, y3);
        g.drawLine(x3, y3, x1, y1);
    }
}

绘图系统类:

//设计一个绘图系统
//图形:
//中心点坐标、颜色
//算周长、面积、画图
//支持画矩形、圆、等边三角形
@SuppressWarnings("serial")
public class ShapeFrame extends JFrame {
    private Shape circle = new Circle(200, 200, Color.red, 100);
    private Shape rectangle = new Rectangle(300, 300, Color.BLUE, 300, 100);
    private Shape triangle = new EquilateralTriangle(400, 400, Color.GREEN, 200);
    
    public ShapeFrame() {
        this.setTitle("绘图");
        this.setSize(800, 600);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        circle.draw(g);
        rectangle.draw(g);
        triangle.draw(g);
    }
    
    public static void main(String[] args) {
        new ShapeFrame().setVisible(true);
    }
}

例子

  • 1.汽车租车系统
    小汽车
    普通 商务
    400 600/1天
    货车
    < 10T 1200
    < 20T 2000
    (>=) 20T 2500
    系统 选择租的车型 具体租哪个车 算租了多少天车 花多少钱

    交通工具类:
public abstract class Vehicle {
    protected String brand;
    
    public Vehicle(String brand) {
        this.brand = brand;
    }
    
    public String getBrand() {
        return brand;
    }
    
    public abstract int rentPerDay();
    
    public int calcRent(int days) {
        return days * rentPerDay();
    }
}

小汽车类:

public class Car extends Vehicle {
    private boolean normal;
    
    public Car(String brand, boolean normal) {
        super(brand);
        this.normal = normal;
    }

    @Override
    public int rentPerDay() {
        return normal ? 400 : 600;
    }
}

货车类:

public class Truck extends Vehicle {
    private double load;

    public Truck(String brand, double load) {
        super(brand);
        this.load = load;
    }

    @Override
    public int rentPerDay() {
        if (load < 10) {
            return 1200;
        }
        else if (load < 20) {
            return 2000;
        }
        else {
            return 2500;
        }
    }
}

测试类:

        Vehicle v1 = new Car("别克", false);
        System.out.println(v1.calcRent(5));
        
        Vehicle v2 =  new Truck("东方", 25);
        System.out.println(v2.calcRent(5));

2.国际象棋棋盘

@SuppressWarnings("serial")
public class ChessFrame extends JFrame {
    private Image knightImage;
    private Image rookImage;
    
    public ChessFrame() {
        this.setTitle("国际象棋");
        this.setSize(800, 700);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        try {
            knightImage = ImageIO.read(this.getClass().getClassLoader().getResource("22.png"));
            rookImage = ImageIO.read(this.getClass().getClassLoader().getResource("21.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                g.setColor((i + j) % 2 == 0 ? new Color(210, 220, 162) : new Color(179, 56, 89));
                g.fillRect(120 + j * 70, 80 + i * 70, 70, 70);
            } 
        }
        g.setColor(Color.RED);
        ((Graphics2D)g).setStroke(new BasicStroke(4));
        g.drawRect(115, 75, 570, 570);
        g.drawImage(knightImage, 190, 80, 70, 70, null);
        g.drawImage(rookImage, 120, 80, 70, 70, null);
    }
    
    public static void main(String[] args) {
        new ChessFrame().setVisible(true);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,319评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,801评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,567评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,156评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,019评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,090评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,500评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,192评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,474评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,566评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,338评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,212评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,572评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,890评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,169评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,478评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,661评论 2 335

推荐阅读更多精彩内容