所以我正在编写一个编程类的代码,该类可以模拟一个客户可以购买产品并将其添加到购物车的网站。我做了一个产品和购物车课程。它们都在某个目录中,并且它们的.class文件位于同一个包中。那么为什么我在编译Cart类时会在Product上找到“找不到符号”?帮助PLZ!和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;
}
}
答案 0 :(得分:2)
编译时,你必须在上层文件夹中 - 即,因为你的包名是com.DownloadThis,你应该在“com”文件夹之上(如果你从命令行发出一个dir你应该看到结果中的com文件夹)。
com文件夹应包含DownloadThis文件夹(名称区分大小写),而该文件夹必须包含.class文件。
答案 1 :(得分:0)
我可以直接编译这两个文件../com/DownloadThis/运行“javac *”