用面向抽象的思想①计算柱体的体积②为学校开发这样一个小系统,包含类型:教师、学校、打印机

本文介绍了如何运用面向抽象的思想计算不同柱体的体积,强调了抽象类的使用方法和特点。同时,设计了一个学校管理系统,包括教师、学校和打印机,实现了各组件的详细信息输出。系统具有良好的可扩展性和可维护性,后续增加了学员类型,并升级了打印机功能,支持黑白和彩色打印机,进一步展示了接口设计的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用面向抽象的思想计算柱体的体积。
要求:至少能计算三种不同形状的柱体体积,并总结抽象类的使用方法和特点。

package ch005;

import java.util.Scanner;

public class Text {
    public static void main(String[] args) {
        System.out.println("请输入所要计算图形形状(1、圆柱体\t 2、三棱柱\t 3、长方体\t 4、结束计算):");
        Scanner scan=new Scanner(System.in);
        boolean flag=true;
        while(flag){
            int shape=scan.nextInt();
            switch (shape){
                case 1:
                    double dim=scan.nextDouble();
                    double height=scan.nextDouble();
                    Cylinder cylinder=new Cylinder(dim,height);
                    System.out.println("圆柱体的体积为:"+cylinder.callVolume());
                    break;
                case 2:
                    double length=scan.nextDouble();
                    double width=scan.nextDouble();
                    double height2=scan.nextDouble();
                    Cuboid cuboid=new Cuboid(length,width,height2);
                    System.out.println("长方体的体积为:"+cuboid.callVolume());
                    break;
                case 3:
                    double base=scan.nextDouble();
                    double bottomEdgeheight=scan.nextDouble();
                    double height3=scan.nextDouble();
                    ThreePrism threePrism=new ThreePrism(base, bottomEdgeheight,height3);
                    System.out.println("三棱柱的体积为:"+threePrism.callVolume());
                    break;
                case 4:
                    flag=false;
                    System.out.println("您退出了计算!");
                    break;
                default:
                    System.out.println("暂无算法");
                    break;

            }
        }

        scan.close();
    }
}
package ch005;

public class Cuboid extends Shape{
    double length;
    double width;
    double height;
    public Cuboid(double length,double width,double height){
        this.height=height;
        this.length=length;
        this.width=width;
    }
    @Override
    public double callVolume() {
        return length*height*width;
    }
}
package ch005;

public class Cylinder extends Shape{
    double dim;
    double height;
    public Cylinder(double dim,double height){
        this.dim=dim;
        this.height=height;
    }

    @Override
    public double callVolume() {
        return 3.14*dim*dim*0.5*height;
    }

}
package ch005;

public class ThreePrism extends Shape {
    double base;
    double bottomEdgeHeight;
    double height;
    public ThreePrism(double base,double bottomEdgeHeight,double height){
        this.base=base;
        this.bottomEdgeHeight=bottomEdgeHeight;
        this.height=height;
    }

    @Override
    public double callVolume() {
        return base*bottomEdgeHeight*height/6.0;
    }
}

用面向接口的思想的解决问题。
(1)为学校开发这样一个小系统,包含类型:教师、学校、打印机,具体要求如下:
教师以及学校都具有方法:输出详细信息
学校具有属性:打印机,能够通过学校的打印机打印教师或学校的详细信息
系统要具备良好的可扩展性与可维护性
(2)为刚才完成的系统增加一种新的类型:学员(Student),具体要求如下:
学员具有detail方法,负责输出学员详细信息
能够通过学校的打印机打印学员的详细信息
系统要具备良好的可扩展性与可维护性
(3)升级上述的学校系统,要求:
打印机有多种类型,比如:黑白打印机、彩色打印机等
学校可能配备其中任意一款打印机,负责打印教师、或者学校的详细信息
系统要具备良好的可扩展性与可维护性
要求:分三步走,继而完成整个程序。总结接口的使用方法和特点。

package ttt;

interface Introduceable {
    public String detail();
}

interface PrinterFace {
    public void print(String content);
}

public class AccpTest {
	   public static void main(String[] args){
		   //创建学校实例
		   AccpSchool3 school=new AccpSchool3();
		   //为学校配备黑白打印机
		   school.setPrinter(new BlackPrinter());
		   school.print(school);
		   //为学校配备彩色打印机
		   school.setPrinter(new ColorPrinter());
		   school.print(school);
	   }
	}
class AccpSchool3 implements Introduceable{
	   private PrinterFace printer;//打印机
	   public void setPrinter(PrinterFace p){
		   this.printer=p;
	   }
	   public String detail(){
		   return"这里是学校";
	   }
	   public void print(Introduceable intro){
		   printer.print(intro.detail());
	   }
	}

class BlackPrinter 
             implements PrinterFace{
  public void print(String content){
	  System.out.println("黑白打印:");
	  System.out.println(content);
  }

}
class ColorPrinter 
implements PrinterFace{
	public void print(String content){
		System.out.println("彩色打印:");
		System.out.println(content);
	}
}

class Printer {
    public void print(String content){
    	System.out.println("开始打印:");
    	System.out.println(content);
    }
}


class School2
                implements Introduceable{
         private Printer printer = new Printer();
         //输出学校详细信息
         public String detail(){
        	 return"这里是学校";
         }
         public void print(Introduceable intro){
        	 printer.print(intro.detail());
         }
}
class Teacher2 
implements Introduceable{
	public String detail(){
	 return "本人是教师";
	}
}
好的,下面是一个使用Java编写的简单应用程序,采用MVC(Model-View-Controller)结构来实现一个计算柱体体积的窗口,窗口布局为FlowLayout布局。 ### Model ```java public class CylinderModel { private double radius; private double height; public void setRadius(double radius) { this.radius = radius; } public void setHeight(double height) { this.height = height; } public double getVolume() { return Math.PI * radius * radius * height; } } ``` ### View ```java import javax.swing.*; import java.awt.*; public class CylinderView extends JFrame { private JTextField radiusField; private JTextField heightField; private JButton calculateButton; private JLabel resultLabel; public CylinderView() { setTitle("计算柱体体积"); setLayout(new FlowLayout()); radiusField = new JTextField(10); heightField = new JTextField(10); calculateButton = new JButton("计算体积"); resultLabel = new JLabel("体积结果: "); add(new JLabel("半径: ")); add(radiusField); add(new JLabel("高度: ")); add(heightField); add(calculateButton); add(resultLabel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 150); setLocationRelativeTo(null); setVisible(true); } public double getRadius() { return Double.parseDouble(radiusField.getText()); } public double getHeight() { return Double.parseDouble(heightField.getText()); } public void setResult(double volume) { resultLabel.setText("体积结果: " + volume); } public void addCalculateListener(java.awt.event.ActionListener listener) { calculateButton.addActionListener(listener); } } ``` ### Controller ```java public class CylinderController { private CylinderModel model; private CylinderView view; public CylinderController(CylinderModel model, CylinderView view) { this.model = model; this.view = view; view.addCalculateListener(new CalculateListener()); } class CalculateListener implements java.awt.event.ActionListener { public void actionPerformed(java.awt.event.ActionEvent e) { double radius = view.getRadius(); double height = view.getHeight(); model.setRadius(radius); model.setHeight(height); double volume = model.getVolume(); view.setResult(volume); } } } ``` ### Main ```java public class CylinderApp { public static void main(String[] args) { CylinderModel model = new CylinderModel(); CylinderView view = new CylinderView(); CylinderController controller = new CylinderController(model, view); } } ``` ### 解释 1. **Model**:负责数据的存储和计算。 2. **View**:负责界面的显示和用户交互。 3. **Controller**:负责处理用户输入和更新模型。 通过这种方式,我们将应用程序的逻辑、界面和数据分离,使代码更加清晰和易于维护。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值