Object、JSONArray和JSONObject

1.Object

Object类是所有类的父类,位于java.lang包中。如果一个类没有用extends明确指出继承于某个类,那么它默认继承Object类。
一些方法:
toString方法可以将任何一个对象转换成字符串返回,返回值的生成算法为:getClass().getName() + ‘@’ + Integer. toHexString(hashCode())
在子类中可以重写toString方法

equals方法:
Object类中equals方法源代码如下:

public boolean equals(Object obj)  
   {  
       return this == obj;  
   }

Object中的equals方法是直接判断this和obj本身的值是否相等,即用来判断调用equals的对象和形参obj所引用的对象是否是同一对象,所谓同一对象就是指内存中同一块存储单元,如果this和obj指向的是同一块内存对象,则返回true,如果this和obj指向的不是同一块内存,则返回false,注意:即便是内容完全相等的两块不同的内存对象,也返回false。
equals方法(equals方法不能作用于基本数据类型的变量),如果没有对equals方法进行重写,则比较的是引用类型的变量所指向的对象的地址;诸如String、Date等类对equals方法进行了重写的,比较的是所指向的对象的内容。

2.JSONArray

public final class JSONArray:
在包net.sf.json中,继承自Object,实现了接口JSON、List、Comparable
一些方法:
public static JSONArray fromObject(Obeject object):
Creates a JSONArray.
Inspects the object type to call the correct JSONArray factory method. Accepts JSON formatted strings, arrays, Collections and Enums.

public int size()
Get the number of elements in the JSONArray, included nulls

public Object get(String key):
Get the value object associated with a key.

3.JSONObject

public final class JSONObject:
在包net.sf.json中,继承自Object,实现了接口JSON、Map、Comparable
一些方法:
public static JSONObject fromObject(Obeject object):
Creates a JSONObject.

public Object get(String key):
Get the value object associated with a key.

public int size()
Get the number of keys stored in the JSONObject.

public String toString()
Make a JSON text of this JSONObject. For compactness, no whitespace is added. If this would not result in a syntactically correct JSON text, then null will be returned instead.

更多可参考链接:
http://json-lib.sourceforge.net/apidocs/jdk15/index.html

猜你喜欢

转载自blog.csdn.net/weixin_34162401/article/details/87096398