zoukankan      html  css  js  c++  java
  • java 接口实现多态

    package unit4;
    import java.awt.Graphics;
    
    public interface Shape {
        void drowme(Graphics g);
        double area();
        double length();
        String getName();
    
    }
    package unit4;
    
    import java.awt.Graphics;
    
    public class Point implements Shape{
        int x,y;
        Point (int x,int y){this.x=x;this.y=y;}
        
        public double area() {
            // TODO Auto-generated method stub
            return 0;
        }
        public void drowme(Graphics g) {
            g.fillOval(x,y,5,5);
            
        }
        public String getName() {
            // TODO Auto-generated method stub
            return "Point";
        }
        public double length() {
            // TODO Auto-generated method stub
            return 0;
        }
    }
        
    package unit4;
    
    import java.awt.Graphics;
    
    public class Triangle implements Shape {
        Point a,b,c;
        public Triangle(Point aa,Point bb,Point cc) {
            // TODO Auto-generated constructor stub
            a=aa;
            b=bb;
            c=cc;
            
        }
    
        public double area() {
            
            double a_b=Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
            double a_c=Math.sqrt((a.x-c.x)*(a.x-c.x)+(a.y-c.y)*(a.y-c.y));
            double b_c=Math.sqrt((b.x-c.x)*(b.x-c.x)+(b.y-c.y)*(b.y-c.y));
            double l=(a_b+a_c+b_c)/2;
            double s=Math.sqrt((l-a_b)*(l-b_c)*(l-a_c)*l);
            return s;
        }
    
        public void drowme(Graphics g) {
            g.drawLine(a.x,a.y,b.x,b.y);
            g.drawLine(a.x,a.y,c.x,c.y);
            g.drawLine(b.x,b.y,c.x,c.y);
            
        }
    
        public String getName() {
            return "triangle";
        }
    
        public double length() {
            double a_b=Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
            double a_c=Math.sqrt((a.x-c.x)*(a.x-c.x)+(a.y-c.y)*(a.y-c.y));
            double b_c=Math.sqrt((b.x-c.x)*(b.x-c.x)+(b.y-c.y)*(b.y-c.y));
            double l=(a_b+a_c+b_c);
            return l;
        }
        
    
    }
    package unit4;
    
    import java.awt.Graphics;
    
    public class Circl implements Shape {
    
        Point c;
        int r;
        public Circl(Point cc,int rr) {
            c=cc;
            r=rr;
        }
        public double area() {
            return 3.14159*r*r;
        }
    
        public void drowme(Graphics g) {
            g.drawOval(c.x-r,c.y-r,2*r,2*r);
            
        }
    
        public String getName() {
            return "circle";
        }
    
        public double length() {
            return 2*3.14159*r;
        }
        
    
    }
    package unit4;
    
    import java.awt.Graphics;
    
    public class Rect implements Shape{
        Point a,b;
        Rect(Point aa,Point bb){a=aa;b=bb;}
    
        public double area() {
            
            return (a.x-b.x)*(a.y-b.y);
        }
    
        public void drowme(Graphics g) {
            g.drawRect(a.x,a.y,b.x-a.x,b.y-a.y);
            
        }
    
        public String getName() {
            return "rectangle";
        }
    
        public double length() {
            return (b.x-a.x)*2+(b.y-a.y)*2;
        }
        
    
    }
    package unit4;
    
    import java.applet.Applet;
    import java.awt.Graphics;
    
    public class Shapetest extends Applet{
         Shape[]myshapes=new Shape[5];
        public  void init() {
            
             Point a0=new Point(50,50);
             Point a1 = new Point(24,24);
             Point a2=new Point(100,200);
             Point a3 = new Point(200,120);
             myshapes[0]=a0;
             myshapes[1]=new Triangle(a1,a2,a3);
             myshapes[2]=new Circl(a2,50);
             myshapes[3]=new Circl(a3,100);
             myshapes[4]=new Rect(new Point(100,100),new Point(200,200));
            
            
        }
        public void paint(Graphics g){
            for(int i=0;i<myshapes.length;i++){
                myshapes[i].drowme(g);
                System.out.println(myshapes[i].getName()+": area: "+myshapes[i].area()+", length: "+myshapes[i].length());
            }
        }
    
    }

    这里可以直接在eclipse上运行位applet,但是,我发现一个严重的问题,我没法部署网站,哎,先不管了,以后用到了在慢慢调吧。

    每次都要费老大劲配置。我日。。

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <applet code="shapetest.class" width=400 height=400>
        </applet>
    
    </body>
    </html>

    理论上,配置成功可以在网页上启动小程序的。

  • 相关阅读:
    iot 表索引dump《2》
    heap表和iot表排序规则不同
    Cannot complete the install because one or more required items could not be found.
    iot表输出按主键列排序,heap表不是
    iot 表主键存放所有数据,且按数据插入顺序排序
    iot表和heap表排序规则不同
    org.eclipse.graphiti.ui.editor.DiagramEditorInput.
    Oracle 排序规则
    perl 异步超时 打印错误
    14.6.3 Grouping DML Operations with Transactions 组DML操作
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5713145.html
Copyright © 2011-2022 走看看