重命名openconnection()还是将其切断? [英] Renaming openconnection() or cutting it up?

查看:147
本文介绍了重命名openconnection()还是将其切断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以重命名openconnection()?

Is it possible to rename the openconnection()?

原始:

URL url = new URL("http://google.co.in");
URLConnection connection = url.**openConnection**();

之后:

URL url = new URL("http://google.co.in");
URLConnection connection = url.**connect**();

我只是想知道它是否可行以及我将如何去做。还有其他选择吗?为了做到这一点,我正在考虑上课,但我不能100%确定是否可能。

I'm just wondering if it is possible and how I would go about doing it. Is there an alternative? I was thinking of making a class in order to do this, but I wasn't 100% sure if that would be possible.

< ------ ------------或---------------->

<------------------ Or ---------------->

原始:

URL url = new URL("http://google.co.in");
URLConnection connection = url.**openConnection**();

之后:

string st1 = "open";
string st2 = "Connection";
URL url = new URL("http://google.co.in");
URLConnection connection = url.**st1 + st2**();

当我把它变成一个字符串时出现错误,但我不确定如何制作它结合了两者来定义它。如果那样的话,那我在使用Java编码方面有点生气。

I get an error when I make it a string, but I'm not really sure how to make it combine the two to define that. If that makes since, I'm kinda rusty at coding with Java.

推荐答案

用一个大包拿这个答案盐

Take this answer with a large bag of salt. This will work but you generally don't want to muck around with reflection unless you're very comfortable with Java.

你可以用反射完成#2,忽略你必须处理的各种令人讨厌的异常:

You can accomplish #2 with reflection, ignoring all sorts of nasty exceptions that you'll have to deal with:

String st1 = "open";
String st2 = "Connection";
URL url = new URL("http://google.co.in");
Object obj = url.getClass().getMethod(st1 + st2).invoke(url);
URLConnection connection = (URLConnection) obj;

参见:

  • Object#getClass()
  • Class#getMethod(String, Class...)
  • Method#invoke(Object, Object...)

您必须以某种方式处理的潜在例外情况,来自一行代码行:

The potential exceptions you'll have to deal with somehow, from one line of code:

  • NoSuchMethodException
  • SecurityException
  • IllegalAccessException
  • IllegalArgumentException
  • InvocationTargetException

我不确定你为什么要这样做。几乎可以肯定有更好的方法来实现相同的最终结果。 您尝试解决的问题是什么?

I'm not really sure why you want to do this, though. There is almost certainly a better way to accomplish the same end result. What is the bigger problem you're trying to solve?

这篇关于重命名openconnection()还是将其切断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆