2015-6-13

来源:互联网 发布:爱淘宝红包 1元 0.5元 编辑:程序博客网 时间:2024/06/08 10:17
经过几天讲解和练习,实现了继承的实例,
在此过程中,感觉主要难度在于子类的继承对象以及强制转换,
例:
import java.awt.Color;
import java.awt.Graphics;
public class Line extends Shape{
public Line(int x1, int y1, int x2, int y2, Color color) {
super(x1, y1, x2, y2, color);
// TODO Auto-generated constructor stub}
public void draw(Graphics g) {
g.drawLine(x1, y1, x2, y2);
}

}
  



if ("10".equals(draw.str)) {
// draw.g.drawLine(x1, y1, x2, y2);
// 创建一个直线对象
shape = new Line(x1,y1,x2,y2,draw.color);
x1 = x2;
y1 = y2;
再如:
} else if ("2".equals(draw.str)) {

draw.g.setColor(Color.white);
BasicStroke basicstroke = new BasicStroke(10);
Shape shape = new Coph(x1,y1,x2,y2,draw.color.white);
Graphics2D graphics2d = (Graphics2D) draw.g;
graphics2d.setStroke(basicstroke);
draw.g.drawLine(x1, y1, x2, y2);
list.add(shape);
x1 = x2;
y1 = y2;
总之,强制转化得新建一对象,毕竟这是子类的属性。



0 0