博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
项目三(2)——抽象类
阅读量:6332 次
发布时间:2019-06-22

本文共 5013 字,大约阅读时间需要 16 分钟。

  1. 按照下面要求完成类的设计

(1)设计一个平面图形抽象类和一个立体图形抽象类,提供该类对象公共的方法和属性。

(2)修改项目三中第2题中所设计的球类、圆柱类,圆锥类、矩形类、三角形类、圆类,分别继承平面图形抽象类和立体图形抽象类。

(3)运行项目三中第2题中的测试方法,进行测试。

package pro4;public class Graphic03 {	public static void main(String[] args) {		ball ball01=new ball("red",3);		ball ball02=new ball("blue",4);		square square01=new square("red",3,4);		triangle triangle01=new triangle("blue",3,4,5);		testGraphic t1=new testGraphic(ball01,ball02);		testGraphic t2=new testGraphic(square01,triangle01);		t1.compare();		t2.printSumA();	}}class testGraphic{	private ball b1;	private ball b2;	private square s1;	private triangle t1;	public testGraphic(ball b1,ball b2){		this.b1=b1;		this.b2=b2;	}	public testGraphic(square s1,triangle t1){		this.s1=s1;		this.t1=t1;	}	public void compare(){		if(b1.graphicV()>b2.graphicV()){			System.out.println(b1.getColor()+"色的球体积"+b1.graphicV()+"大于"+b2.getColor()+"色的球的体积"+b2.graphicV());		}else{			System.out.println(b2.getColor()+"色的球体积"+b2.graphicV()+"大于"+b1.getColor()+"色的球的体积"+b1.graphicV());		}	}	public void printSumA(){		System.out.println(s1.getColor()+"色矩形和"+t1.getColor()+"色三角形面积的和:"+(s1.graphicA()+t1.graphicA()));	}}abstract class LiTi{	private String color;	public LiTi(String color){		this.setColor(color);	}	public void setColor(String color){		this.color=color;	}	public String getColor(){		return this.color;	}	public abstract double graphicA();	public abstract double graphicV();}class ball extends LiTi{		private double side01;	public ball(String color,double side01) {		super(color);		this.setSide01(side01);	}	public void setSide01(double side01){		this.side01=side01;	}	public double getSide01(){		return this.side01;	}	public double graphicA(){		return 4*3.14*side01*side01;	}	public double graphicV(){		double v=3.14*3/4*side01*side01*side01;		return v;	}	public void print(){		System.out.println("颜色是"+super.getColor());	}}class comuln extends LiTi{	private double side01;	private double side02;	public comuln(String color,double side01,double side02) {		super(color);		this.setSide01(side01);		this.setSide02(side02);	}	public void setSide01(double side01){		this.side01=side01;	}	public void setSide02(double side02){		this.side02=side02;	}	public double getSide01(){		return this.side01;	}	public double getSide02(){		return this.side02;	}	public double graphicA(){		return 2*3.14*side01*side01+2*3.14*side01*side02;	}	public double graphicV(){		return 3.14*side01*side01*side02;	}}class cone extends LiTi{	private double side01;	private double side02;	public cone(String color,double side01,double side02) {		super(color);		this.setSide01(side01);		this.setSide02(side02);	}	public void setSide01(double side01){		this.side01=side01;	}	public void setSide02(double side02){		this.side02=side02;	}	public double getSide01(){		return this.side01;	}	public double getSide02(){		return this.side02;	}	public double graphicA(){		return 3.14*side01*side01+3.14*side01*java.lang.Math.sqrt(side02*side02+side01*side01);	}	public double graphicV(){		return 3.14/3*side01*side01*side02;	}}abstract class PingMian{	private String color;	public PingMian(String color){		this.setColor(color);	}	public void setColor(String color){		this.color=color;	}	public String getColor(){		return this.color;	}	public abstract double graphicA();	public abstract double graphicL();}class square extends PingMian{	private double side01;	private double side02;	public square(String color,double side01,double side02) {		super(color);		this.setSide01(side01);		this.setSide02(side02);	}	public void setSide01(double side01){		this.side01=side01;	}	public void setSide02(double side02){		this.side02=side02;	}	public double getSide01(){		return this.side01;	}	public double getSide02(){		return this.side02;	}	public double graphicA(){		return side01*side02;	}	public double graphicL(){		return 2*(side01+side02);	}}class triangle extends PingMian{	private double side01;	private double side02;	private double side03;	public triangle(String color,double side01,double side02,double side03) {		super(color);		this.setSide01(side01);		this.setSide02(side02);		this.setSide03(side03);	}	public void setSide01(double side01){		this.side01=side01;	}	public void setSide02(double side02){		this.side02=side02;	}	public void setSide03(double side03){		this.side03=side03;	}	public double getSide01(){		return this.side01;	}	public double getSide02(){		return this.side02;	}	public double getSide03(){		return this.side03;	}	public double graphicA(){		return side01*(java.lang.Math.sqrt(java.lang.Math.abs(side03*side03-side01*side01))/2);	}	public double graphicL(){		return side01+side02+side03;	}}class circle extends PingMian{	private double side01;	public circle(String color,double side01) {		super(color);		this.setSide01(side01);	}	public void setSide01(double side01){		this.side01=side01;	}	public double getSide01(){		return this.side01;	}	public double graphicA(){		return 3.14*side01*side01;	}	public double graphicL(){		return 2*3.14*side01;	}}

  

转载于:https://www.cnblogs.com/hxtblogs/p/7649544.html

你可能感兴趣的文章
linux批量修改文件名大小写
查看>>
我的友情链接
查看>>
CSS预处理器-Sass
查看>>
mysql主主同步+Keepalived
查看>>
F5 负载均衡学习笔记----V9.x启动U盘制作方法
查看>>
如何学编程
查看>>
学习Linux决心书
查看>>
javascript中函数的参数与arguments关系
查看>>
MySql函数大全<->
查看>>
头像裁剪
查看>>
MySQL 自连接分组取每组最大N条记录
查看>>
通俗易懂理解 AI “深度学习”的基本原理:梯度下降
查看>>
大数据统计之基数估计(Cardinality Estimation)
查看>>
ubuntu 下mysql 5.6安装、删除和配置中文乱码问题
查看>>
我的友情链接
查看>>
Guava Files
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
苹果手机开源×××-StrongSwan【新任帮主】
查看>>
hadoop2.6 编译
查看>>