面向对象练习题

父类

package com.lovoinfo.ui;

import java.awt.Color;
import java.awt.Graphics;

public abstract class Shape {
    protected int x1;//起点横坐标
    protected int y1;//起点纵坐标
    protected int x2;//终点横坐标
    protected int y2;//重点纵坐标

    protected Color color;


    public void setColor(Color color) {
        this.color = color;
    }

    public abstract void draw(Graphics g);

    public int getX1() {
        return x1;
    }

    public void setX1(int x1) {
        this.x1 = x1;
    }

    public int getY1() {
        return y1;
    }

    public void setY1(int y1) {
        this.y1 = y1;
    }

    public int getX2() {
        return x2;
    }

    public void setX2(int x2) {
        this.x2 = x2;
    }

    public int getY2() {
        return y2;
    }

    public void setY2(int y2) {
        this.y2 = y2;
    }

}

子类线

package com.lovoinfo.ui;

import java.awt.Graphics;
/**
 * 线
 * @author 
 *
 */
public class Line extends Shape {

    @Override
    public void draw(Graphics g) {
        g.setColor(color);
        g.drawLine(x1, y1, x2, y2);


    }

}

子类圆形

package com.lovoinfo.ui;

import java.awt.Graphics;
/**
 * Բ圆
 * @author 
 *
 */
public class Oval extends Shape {

    @Override
    public void draw(Graphics g) {
        int width = Math.abs(x2 - x1);
        int height = Math.abs(y2 - y1);

        int x = x1 < x2 ? x1 : x2;
        int y = y1 < y2 ? y1 : y2;
        g.setColor(color);
        g.drawOval(x, y, width, height);

    }

}

子类矩形

package com.lovoinfo.ui;

import java.awt.Graphics;
/**
 * 矩形
 * @author 
 *
 */
public class Rectangle extends Shape {

    @Override
    public void draw(Graphics g) {
        int width = Math.abs(x2 - x1);
        int height = Math.abs(y2 - y1);

        int x = x1 < x2 ? x1 : x2;
        int y = y1 < y2 ? y1 : y2;
        g.setColor(color);
        g.drawRect(x, y, width, height);

    }

}

创建一个窗口

package com.lovoinfo.ui;

import java.awt.Color;
import java.awt.Graphics;
import java.lang.annotation.Retention;

import javax.swing.JFrame;

import com.lovoinfo.util.MyUtil;

@SuppressWarnings("serial")
public class MainFrame extends JFrame {

    private Shape[] shapes = new Shape[100];

    public MainFrame() {
        this.setTitle("绘图窗口");
        this.setSize(800, 600);// 窗口的大小
        this.setResizable(false);// 我不想别人移动窗口
        this.setLocationRelativeTo(null);// 窗口剧中
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);// 在退出窗口时,同时关闭控制台

        for (int i = 0; i < shapes.length; i++) {
            switch (MyUtil.random(1, 3)) {
            case 1:
                shapes[i] = new Line();
                break;// 用用封装的随机数来取图形输出次数
            case 2:
                shapes[i] = new Oval();
                break;
            case 3:
                shapes[i] = new Rectangle();
                break;
            }

            shapes[i].setX1(MyUtil.random(0, 800));// 用封装的随机数来取N个随机坐标
            shapes[i].setY1(MyUtil.random(0, 600));
            shapes[i].setX2(MyUtil.random(0, 500));
            shapes[i].setY2(MyUtil.random(0, 400));

            shapes[i].setColor(MyUtil.randomColor());// 用封装的随机颜色来获得N个随机颜色
        }

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        for (int i = 0; i < shapes.length; i++) {
            shapes[i].draw(g);
        }
    }

}

建立一个对象##

package com.lovoinfo.ui;

public class MainFrameTest {
    public static void main(String[] args) {
        // 匿名对象
        new MainFrame().setVisible(true);
    }

}

———————————————

       public abstract class ~~~~~~~~~~
       这是一个抽象类不能创建对想,专门用来让别的类来继承它。
       public abstract void XXX();
       一个如果有抽象方法这个类必须被声明为抽象类。

抽象—封装—继承—多态

继承:子类继承父类属性和行动。
多态:

1.重写:在继承方法过程中重写。
2.造型:对象造型,(同样的引用同样的方法,却做了不做的事情)。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值