1

我有一个包含类型字段的对象,String其值类似于“ http://stackoverflow.com

我有一个必须采用URL. 所以我将它包装在一个方法中,该方法采用String,然后URL从构造 a String(显然需要包装在 try catch 中......)

似乎应该有一个更简单的方法。是否可以将我的值作为 aURL而不是 a存储在对象中String?我试过这个public URL url = "http://stackoverflow.com";

是否有可能按照这些思路做一些事情?有没有比我最初做的更好的方法?

谢谢

原来的:

public String urlString = "http://stackoverflow.com";


public boolean myMethod(String urlString) {
 try {
        URL newURL = new URL(urlString);
        useUrl(newURL);
 } catch (MalformedURLException e) {
                e.printStackTrace();
            }

}

不起作用,但似乎更好:

public URL url = "http://stackoverflow.com";


public boolean myMethod(URL url) {
        useUrl(url);
  }
4

7 回答 7

3

Java 不支持隐式转换。如果您想将一种类型转换为另一种类型,则必须明确地这样做

final URL url = new URL("http://stackoverflow.com/");

但是这会引发一个检查异常,所以你需要一个辅助方法,比如

private static final URL url = getUrl("http://stackoverflow.com/");

private static URL getUrl(String spec)  {
    try {
        return new URL(spec);
    } catch (MalformedURLException e) {
        throw new AssertionError(e);
    }
}

或使用构造函数

private final URL url;

public MyClass(String spec) throws MalformedURLException {
    url = new URL(spec);
}
于 2013-04-24T20:36:57.753 回答
0

您可以将其缩短一点,但您需要创建一个新的 URL 对象。

public boolean myMethod(String urlString) {
 try {

        useUrl(new URL(urlString));
 } catch (MalformedURLException e) {
                e.printStackTrace();
            }

}
于 2013-04-24T20:41:05.193 回答
0
public class Foo {  

  private URL test;
    {
        try {
            test = new URL("http://www.google.com");
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }
   public Foo() {

   }
}

或者

public class Foo {  

  private URL test;

   public Foo() {
        try {
            test = new URL("http://www.google.com");
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
   }
}

要么会工作。最好的方法是在构造函数中进行。

于 2013-04-24T20:48:23.537 回答
0

你不能这样做。根据定义,URL 不能格式错误。因此,如果问题是避免 try/catch,您的类应该使用String而不是URL, 并在需要时转换为URL内部。

您可以创建一个可以安全地转换为 URL 的新对象:

private class UnsafeURL {
    private String url;

    public UnsafeURL(String url){
        this.url = url;
    }

    public URL toURL() throws MalformedURLException {
        return new URL(url);
    }
}
于 2013-04-24T20:46:35.373 回答
0

我不确定我是否理解你想做什么,但这对你有帮助吗?

class SomeClass {
  private final String urlString = "http://stackoverflow.com";
  public URL url;

  public SomeClass() {
     try {
        this.url = new URL(this.urlString); // suppose the URL is correct
     } catch (MalformedURLException e) {
        e.printStackTrace();
     }
  }
}
于 2013-04-24T20:39:36.380 回答
0

您可能想看看 Spring 和 Apache 中的 URIBuilder,以获得一点额外的魔力。

URIBuilder uriBuilder = URIBuilder.fromUri( "http://stackoverflow.com/" );
URI uri = uriBuilder.build();
于 2013-04-24T20:43:16.780 回答
0

异常处理是必要的,因为 Java 在尝试这样做之前无法知道您的 String 是否可以转换为 URL。

可能你真正想做的是:

static URL URLofString(String s){
 URL r=null;
 try { 
   r= new URL("http://stackoverflow.com");  
  } catch (MalformedURLException e) { } 
  return r;
 }

接着

final static URL myURL=URLofString("http://stackoverflow.com");

在 Java 8 中,您将能够使用 lambda,尽管它几乎没有更好:

final static URL myURL = ()->{
  URL r=null; 
  try {  r= new URL("http://stackoverflow.com");  }
  catch (MalformedURLException e) { } return r;};
于 2013-04-24T20:55:23.513 回答