【Java】图形类的多态与接口

父类Element

package zuoye;

public abstract class Element {
	public abstract void displayElement();  //显示元素的信息
    //利用多态实现,输出组成图形的各种属性
	
}

子类 点、线、圆形和矩形;

package zuoye;

public class Point extends Element{
    private double x;
    private double y;  //x和y坐标作为点的属性

    Point(int x,int y){
        this.x=x;
        this.y=y;
    }

    public void setXY(double x,double y) {
        this.x = x;
        this.y = y;
    }

    public Point() {
    }

    @Override
    public void displayElement() {
        System.out.println("该点的坐标为:("+x+" ,"+y+")");
    }
}
package zuoye;

public class Line extends Element {
	private double length;
	
	Line(double length){
		this.length = length;
	}
	
	public Line() {
    }

    public void setLength(double length) {
        this.length = length;
    }

    public double getPerimeter() {
        return length;
    }

    @Override
    public void displayElement() {
        System.out.println("该线段的长度为:"+length);
    }
	
package zuoye;

public class Circle extends Element implements Get {	//必须实现接口里的全部方法
	private double r;	//private 为了防止类中的数据成员 在类的定义之外被修改
	
	public Circle(double r) {
		this.r = r;
	}
	
	 public Circle() {
	}
	
	public double getPeri() {
		return 2*Math.PI*this.r;
	}
	
	public double getArea() {
		return Math.PI*Math.pow(r, 2);
	}

	public double getR() {
		return r;
	}

	public void setR(double r) {
		this.r = r;
	}
	
	@Override
    public void displayElement() {
        System.out.println("该圆的面积为:"+this.getArea()+"   该圆的周长为:"+this.getPeri());
    }
}
package zuoye;

public class Rectangle extends Element implements Get{
	 	private double length;
	    private double width;

	    public Rectangle(double length, double width) {
	        this.length = length;
	        this.width = width;
	    }

	    public void setLength(double length,double width) {
	        this.length = length;
	        this.width = width;
	    }


	    public Rectangle() {
	    }

	    public double getLength() {
	        return length;
	    }
	    public double getWidth() {
	        return width;
	    }

	    @Override
	    public double getArea() {
	        return length*width;
	    }

	    @Override
	    public double getPeri() {
	        return 2*(length+width);
	    }

	    @Override
	    public void displayElement() {
	        System.out.println("该矩形的面积:"+this.getArea()+"   该矩形的周长为:"+getPeri());
	    }
}

圆形和矩形的面积、周长接口

package zuoye;

public interface Get {
	public abstract double getArea();
	public abstract double getPeri();
}

main

package zuoye;

import java.io.*;
import java.sql.SQLOutput;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo {
    public static void file_in() {   //文件输入
        /*
        * 1 使用File类打开一个文件
          2 通过字节流或字符流的子类,指定输出的位置
          3 进行读/写操作
          4 关闭输入/输出
        * */


        Circle circle=new Circle();
        Point point=new Point();
        Line line=new Line();
        Rectangle rectangle=new Rectangle();
        try {
            File file = new File("graph_information.txt");
            BufferedReader bf = new BufferedReader(new FileReader(file));//filename为参数,文件路径;bf不是指针,用于字符缓冲,文件输入的一种方式
            String textLine = new String();
            String str = new String();
            while ((textLine = bf.readLine()) != null) {//readline每次读取一行,也是最推荐的读取文件的方法
                str += textLine + ",";
            }

            String[] numbers = str.split(",");	//将str这个字符串用','切割后分别放到numbers数组中
            String[] circle_inf = numbers[0].split(" ");	//如"a b  c         ",切割后有四位,a,b, ,c(若干最后n位都是切割符,split(" ")不会继续切分)
            String[] point_inf=numbers[2].split(" ");
            String[] line_inf=numbers[1].split(" ");
            String[] rectangle_inf=numbers[3].split(" ");
            
            double r= Double.parseDouble(circle_inf[0]);	//将字符串强制转化成double型
            circle.setR(r);    

            double length=0;
            length= Double.parseDouble(line_inf[0]);
            line.setLength(length);

            double x,y;
            x=Double.parseDouble(point_inf[0]);
            y=Double.parseDouble(point_inf[1]);
            point.setXY(x,y);

           double rec_len,rec_wid;
            rec_len=Double.parseDouble(rectangle_inf[0]);
            rec_wid=Double.parseDouble(rectangle_inf[1]);
            rectangle.setLength(rec_len,rec_wid);

            point.displayElement();
            circle.displayElement();
            line.displayElement();
            rectangle.displayElement();
        }catch (IOException e){
            e.printStackTrace();	//在命令行打印异常信息在程序中出错的位置及原因
        }

    }

    public static void console_in(){   //控制台输入
        Circle circle=new Circle();
        Point point=new Point();
        Line line=new Line();
        Rectangle rectangle=new Rectangle();
        Scanner scan = new Scanner(System.in);
        
        System.out.println("请输入线段长度: ");
        try {
        	
        	String next = scan.next();
            while (!isDouble(next)) {
                System.out.print("您输入的不是double类型,请重新输入:");
                next=scan.next();
            }
        	
            double line_len = Double.parseDouble(next);
            line.setLength(line_len);
            line.displayElement();

            System.out.println("请输入圆半径: ");
            
            next = scan.next();
            while (!isDouble(next)) {
                System.out.print("您输入的不是double类型,请重新输入:");
                next=scan.next();
            }
            
            double cir_r = Double.parseDouble(next);
            circle.setR(cir_r);
            circle.displayElement();

            System.out.println("请输入矩形的长和宽: ");
            
            next = scan.next();
            while (!isDouble(next)) {
                System.out.print("您输入的不是double类型,请重新输入:");
                next=scan.next();
            }
            
            double rec_len=Double.parseDouble(next);
            
            next = scan.next();
            while (!isDouble(next)) {
                System.out.print("您输入的不是double类型,请重新输入:");
                next=scan.next();
            }
            
            double rec_wid=Double.parseDouble(next);
            rectangle.setLength(rec_len,rec_wid);
            rectangle.displayElement();

            System.out.println("请输入点的x,y坐标: ");
            
            next = scan.next();
            while (!isDouble(next)) {
                System.out.print("您输入的不是double类型,请重新输入:");
                next=scan.next();
            }
            
            double point_x=Double.parseDouble(next);
            
            next = scan.next();
            while (!isDouble(next)) {
                System.out.print("您输入的不是double类型,请重新输入:");
                next=scan.next();
            }
            
            double point_y=Double.parseDouble(next);
            point.setXY(point_x,point_y);
            point.displayElement();
        }catch (InputMismatchException e){	//InputMismatchException  输入类型不匹配异常
            System.out.println(e.getMessage());	//e.getMessage() 获得具体的异常名称 这个异常对象中没有维护异常的原因 所以通过getMessage获取不到异常信息 null值
            System.err.println("请输入浮点型数据");	//System.err.println 标准错误输出流
        }
    }
    
    public static boolean isDouble(String str) {
        try{
            Double.parseDouble(str);
            return true;
        }
        catch(NumberFormatException ex){}
        return false;
    }

    public static void main(String[] args) {
    	while(true) {
    		 Scanner scan=new Scanner(System.in);
    		 System.out.println("您要用哪种方式来进行输入");
    		 System.out.println("1. 控制台输入");
    		 System.out.println("2. 文件输入");
    		 System.out.println("3. 退出");
    		 System.out.print(": ");
    		 int cho=scan.nextInt();
    		 if(cho==1){
    			 console_in();
    		 }
    		 else if(cho==2) {
    			 file_in();
    		 }
    		 else if(cho==3) {
    			 break;
    		 }
    		 else{
    			 System.out.println("请输入1-3的数字,请重新输入");
    		 }
    	} 
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值