[英]create object object in java confusion
hello everyone I am newto programming and I am just a little bit confused. 大家好,我是新手编程,我只是有点困惑。 I have three files in java
我在java中有三个文件
Product.java
this file defines the product attributes like product name, quantity, ID in this file i dont have a main Product.java
这个文件定义了产品属性,如产品名称,数量,ID在这个文件中我没有主
ProductCollection.java
this file create a vector to hold data temporarily when accessing database ProductCollection.java
此文件创建一个向量来临时访问数据库时保存数据
Database.java
database connection Database.java
数据库连接
EServer
communicate with client and retrieve access database EServer
与客户端通信并检索访问数据库
rs = query.executeQuery(queryString);
pColl = new ProductCollection();<-------------------confuse
product extractedProduct; <------------------------ confuse
while(rs.next())
{
extractedProduct = (Product).addProduct(rs);
// Object [ ] extractedProduct = ProductCollection.addProduct(rs);
}
EClient.java EClient.java
can someone explain this two line please 有人可以解释这两行吗
[ pColl = new ProductCollection();] [ product extractedProduct;]
also I am struggling to add an object to store the result temporary. 我也在努力添加一个对象来临时存储结果。 Can someone help me please I want extract out the contents of a row and set up a Product object
extractedProduct
有人可以帮助我,我想要提取出一行的内容并设置一个Product对象
extractedProduct
pColl = new ProductCollection();
This is as simple assignment. 这是一个简单的任务。 On the left hand side you have a variable,
pColl
, and on the right hand side you have an expression that creates a new object of type ProductCollection
and returns a reference to it. 在左侧,您有一个变量
pColl
,在右侧有一个表达式,它创建一个ProductCollection
类型的新对象并返回对它的引用。
product extractedProduct;
This is a variable declaration which says, "from this line on, I want to be able to use a variable which I call extractedProduct
and it refers to a product
" (or, put another way, you declare a variable of type product
). 这是一个变量声明 ,它说:“从这一行开始,我希望能够使用一个我称之为
extractedProduct
的变量,它指的是一个product
”(换句话说,你声明一个类型为product
的变量)。
The Java convention says that class names should start with a capital letter, so you probably want to change the name of the class so it reads Java约定说类名应该以大写字母开头,所以你可能想要更改类的名称以便读取
Product extractedProduct;
Useful links: 有用的链接:
ProductCollection is a class. ProductCollection是一个类。 Typically in any OO language the program operates on instances of objects.
通常,在任何OO语言中,程序都在对象的实例上运行。 Think of the class as a template for creating new instances of the class.
将类视为用于创建类的新实例的模板。
so the 所以
pColl = new ProductCollection();
line creates a new ProductCollection instance and gives your a reference to that instance, pColl. line创建一个新的ProductCollection实例,并为您提供对该实例pColl的引用。 On that particular line the type of pColl is not defined, so it must be somewhere else in the code before that line, or there is an error.
在该特定行上,未定义pColl的类型,因此它必须在该行之前的代码中的其他位置,或者存在错误。 You could do
你可以做到
ProductCollection pColl = new ProductCollection();
if that is the case. 如果是这样的话。 The next line
下一行
product extractedProduct;
means that extranctedProduct is a reference to an instance of product. 表示extranctedProduct是对产品实例的引用。 As a note, if product is a class it should be Product with a capital P. Since you don't assign a value, extractedProduct is uninitialized.
请注意,如果产品是一个类,它应该是具有大写字母P的Product。由于您没有赋值,extractProduct是未初始化的。
import java.sql.*;
import java.net.*;
import java.io.*;
import java.util.Enumeration;
public class EServer
{
public static void main(String[] args)
{
// Net and IO objects
ServerSocket ss= null;
Socket s = null;
BufferedReader bf = null;
PrintWriter pw = null;
// Queries
// Query which retireves all products
String allQuery = "Select * from StoreProducts";
// Query which retrieves products which are out of stock
String outOfStockQuery = "Select * from StoreProducts where Quantity = 0";
String queryString = "";
String messageBack = "";
// Database objects
Connection cn = null;
Statement query = null;
ResultSet rs = null;
String lineRead= "";
ProductCollection pColl = new ProductCollection();
Enumeration pEnumeration;
try
{
// Server socket set up on port 2000
ss = new ServerSocket(2000);
System.out.println("...Server socket set up");
// Set up a database
Database db = new Database
("darrel", "", "sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:", "products");
System.out.println("...Database set up");
// Get a connection to the database
Database.establishConnection();
cn = Database.getConnection();
System.out.println("...Database connection set up");
//Set up a query object
query = cn.createStatement();
// Loop waiting for connections
int count = 1;
while(true)
{
System.out.println("...Waiting for connection "+count);
count++;
s = ss.accept();
pw = new PrintWriter(s.getOutputStream(), true);
bf = new BufferedReader(new InputStreamReader(s.getInputStream()));
boolean looper = true;
while(true)
{
lineRead = bf.readLine();
switch(lineRead.charAt(0))//
{
// Out of Stock command
case 'O':{
queryString = outOfStockQuery;
break;
}
// All products
case 'A':{
queryString = allQuery;
break;
}
// Client has terminated
case 'E':{
looper = false;
break;
}
}
//Check if client has terminated
if(!looper) break;
// Client has not terminated
// Execute the required query and create result set
rs = query.executeQuery(queryString);
// Create collection of products
pColl = new ProductCollection();
// Process the rows that have been extracted
// Place them in pColl
Product extractedProduct;
while(rs.next())
{
*extractedProduct = (Product) pColl.addProduct(rs);*
}
// Form the collection of products, each terminated by asterisk
pEnumeration = pColl.elements();
// messageBack is to be concatenated to so initialise it to the empty string
messageBack= "";
while(pEnumeration.hasMoreElements())
{
messageBack+=(Product)pEnumeration.nextElement()+"*";
}
// Now send back the collection string to the client for display
pw.println(messageBack);
}
}
}
catch(Exception e)
{System.out.println("Trouble setting up the database "+e);}
// Close database connection
try
{
cn.close();
}
catch(Exception e)
{System.out.println("Problem closing connection");}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.