java - "Cannot Find Symbol"为什么?

标签 java

因此,我正在为一个编程类编写代码,该类模拟一个网站,客户可以在该网站上购买产品并将其添加到购物车。我做了一个产品和购物车类(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/

相关文章:

Java Math RoundingMode.HALF_EVEN 打印意外结果

java - 如何保存图像形式的 Jmeter 响应?

java - 使用自定义套接字工厂池化 LDAP 连接

java - 就内存管理而言,定义全局变量相对于局部变量的好处

java - FFT解释

java - 使用 RamdomAccesFile 和 FileChannel 对文件进行分块时出现问题

java - 将/注销请求重定向到/api/logout webflux spring boot

java - gridbaglayout 中的权重不正确

java - 执行引擎不打印结果

java - 在这种解析文本的场景中无法找出正确的数据结构和正确的方法