我有一个包含类型字段的对象,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);
}