java学习进展到类,首先就对万类之父Object类进行举例练习,这里我是对一维数组和接口用Object接受数组和接口。
package test1; public class enum1 { public static void main(String[] args) { int temp[] = {1,3,5,7,9}; //测试于Object类接收数组
Object obj = temp;
print(obj);
A test = new B();//向上转型,实例化
System.out.println(test.getinfo());
System.out.println("\n");
Object obj2 = test; //向上转型
/*System.out.println(obj2.getinfo);此行代码无法运行,因为向上转型,子类方法无法运行*/
A x = (A)obj2; //向下转型
System.out.println(x.getinfo()); }
public static void print(Object o)
{
if(o instanceof int[])
{
int x[] = (int[])o;
for(int i =0;i<x.length;i++)
{
System.out.println(x[i]+"\t");
}
}
}}
interface A
{
public String getinfo();
}
class B implements A
{
public String getinfo()
{
return "hello world!";
}
}