What is a driver class? (Java)

2020-02-05 06:18发布

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 oop driver
4条回答
Summer. ? 凉城
2楼-- · 2020-02-05 06:51

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

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

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)

查看更多
祖国的老花朵
3楼-- · 2020-02-05 06:58

According to my Java book:

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.

查看更多
家丑人穷心不美
4楼-- · 2020-02-05 07:00

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."

查看更多
可以哭但决不认输i
5楼-- · 2020-02-05 07:02

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.Driver interface 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.

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

查看更多
登录 后发表回答