1

因此,我正在为一个模拟网站的编程类编写代码,客户可以在该网站上购买产品并将其添加到购物车中。我做了一个产品和购物车类。它们都在某个目录中,并且它们的 .class 文件在同一个包中。那么为什么我在编译我的 Cart 类时在 Product 上得到“找不到符号”?请帮忙!和 ty

购物车类:

package com.DownloadThis;

import java.util.ArrayList;

public class Cart {

private ArrayList<Product> myCart;

public Cart() {
myCart = new ArrayList<Product>();
}

public void addProduct(Product p) {
myCart.add(p);
}

public float getTotal() {
float totalPrice = 0;
for(int i = 0; i < myCart.size(); i++) {
  Object obj = myCart.get(i);
  Product p = (Product)obj;
  totalPrice = totalPrice + p.getPrice();
}
return totalPrice;
}

public void addToCart(int product_id) {


}
}

产品类别:

package com.DownloadThis;

import java.sql.*;

public class Product {

private String artist;
private String album;
private int year;
private String genre;
private float price;
private String albumart;
private String username;
private String password;

private Connection connection = null;
private ResultSet rs = null;
private Statement st = null;

public Product() {
}

public Product(String artist, String album, int year, String genre, float price, String albumart) {
 this.artist = artist;
 this.album = album;
 this.year = year;
 this.genre = genre;
 this.price = price;
 this.albumart = albumart;
}


public String getArtist() {
    return this.artist;
}

public void setArtist(String artist) {
    this.artist = artist;
}

public String getAlbum() {
    return this.album;
}

public void setAlbum(String album) {
    this.album = album;
}

public int getYear() {
    return this.year;
}

public void setYear(int year) {
    this.year = year;
}

public String getGenre() {
    return this.genre;
}

public void setGenre(String genre) {
    this.genre = genre;
}

public float getPrice() {
    return this.price;
}

public void setPrice(float price) {
    this.price = price;
}

public String getAlbumart() {
    return this.albumart;
}

public void setFilename(String albumart) {
    this.albumart = albumart;
}

}

4

2 回答 2

2

编译时,您必须在上层文件夹中 - 即,因为您的包名称是 com.DownloadThis,所以您应该位于“com”文件夹之上(如果您从命令行发出 dir,您应该会看到 com 文件夹结果中)。

com 文件夹应包含 DownloadThis 文件夹(名称区分大小写),该文件夹又必须包含您的 .class 文件。

于 2012-11-02T18:56:12.820 回答
0

我可以通过运行“javac *”直接从 ../com/DownloadThis/ 编译这两个文件

于 2012-11-02T19:08:09.353 回答