简体   繁体   English

Java编程中的实用程序类

[英]Utility Classes in Java Programming

I am new to Java and I referred regarding my question on the Net but not quite Satisfied. 我是Java的新手,我在网上提到了我的问题,但并不满意。 I want to know what the "Utility Class" in Java is? 我想知道Java中的“实用程序类”是什么?

Can anybody please tell me with an Example. 有人可以告诉我一个例子吗?

Thanks, david 谢谢大卫

It's usually a class which only has static methods (possibly with a private constructor and marked abstract/final to prevent instantiation/subclassing). 它通常是一个只有静态方法的类(可能带有私有构造函数,并标记为abstract / final以防止实例化/子类化)。 It only exists to make other classes easier to use - for example, providing a bunch of static methods to work with String values, performing extra actions which String itself doesn't support. 它的存在只是使其他类更易于使用-例如,提供了一堆使用String值的静态方法,执行String本身不支持的额外操作。

Utility classes generally don't operate on classes you have control over, as otherwise you'd usually put the behaviour directly within that class. 实用程序类通常不会在您可以控制的类上运行,否则您通常会将行为直接放在该类中。 They're not terribly neat in OO terms, but can still be jolly useful. 从面向对象的角度来看,它们并不是十分整洁,但是仍然非常有用。

为了扩展Jon Skeet的答案, java.lang.Mathjava.util.Collectionsjava.util.Arrays是此类的典型示例。

它是一个具有所有静态方法且没有成员元素的类。

有一个实用程序 java.util ,其中包含一堆东西,例如日期,时间,字符串标记程序...不确定这是否是您在说的。

public class Utils { 公共类实用程序{

public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager
            .getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

public static void unlockScreenOrientation(Activity activity) {
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}

public static void lockScreenOrientation(Activity activity) {
    int currentOrientation = activity.getResources().getConfiguration().orientation;
    if (currentOrientation == Configuration.ORIENTATION_PORTRAIT)
    {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    else
    {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
}
// Get Http Post response
@Nullable
public static String getHttpResponse(String url, List<NameValuePair> nameValuePairs) {
    HttpClient httpClient = new DefaultHttpClient();

    HttpPost httpPost = new HttpPost(url);
    UrlEncodedFormEntity entity;
    try {
        entity = new UrlEncodedFormEntity(nameValuePairs);
        httpPost.setEntity(entity);
        HttpResponse response = httpClient.execute(httpPost);

        HttpEntity resEntity = response.getEntity();
        String res =  EntityUtils.toString(resEntity);
        return res;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

public static void CopyStream(InputStream is, OutputStream os) {
    final int buffer_size=1024;
    try
    {
        byte[] bytes=new byte[buffer_size];
        for(;;)
        {
            int count=is.read(bytes, 0, buffer_size);
            if(count==-1)
                break;
            os.write(bytes, 0, count);
        }
    }
    catch(Exception ex){}
}

public static JSONObject getJsonObjectFromXmlResponse(String xmlString) {
    JSONObject objectJson = new JSONObject();
    //JSONArray arrayJson = new JSONArray();

    XmlPullParser parser = Xml.newPullParser();
    try {
        parser.setInput(new StringReader(xmlString));
        int eventType = parser.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {
            String name;
            switch (eventType) {
                case XmlPullParser.START_TAG:
                    name = parser.getName();
                    if (name.equalsIgnoreCase("string")) {
                        String yourValue = parser.nextText();
                        //arrayJson = new JSONArray(yourValue);
                        objectJson = new JSONObject(yourValue);
                    }
                    break;
            }
            eventType = parser.next();
        }
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return objectJson;
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM