2 回答
TA贡献2016条经验 获得超9个赞
第一题有问题:1、创建Person接口(即“人”),它有setData()和getData()方法对“人”属性name、sex和birthday赋值和获得这些属性组成的字符串信息。
问题是:你说要创建一个人(接口),然后里面有方法对人的属性进行赋值?这怎么可能呢,接口是没有成员变量(属性)的,怎么能赋值?接口里只能有常量。
第二题可以答一下:
package pillar;
public class Pillar { private Geometry buttom;
private double height;
public Pillar() {
// TODO Auto-generated constructor stub
}
public Pillar(Geometry button,double height){
this.buttom = button;
this.height = height;
}
public double getVolume(){
return this.buttom.getArea()*height;
}
public Geometry getButtom() {
return buttom;
}
public void setButtom(Geometry buttom) {
this.buttom = buttom;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
------------------------------------------------类分割线---------------------------------------------------------
package pillar;
public interface Geometry { double getArea();
}
------------------------------------------------类分割线---------------------------------------------------------
package pillar;
public class Circle implements Geometry { private double r;
public Circle() {
// TODO Auto-generated constructor stub
}
public Circle(double r) {
this.r = r;
}
public double getArea() { return Math.PI*r*r;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
}
------------------------------------------------类分割线---------------------------------------------------------
package pillar;
public class Rectangle implements Geometry { private double width;
private double height;
public Rectangle() {
// TODO Auto-generated constructor stub
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getArea() { return this.width*this.height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
------------------------------------------------类分割线---------------------------------------------------------
package pillar;
public class TestPillar {
/** * @param args
*/
public static void main(String[] args) {
Circle c = new Circle(5);
Rectangle r = new Rectangle(3,4);
Pillar p1 = new Pillar(c,6);
Pillar p2 = new Pillar(r,6);
System.out.println("圆的体积:"+p1.getVolume()+"\t矩形的体积:"+p2.getVolume());
}
}
TA贡献1796条经验 获得超7个赞
第一题
Person接口
import java.util.Date; public interface Person { public void setData(String sID, String speciality, String name, String sex, Date birthday); public String getData(); } |
Student实现类
import java.util.Date; public class Student implements Person { private String sID; private String speciality; private String name; private String sex; private Date birthday; @Override public void setData(String sID, String speciality, String name, String sex, Date birthday) { this .sID = sID; this .speciality = speciality; this .name = name; this .sex = sex; this .birthday = birthday; } @Override public String getData() { return "Student [sID=" + sID + ", speciality=" + speciality + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + "]" ; } } |
添加回答
举报