*************************************************************************************************************
调试这段java代码是出错: 无法从静态上下文中引用非静态 变量 this
Product p=new Product("X123","戴尔","计算机","L321",25,15,3500.0,"广州","电脑城","计算机设备");
^
1 错误
处理已完成。请问这是为什么?怎么改?谢谢大家!
*************************************************************************************************************
import java.io.*;
import java.util.*;
public class ObjTest
{
public static void main(String[] args)
{
try
{
FileOutputStream fs=new FileOutputStream("product.obj");
ObjectOutputStream os=new ObjectOutputStream(fs);
Product p=new Product("X123","戴尔","计算机","L321",25,15,3500.0,"广州","电脑城","计算机设备");
os.writeObject(p);
os.flush();
fs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
class Product implements java.io.Serializable
{
String ProductNo;
String ProductName;
String ProductClass;
String ProductType;
int ProductNumber;
int MinNumber;
Double ProductPrice;
String ProductArea;
String SupplierCompany;
String ProductDescript;
public Product(String ProductNo,String ProductName,String ProductClass,String ProductType,int ProductNumber,
int MinNumber,Double ProductPrice,String ProductArea,String SupplierCompany,String ProductDescript)
{
this.ProductNo=ProductNo;
this.ProductName=ProductName;
this.ProductClass=ProductClass;
this.ProductType=ProductType;
this.ProductNumber=ProductNumber;
this.MinNumber=MinNumber;
this.ProductPrice=ProductPrice;
this.ProductArea=ProductArea;
this.SupplierCompany=SupplierCompany;
this.ProductDescript=ProductDescript;
}
}
}
------解决方案--------------------
static class Product implements java.io.Serializable
或者
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class ObjTest {
public static void main(String[] args) {
init();
}
public static void init(){
try {
FileOutputStream fs = new FileOutputStream("product.obj");
ObjectOutputStream os = new ObjectOutputStream(fs);
Product p = new Product("X123", "戴尔", "计算机", "L321", 25, 15,
3500.0, "广州", "电脑城", "计算机设备");
os.writeObject(p);
os.flush();
fs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
static class Product implements java.io.Serializable {
String ProductNo;
String ProductName;
String ProductClass;
String ProductType;
int ProductNumber;
int MinNumber;
Double ProductPrice;
String ProductArea;
String SupplierCompany;
String ProductDescript;
public Product(String ProductNo, String ProductName,
String ProductClass, String ProductType, int ProductNumber,
int MinNumber, Double ProductPrice, String ProductArea,
String SupplierCompany, String ProductDescript) {
this.ProductNo = ProductNo;
this.ProductName = ProductName;
this.ProductClass = ProductClass;
this.ProductType = ProductType;
this.ProductNumber = ProductNumber;
this.MinNumber = MinNumber;
this.ProductPrice = ProductPrice;
this.ProductArea = ProductArea;
this.SupplierCompany = SupplierCompany;
this.ProductDescript = ProductDescript;