Java 接口序列化
我试图序列化一个接口对象,我已经实例化并实现了它的方法,对于一个问题,我得到了以下错误,它似乎在文件中写入了一些东西,但我还是得到了异常 ---代码已更新--- 接口Java 接口序列化,java,serialization,interface,Java,Serialization,Interface,我试图序列化一个接口对象,我已经实例化并实现了它的方法,对于一个问题,我得到了以下错误,它似乎在文件中写入了一些东西,但我还是得到了异常 ---代码已更新--- 接口 package ro.axonsoft.internship21.pay; import ro.axonsoft.internship21.error.PayError; import java.io.Serializable; import java.math.BigDecimal; public interface Pa
package ro.axonsoft.internship21.pay;
import ro.axonsoft.internship21.error.PayError;
import java.io.Serializable;
import java.math.BigDecimal;
public interface PayMetrics extends Serializable {
BigDecimal averagePaymentAmount();
Integer bigPayments();
Integer smallPayments();
Integer paymentsByMinors();
BigDecimal totalAmountCapitalCity();
Integer foreigners();
PayError[] errors();
}
以及实现和序列化
package ro.axonsoft.internship21.pay;
import ro.axonsoft.internship21.cnp.CnpException;
import ro.axonsoft.internship21.cnp.CnpParts;
import ro.axonsoft.internship21.cnp.CnpValidatorImpl;
import ro.axonsoft.internship21.cnp.Judet;
import ro.axonsoft.internship21.error.PayError;
import java.io.*;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
public class PayMetricsProcessorImpl implements PayMetricsProcessor{
@Override
public void process(InputStream paymentsln, OutputStream metricsOut) throws IOException {
Scanner scanner=new Scanner(paymentsln);
List<PayError> payErrors=new LinkedList<>();
HashMap<CnpParts,BigDecimal> hashMap=new HashMap<>();
int nr_line=0;
while(scanner.hasNextLine()){
String line=scanner.nextLine();
String[] components=new String[2];
if(line.contains(";")) {
components=line.split(";");
boolean price_ok=true;
for(int i=0;i<components[1].length();i++){
if(Character.isLetter(components[1].charAt(i))){
price_ok=false;
break;
}
}
if (!price_ok){
int finalNr_line = nr_line;
PayError payError=new PayError() {
@Override
public Integer line() {
return finalNr_line;
}
@Override
public Integer type() {
return 2;
}
};
payErrors.add(payError);
}
else {
BigDecimal price=BigDecimal.valueOf(Double.parseDouble(components[1]));
try {
CnpValidatorImpl cnpValidator=new CnpValidatorImpl();
CnpParts cnpParts=cnpValidator.validateCnp(components[0]);
hashMap.put(cnpParts,price);
}
catch (CnpException e){
int finalNr_line1 = nr_line;
PayError payError=new PayError() {
@Override
public Integer line() {
return finalNr_line1;
}
@Override
public Integer type() {
return 1;
}
};
payErrors.add(payError);
}
}
}
else {
int finalNr_line2 = nr_line;
PayError payError=new PayError() {
@Override
public Integer line() {
return finalNr_line2;
}
@Override
public Integer type() {
return 0;
}
};
payErrors.add(payError);
}
nr_line++;
}
scanner.close();
///AVERAGE PAYMENT
BigDecimal sum=new BigDecimal(0);
for(Map.Entry mapEl:hashMap.entrySet()){
BigDecimal value= (BigDecimal) mapEl.getValue();
sum=sum.add(value);
}
BigDecimal averagePaymentAmount=new BigDecimal(String.valueOf(sum));
BigDecimal numitor=new BigDecimal(hashMap.size());
averagePaymentAmount=averagePaymentAmount.divide(numitor);
averagePaymentAmount.setScale(2, BigDecimal.ROUND_UP);
///BIG PAYMENTS
Integer bigpayments=new Integer(0);
for(Map.Entry mapEl:hashMap.entrySet()){
BigDecimal value= (BigDecimal) mapEl.getValue();
if(value.intValue()>5000){
bigpayments++;
}
}
///SMALL PAYMENTS
Integer smallpayments=new Integer(0);
for(Map.Entry mapEl:hashMap.entrySet()){
BigDecimal value= (BigDecimal) mapEl.getValue();
if(value.intValue()<=5000){
smallpayments++;
}
}
///PAYMENTS BY MINORS
Integer paymentsbyminors=new Integer(0);
for(Map.Entry mapEl:hashMap.entrySet()){
CnpParts cnpParts= (CnpParts) mapEl.getKey();
LocalDate localDate=LocalDate.now();
if(localDate.getYear()-cnpParts.birthDate().year()<18){
paymentsbyminors++;
}
}
///TOTAL AMOUNT CAPABILITY
BigDecimal totalAmountCapitalCity=new BigDecimal(0);
for(Map.Entry mapEl:hashMap.entrySet()){
CnpParts cnpParts= (CnpParts) mapEl.getKey();
BigDecimal value= (BigDecimal) mapEl.getValue();
if(cnpParts.judet()== Judet.BU){
totalAmountCapitalCity=totalAmountCapitalCity.add(value);
}
}
///FOREIGNERS
Integer foreigners=new Integer(0);
for(Map.Entry mapEl:hashMap.entrySet()){
CnpParts cnpParts= (CnpParts) mapEl.getKey();
if(cnpParts.foreigner()){
foreigners++;
}
}
///PAY ERRORS
int size=payErrors.size();
PayError[] payerros=new PayError[size];
for(int i=0;i<size;i++){
payerros[i]=payErrors.get(i);
}
BigDecimal finalAveragePaymentAmount = averagePaymentAmount;
Integer finalBigpayments = bigpayments;
Integer finalSmallpayments = smallpayments;
Integer finalPaymentsbyminors = paymentsbyminors;
BigDecimal finalTotalAmountCapitalCity = totalAmountCapitalCity;
Integer finalForeigners = foreigners;
PayMetrics payMetrics=new PayMetrics() {
@Override
public BigDecimal averagePaymentAmount() {
return finalAveragePaymentAmount;
}
@Override
public Integer bigPayments() {
return finalBigpayments;
}
@Override
public Integer smallPayments() {
return finalSmallpayments;
}
@Override
public Integer paymentsByMinors() {
return finalPaymentsbyminors;
}
@Override
public BigDecimal totalAmountCapitalCity() {
return finalTotalAmountCapitalCity;
}
@Override
public Integer foreigners() {
return finalForeigners;
}
@Override
public PayError[] errors() {
return payerros;
}
};
ObjectOutputStream out = new ObjectOutputStream(metricsOut);
out.writeObject(payMetrics);
out.close();
}
}
此
PayMetrics PayMetrics=new PayMetrics(){….}
将创建一个匿名内部类,该类具有对外部类的隐式引用PayMetricsProcessorImpl
。如果希望能够序列化后者,则必须能够序列化前者
如果您可以创建一个单独的类,这对您来说会容易得多,如:
public class PayMetricsImpl implements PayMetrics { .... }
并序列化
PayMetricsImpl
的特定实例,其中PayMetricsProcessorImpl
?您显示的代码显然不是您运行的代码这个序列化在impl类中,您说的“您没有它”是什么意思?线程“main”java.io.NotSerializableException:ro.axonsoft.Internicship21.pay.PayMetricsProcessorImpl中的异常从何而来?对不起,我读错了,impl类是当前的类如果我这样做,我现在就添加代码,将我的文件输入发送到impl类进行解析的最佳方法是什么?类似的实例哈希映射?抱歉,我不理解你的问题,而且它超出了你最初的问题。抱歉,那么,我会重新检查
public class PayMetricsImpl implements PayMetrics { .... }