实验内容与操作:
public class Appilaction{
public static void main(String arges[]){
Building schoolBuilding;
int m=7;
schoolBuilding=new Building(m);
Window[]w=new Window[m];
for(int i=0;i<m;i++){
if(i%2==0){
w[i]=newWoodWindow();
w[i].setWidth(109.98);
w[i].setHeight(156.89);
if(i%2==1){
w[i]=newAluminumWindow();
w[i].setWidth(109.99);
w[1].setHeight(156.87);
}
}
schoolBuilding.use(w);
schoolBuilding.showWindow();
}
}
public class AluminumWindow{
public class AluminuWindow extends Window{
public String getMaterial(){
return"铝合金窗户";}}}
public class WoodWindow extends Window{
public String getMaterial(){
return"木质窗户";}}
public abstract class Window{
double width;
double height;
public abstract String getMaterial();
public void setWidth(double w){
width=w;
}
public void setHeight(double h){
height=h;
}
public double setHeight1(double h){
return width;
}
public double getWidth(){
return width;}}
public class Building{
int windowNumber=100;
double width=109.98;
double height=156.88;
Window []window;
Building(){
window=new Window[windowNumber];
}
Building(int n){
windowNumber=n;
window=new Window[windowNumber];}
public void use(Window[]w){
for(int i=0;i<window.length;i++){
booleanboo=Math.abs(w[i].getWidth()-width)<=1E-2&&Math.abs(w[i].getHeight()-height)<=1E-1;
if(boo){
window[i]=w[i];}}}
public void showWindow(){
for(int i=0;i<window.length;i++){
if(window[i]!=null){
System.out.println("第"+(i+1)+"扇窗户是:"+window[i].getMaterial());}
else{
System.out.println("该窗户未安装");}}}
实验结果:
第1个窗户是铝合金窗户
第2个窗户是铝合金窗户
第3个窗户是铝合金窗户
第4个窗户是铝合金窗户
第5个窗户是铝合金窗户