java中关于object[]数组的问题?

    Object[] objs={"abc",1,1.56};
    String str=(String) objs[0];
    Integer i=(Integer) objs[1];
    Double d=(Double) objs[2];
    System.out.println(objs[0]);
    System.out.println(objs[1]);
    System.out.println(objs[2]);
    往object[]数组中存3个数据,分别是String,Integer,Double型,然后从数组中取数据。取出来的数据赋值给对应的引用需要强制转换,也就是说取出来的数据是object型的。如果取出来的数据是object型的话,打印的结果应该是:getClass().getName() + "@" + Integer.toHexString(hashCode());类名@哈希值。但是实际上打印结果是:abc   1   1.56,与预期不符,原因是什么??

System.out.println实际是输出的内容是当前对象的toString方法,你说的getClass().getName() + "@" + Integer.toHexString(hashCode())这个是
Object默认的toString方法

Object源码:

  public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

而String,Integer,Double的toString都已经覆盖重写,所以不是Object那种输出了。

以后如果你想要输出自己声明类的输出信息,也可以覆盖toString方法。

纯手打,望采纳。有什么问题可以进一步交流

多态的问题,字符串和整型都重写了超类的方法

toString调用的是派生类重写的方法,无论你有没有强制转换成object,都不管,实际什么类型,就是什么类型。

多态的体现 父类引用指向子类对象 调用方法时 调用的是子类重写的方法

 1. 问题描述

在js中使用ajax请求在网页控制台下打印以下错误信息:

XMLHttpRequest cannot load http://192.168.2.46:8000/account/getjson/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access.
本人的ajax代码如下

var jsontree = [];
$.ajax({
    url: "http://192.168.2.46:8000/account/getjson/",
    type: "GET",
    dataType: 'JSON',
    success: function(result){
        jsontree = result;
    }
});
1
2
3
4
5
6
7
8
9
2. 解决方法

将上面的dataType: 'JSON'替换为dataType: 'JSONP'即可。

OK, Enjoy it!!!
多态调用,你可以试试编写一个代码,比如:
class Person{
    public void run(){
        System.out.println("跑");
    }
}

class SuperMan extends Person{
    public void run(){
        System.out.println("飞快的跑");
    }
}

然后编写一个测试类

class Test{
    public static void main(String[] args){
        Person p=new SuperMan();
        p.run();
    }
}

这个时候,打印出来的结果应该是"飞快的跑";
同理,你说的那个问题就是,多态调用了子类相应的toString方法..(纯手打,希望代码没写错 : ) )

toString调用的是派生类重写的方法,无论你有没有强制转换成object,都不管,实际什么类型,就是什么类型。

多态的问题,子类重写了Object类的toString方法

因为他们都重写了object的toStirng()方法