gpt4 book ai didi

java - 带有实例的类似枚举的结构?

转载 作者:行者123 更新时间:2023-11-29 03:43:57 25 4
gpt4 key购买 nike

我使用以下枚举类型:

enum Status {OK,TIMEOUT,EXCEPTION}

但现在我想存储异常到底是什么。不幸的是,您不能实例化枚举类型。使类似以下内容成为可能的最佳方法是什么?

switch(status)
{
case(OK) {System.out.println("Everything OK!");break;}
case(TIMEOUT) {System.out.println("Timeout :-(");break;}
case(EXCEPTION) {System.out.println("We have an exception: "+status.exception);break;}
}

我的想法

  1. 单例类

    class Status
    {
    final Exception e;
    public final Status OK = new Status(null);
    public final Status TIMEOUT = new Status(null);
    public Status(Exception e) {this.e=e;}
    }

然后我可以这样做:

 if(status==Status.OK) {System.out.println("Everything OK!");}
else if(status==Status.TIMEOUT) {System.out.println("Timeout :-(");}
else {System.out.println("We have an exception: "+status.exception);}

2。几个类

class Status {}
class StatusOK extends Status {}
class StatusTimeout extends Status {}
class StatusException extends Status
{
final Exception e;
public StatusException(Exception e) {this.e=e;}
}

然后我需要一堆“instanceOf”语句。

P.S.: 好吧,我好像没有解释清楚。在我的程序中,我回答请求并存储这些请求的处理状态:

Map<Request,Status> request2Status;

因此我不能使用像 Status.getMessage(exception); 这样的东西。因为在我的代码中的那个位置我不知道它是哪个异常。这就是为什么我想将其保存在内部状态。

选择的解决方案

private static class LearnStatus implements Serializable
{
private static final long serialVersionUID = 1L;
public static final LearnStatus OK = new LearnStatus(null);
public static final LearnStatus TIMEOUT = new LearnStatus(null);
public static final LearnStatus NO_TEMPLATE_FOUND = new LearnStatus(null);
public static final LearnStatus QUERY_RESULT_EMPTY = new LearnStatus(null);
public static final LearnStatus NO_QUERY_LEARNED = new LearnStatus(null);

public final Exception exception;

private LearnStatus(Exception exception) {this.exception = exception; }

public static LearnStatus exceptionStatus(Exception cause)
{
if (cause == null) throw new NullPointerException();
return new LearnStatus(cause);
}

@Override public String toString()
{
if(this==OK) {return "OK";}
if(this==TIMEOUT) {return "timeout";}
if(this==NO_TEMPLATE_FOUND) {return "no template found";}
if(this==QUERY_RESULT_EMPTY) {return "query result empty";}
if(this==NO_QUERY_LEARNED) {return "no query learned";}
return "<summary>Exception: <details>"+exception.getLocalizedMessage()+"</details></summary>";
}
}

问题

如果我用 Status.OK 序列化一个对象,反序列化后 if(status==Status.OK) 不再工作。

新方案

我现在在类中包含了一个枚举类型。你怎么看?

private static class LearnStatus implements Serializable
{
public enum Type {OK, TIMEOUT, NO_TEMPLATE_FOUND,QUERY_RESULT_EMPTY,NO_QUERY_LEARNED,EXCEPTION}

public final Type type;

private static final long serialVersionUID = 1L;
public static final LearnStatus OK = new LearnStatus(Type.OK,null);
public static final LearnStatus TIMEOUT = new LearnStatus(Type.TIMEOUT,null);
public static final LearnStatus NO_TEMPLATE_FOUND = new LearnStatus(Type.NO_TEMPLATE_FOUND,null);
public static final LearnStatus QUERY_RESULT_EMPTY = new LearnStatus(Type.QUERY_RESULT_EMPTY,null);
public static final LearnStatus NO_QUERY_LEARNED = new LearnStatus(Type.NO_QUERY_LEARNED,null);

public final Exception exception;

private LearnStatus(Type type, Exception exception) {this.type=type;this.exception = exception;}

public static LearnStatus exceptionStatus(Exception cause)
{
if (cause == null) throw new NullPointerException();
return new LearnStatus(Type.EXCEPTION,cause);
}

@Override public String toString()
{
switch(type)
{
case OK: return "OK";
case TIMEOUT: return "timeout";
case NO_TEMPLATE_FOUND: return "no template found";
case QUERY_RESULT_EMPTY:return "query result empty";
case NO_QUERY_LEARNED: return "no query learned";
case EXCEPTION: return "<summary>Exception: <details>"+exception.getLocalizedMessage()+"</details></summary>";
default: throw new RuntimeException("switch type not handled");
}
}
}

最佳答案

除非一切正常,否则我会使用异常。

喜欢

   System.out.println("Everything OK!");
} catch(TimeoutException te) {
System.out.println("Timeout :-(")
} catch(Exception e) {
System.out.println("We have an exception: " + e);
}

当异常设计用于执行此类操作时,我认为没有必要使用 enum


在您和原始异常之间的层之上添加另一个层,您可以执行此操作。

interface Status {
String getMessage();
}

enum Statuses implements Status {
OK("Everything OK"), TIMEOUT("Timeout :-(");

private final String message;
private Statuses(String message) { this.message = message; }

String getMessage() { return message; }
}

class ExceptionStatus implement Status {
private final String message;
String getMessage() { return "Exception: " + message; }
}

// to print the message
System.out.println(status.getMessage());

关于java - 带有实例的类似枚举的结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11899491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com