zoukankan      html  css  js  c++  java
  • 继承和接口

    几何图形类的构造

    import java.awt.*;
    import java.applet.*;
    
    abstract class Shape {
        //定义图形类,此类只是一般的图形总类,定义为抽象类,只是规定图形的一般格式
        int x,y;//图形左上角坐标
        Color c;//绘图颜色
        Graphics g;//图形对象
        Shape(){}//无参构造函数
        Shape(int x,int y,Color c,Graphics g){//有参构造函数
            this.x=x;this.y=y;this.c=c;this.g=g;
        }
        void setValue(int x,int y,Color c,Graphics g){//给成员数据赋值
            this.x=x;this.y=y;this.c=c;this.g=g;
        }
        abstract double area();//求面积方法
        abstract void draw();//绘图方法
    }
    //定义正方形类,作为图形的子类
    class Square extends Shape{
        int a;
        Square(){}
        Square(int x,int y,Color c,Graphics g){
            super(x,y,c,g);this.a=a;
        }
        void setValue(int x,int y,Color c,Graphics g){
            super.setValue(x, y, c, g);this.a=a;
        }
        double area(){//实现求面积的方法
            return a*a;
        }
        void draw(){//实现画图的方法
            g.setColor(c);//设置色彩
            g.fillRect(x, y, a, a);
        }
    }
    //定义矩形类,作为正方形的子类
    class Rectangle extends Square{
        int b;
        Rectangle(){}
        Rectangle(int x,int y,int a,int b,Color c,Graphics g){
            super(x,y,c,g);this.b=b;
        }
        void setValue(int x,int y,int a,int b,Color c,Graphics g){
            super.setValue(x, y, c, g);this.b=b;
        }
        double area(){//实现求面积的方法
            return a*b;
        }
        void draw(){//实现画图的方法
            g.setColor(c);//设置色彩
            g.fillRect(x, y, a, b);
        }
    }
    //定义三角形类,作为图形的子类
    class Triangle extends Shape{
        int a[]={210,260,160};
        int b[]={20,70,70};
        Triangle(){}
        Triangle(int x,int y,Color c,Graphics g){
            super(x,y,c,g);
        }
        double area(){//实现求面积的方法
            double p,x,y,z;
            x=Math.sqrt(Math.pow(b[1]-b[0], 2)+Math.pow(a[1]-a[0], 2));
            y=x=Math.sqrt(Math.pow(b[2]-b[1], 2)+Math.pow(a[2]-a[1], 2));
            z=Math.sqrt(Math.pow(b[0]-b[2], 2)+Math.pow(a[0]-a[2], 2));
            p=(x+y+z)/2;
            return Math.sqrt(p*(p-x)*(p-y)*(p-z));
        }
        void draw(){//实现画图的方法
            g.setColor(c);//设置色彩
            g.fillPolygon(a,b,3);
        }
    }
    //定义圆类,作为图形的子类
    class Circle extends Shape{
        int r;
        Circle(){}
        Circle(int x,int y,Color c,Graphics g){
            super(x,y,c,g);this.r=r;
        }
        void setValue(int x,int y,Color c,Graphics g){
            super.setValue(x, y, c, g);this.r=r;
        }
        double area(){//实现求面积的方法
            return (int)(3.1416*r*r);
        }
        void draw(){//实现画图的方法
            g.setColor(c);//设置色彩
            g.fillOval(x, y,2*r, 2*r);
        }
    }
    //定义椭圆类,作为图形的子类
    class Oval extends Circle{
        int rr;
        Oval(){}
        Oval(int x,int y,int r,int rr,Color c,Graphics g){
            super(x,y,c,g);this.rr=rr;
        }
        void setValue(int x,int y,int r,int rr,Color c,Graphics g){
            super.setValue(x, y, c, g);this.rr=rr;
        }
        double area(){//实现求面积的方法
            return (int)(3.1416*r*rr);
        }
        void draw(){//实现画图的方法
            g.setColor(c);//设置色彩
            g.fillOval(x, y,2*r, 2*rr);
        }
    }
    public class ShapeDemo extends Applet{
        public void paint(Graphics g){
            Square a=new Square();
            a.setValue(20,20, Color.blue, g);
            a.draw();
            g.drawString("area of a="+a.area(),20,130);
            Triangle b=new Triangle(160,20,Color.red,g);
            b.draw();
            g.drawString("area of b="+b.area(),160,80);
            Square m=new Rectangle(160,100,100,150,Color.black,g);
            m.draw();
            g.drawString("area of c="+m.area(),160,260);
            Circle d=new Circle(20,150,Color.magenta,g);
            d.draw();
            g.drawString("area of d="+d.area(),20,260);
            Oval e=new Oval(300,20,50,115,Color.pink,g);
            e.draw();
            g.drawString("area of e="+e.area(),300,260);
        }
    }
  • 相关阅读:
    架构探险笔记3-搭建轻量级Java web框架
    软件工程
    Mysql基础
    Navicat快捷键
    百度搜索的使用技巧
    利用adb截图然后传到电脑
    Div不用float布局
    安卓开发之ScrollView
    安卓开发ScrollView嵌套ListView只显示一行
    修改eclipse的背景色(转载)
  • 原文地址:https://www.cnblogs.com/ljs-666/p/7828341.html
Copyright © 2011-2022 走看看