因此,我正在为一个编程类编写代码,该类模拟一个网站,客户可以在该网站上购买产品并将其添加到购物车。我做了一个产品和购物车类(class)。它们都位于 some 目录中,并且它们的 .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;
}
}
最佳答案
当你编译时,你必须位于上层文件夹 - 即,因为你的包名称是 com.DownloadThis,你应该位于“com”文件夹之上(如果你从命令行发出一个目录,你应该看到结果中的 com 文件夹)。
com 文件夹应包含 DownloadThis 文件夹(名称区分大小写),该文件夹又必须包含您的 .class 文件。
关于java - "Cannot Find Symbol"为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13201342/