标签:except 内容 shape cep 实验代码 引用 stat class 一个
abstract class Door{
void open(){}
void close(){}
}
interface Door{
void open();
void close();
}
abstract class Door{
void open(){}
void close(){}
void bilibili(){}
}
interface Door{
void open();
void close();
void bilibill();
}
interface Ring{
void bilibill();
}
abstract class Door{
void open(){}
void close(){]
}
class DoorWithRing extends Door implements Ring{
public void bilibili(){
}
}
class DoorWithRing extends Door{
}
interface CatEye{
void see();
}
class DoorWithRingAndCatEye extends Door implemens Ring,CatEye{
public void see(){
}
public void bilibili(){
}
}
try{
//有可能出现异常的语句
}catch(异常类 异常对象){
//编写异常的处理语句
}[catch(异常类 异常对象){
//编写异常的处理语句
}catch(异常类 异常处理对象){
//编写异常的处理语句
}...]
[finally{
一定会运行到的实验代码;
}]
设计一个类层次,定义一个抽象类--形状,其中包括有求形状的面积的抽象方法。 继承该抽象类定义三角型、矩形、圆。 分别创建一个三角形、矩形、圆存对象,将各类图形的面积输出。
注:三角形面积s=sqrt(p(p-a)(p-b)*(p-c)) 其中,a,b,c为三条边,p=(a+b+c)/2
package Test9;
abstract class Form {
abstract public double Area();
}
class Triangle extends Form {
private double a;
private double b;
private double c;
public Triangle(double a,double b,double c) {
this.a=a;
this.b=b;
this.c=c;
}
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public double getC() {
return c;
}
public void setC(double c) {
this.c = c;
}
public double Area() {
double p=(a+b+c)/2;
return Math.sqrt(p*(p-a)*(p-b)*(p-c));
}
}
class Ractangle extends Form {
private double height;
private double width;
public Ractangle(double height,double width) {
this.height=height;
this.width=width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double Area() {
return height*width;
}
}
class Circle extends Form {
private double r;
public Circle(double r) {
this.r=r;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public double Area() {
return Math.PI*r*r;
}
}
package Test9;
public class Test {
public static void main(String[] args) {
Form tri=new Triangle(7,8,9);
Form rac=new Ractangle(9,8);
Form cir=new Circle(8);
System.out.println("三角形的面积:"+tri.Area());
System.out.println("长方形的面积:"+rac.Area());
System.out.println("圆形的面积:"+cir.Area());
}
}
编程技巧
package WH;
interface Shape {
public double getSize();
}
class Circle implements Shape {
private double r;
public Circle(double r) {
this.r=r;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public double getSize() {
return Math.PI*r*r;
}
}
class Line implements Shape {
private double x1,y1,x2,y2;
public Line(double x1,double y1,double x2,double y2) {
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
}
public double getX1() {
return x1;
}
public void setX1(double x1) {
this.x1 = x1;
}
public double getY1() {
return y1;
}
public void setY1(double y1) {
this.y1 = y1;
}
public double getX2() {
return x2;
}
public void setX2(double x2) {
this.x2 = x2;
}
public double getY2() {
return y2;
}
public void setY2(double y2) {
this.y2 = y2;
}
public double getSize() {
return Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
}
package WH;
public class Test {
public static void main(String[] args) {
Shape cr=new Circle(8);
Shape li=new Line(7,8,9,10);
System.out.println("圆的面积:"+cr.getSize());
System.out.println("直线长度:"+li.getSize());
}
}
标签:except 内容 shape cep 实验代码 引用 stat class 一个
原文地址:https://www.cnblogs.com/buxiu888/p/11657231.html