Java 什么是司机班?(爪哇)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/765751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:13:01  来源:igfitidea点击:

What is a driver class? (Java)

javaoopdriver

提问by lfaraone

I was reading through a Java textbook, and it mentions something called a "driver class". What is it, and how is it different from a normal class?

我正在阅读一本 Java 教科书,它提到了一个叫做“驱动程序类”的东西。它是什么,它与普通班级有何不同?

采纳答案by Stefan Kendall

A "Driver class" is often just the class that contains a main. In a real project, you may often have numerous "Driver classes" for testing and whatnot, or you can build a main into any of your objects and select the runnable class through your IDE, or by simply specifying "java classname."

“驱动程序类”通常只是包含 main 的类。在实际项目中,您可能经常有许多“驱动程序类”用于测试等等,或者您可以在任何对象中构建一个 main 并通过您的 IDE 选择可运行的类,或者只需指定“java 类名”。

回答by Jon Skeet

Without context, it's hard to tell. Is it talking about a JDBC driver, perhaps? If so, the driver class is responsible for implementing the java.sql.Driverinterface for a particular database, so that clients can write code in a db-agnostic way. The JDBC infrastructure works out which driver to use based on the connection string.

没有上下文,很难说清楚。也许是在谈论 JDBC 驱动程序?如果是这样,驱动程序类负责为特定数据库实现java.sql.Driver接口,以便客户端可以以与数据库无关的方式编写代码。JDBC 基础结构根据连接字符串确定要使用的驱动程序。

If the book wasn't talking about JDBC though, we'll need more context.

如果这本书不是在谈论 JDBC,我们将需要更多的上下文。

回答by java newbie

"driver class" could refer to a procedural programming style involving: (1) "container classes" and (2) "driver classes"

“驱动程序类”可以指涉及以下内容的过程编程风格:(1)“容器类”和(2)“驱动程序类”

Say that you are creating your own object as a container for data. Then you might want to create two types of classes: "containers" and "drivers"

假设您正在创建自己的对象作为数据容器。那么你可能想要创建两种类型的类:“容器”和“驱动程序”

The "container class" might contain: - instance variables to hold the relevant data - getters and setters - methods to support moving data in/out of class (parsing, translation) - limited computations

“容器类”可能包含: - 保存相关数据的实例变量 - getter 和 setter - 支持将数据移入/移出类(解析、翻译)的方法 - 有限的计算

The "driver class" might contain: - main method that drives the execution of the overall task (aka entry point for execution) - calls to static methods, as with procedural programming - instances of container class objects to hold different data (may be organized in other data structures, e.g. arrays; manipulated to solve overall task)

“驱动程序类”可能包含: - 驱动整个任务执行的主要方法(也称为执行入口点) - 调用静态方法,如过程式编程 - 容器类对象的实例以保存不同的数据(可以组织在其他数据结构中,例如数组;操纵以解决整体任务)

回答by NellyRed

According to my Java book:

根据我的 Java 书:

A driver class is a class that is in charge of running other classes. Just as the computer term "Software Driver" refers to a piece of software that runs or drives something else.

驱动程序类是负责运行其他类的类。正如计算机术语“软件驱动程序”是指运行或驱动其他东西的软件。