每日要点
杂项
-
模板方法模式
模板方法模式(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);
}
}