3 回答
TA贡献1831条经验 获得超4个赞
package com.shape;
public interface Volume {
double PI = 3.1415926;
//计算体积 public double calVolume();
//计算表面积 public double calArea();
} package com.shape;
//圆柱体 public class Cylinder implements Volume {
private double Radius ; private double Height;
public Cylinder(double Radius, double Height) {
this.Radius = Radius; this.Height = Height; }
public double getRadius() { return Radius; }
public void setRadius(double radius) { Radius = radius; }
public double getHeight() { return Height; }
public void setHeight(double height) { Height = height; }
@Override public double calVolume() { // TODO Auto-generated method stub return PI * Radius * Radius * Height; }
@Override public double calArea() { // TODO Auto-generated method stub return PI * Radius * Radius * 2 + PI * Radius * Height * 2; }
} package com.shape;
//圆锥 public class Cone implements Volume {
private double Radius; private double Height;
public double getRadius() { return Radius; } public void setRadius(double radius) { Radius = radius; } public double getHeight() { return Height; } public void setHeight(double height) { Height = height; }
public Cone(double Radius, double Height) { this.Radius = Radius; this.Height = Height; }
//圆锥体的母线的长 public double getConeLine() {
double Line = Radius * Radius + Height * Height; return Math.sqrt(Line); }
@Override public double calVolume() { // TODO Auto-generated method stub return PI * Radius * Radius * Height / 3; }
@Override public double calArea() { // TODO Auto-generated method stub return PI * Radius * Radius + PI * Radius * getConeLine(); }
} public class ShapeTestDemo {
public static void main(String[] args) { // TODO Auto-generated method stub
Cylinder cy = new Cylinder(3, 4); System.out.println(cy.calVolume()); System.out.println(cy.calArea());
Cone cone = new Cone(3, 4); System.out.println(cone.calVolume()); System.out.println(cone.getConeLine()); System.out.println(cone.calArea());
}
} |
TA贡献1853条经验 获得超6个赞
package temp;
public class VolumeTestDemo {
public static void main(String[] args) { // TODO Auto-generated method stub
}
interface Volume{ final double pai = 3.1415; //体积计算 public double calVolume(); }
//圆柱体类 class Cylinder implements Volume{
//底边半径 double r =0; //高 double h =0;
public Cylinder(double r,double h){ this.r = r; this.h = h; }
//圆柱体体积计算 @Override public double calVolume() { // TODO Auto-generated method stub return r*r*pai*h; }
}
//圆锥类 class Cone implements Volume{
//底边半径 double r =0; //高 double h =0;
public Cone(double r,double h){ this.r = r; this.h = h; }
@Override public double calVolume() { // TODO Auto-generated method stub return 1/3*pai*r*r*h; }
} } |
TA贡献2016条经验 获得超9个赞
public class Test {
public static void main(String[] args) throws Exception{
Lamp lamp = new Lamp(new RedBulb());
lamp.on();
lamp.setB(new GreenBulb());
lamp.on();
}
}
class Lamp{
private Bulb b;
public Lamp(Bulb b){
this.b = b;
}
public void on(){
b.light();
}
public void setB(Bulb b){
this.b = b;
}
}
interface Bulb{
void light();
}
class RedBulb implements Bulb{
@Override
public void light() {
System.out.println("Red light.");
}
}
class GreenBulb implements Bulb{
@Override
public void light() {
System.out.println("Green light.");
}
}
添加回答
举报