Loading... 欢迎移步博主CSDN:[CSDN博客](https://blog.csdn.net/weixin_42327790/article/details/97132490) # 简介 今天有人问了一个关于JAVA对象之间的显隐式转换问题。为详细解释一下该原理,故此写下本篇博客!最后又顺便写了下该容器的具体实现。 # 具体缘由 ## 问 Question: 在一个容器里取出来时候 (没有定义泛型)为啥要强转 才能取出来? 知道索引不能直接取出来吗? ![请输入图片描述](https://www.520315.xyz/usr/uploads/2023/03/1373806757.png) ![请输入图片描述](https://www.520315.xyz/usr/uploads/2023/03/4095726017.png) ## 答 Answer: object对象不能直接赋值给int,不能隐式转换,需要手动写对象类型显式转换,这就像猫一定是动物,但动物却不一定是猫一样。强转弱能使用隐式转换,而弱转强则一定要显示转换 JAVA对象之间的显隐式转换: ```java public class MyTest { public static void main(String[] args) { MyTest mytest = new MyTest(); Object obj = new Object(); Animal a = new Animal(); Cat c = new Cat(); System.out.println("obj的类型:" + obj.getClass()); System.out.println("a的类型:" + a.getClass()); System.out.println("c的类型:" + c.getClass()); System.out.println(); //隐式转换 obj = a; a = c; System.out.println("obj的类型:" + obj.getClass()); System.out.println("a的类型:" + a.getClass()); System.out.println("c的类型:" + c.getClass()); System.out.println(); //显式转换 c = (Cat)a; a = (Animal)obj; System.out.println("obj的类型:" + obj.getClass()); System.out.println("a的类型:" + a.getClass()); System.out.println("c的类型:" + c.getClass()); System.out.println(); } } class Animal{ } class Cat extends Animal{ } ``` 输出结果: ```shell obj的类型:class java.lang.Object a的类型:class test.Animal c的类型:class test.Cat obj的类型:class test.Animal a的类型:class test.Cat c的类型:class test.Cat obj的类型:class test.Animal a的类型:class test.Animal c的类型:class test.Cat ``` --- **注** 因为在java中Objec类是所有类的超类,Object对象转Integer对象需要使用显式转换,然后Integer赋值给int 类型的a变量,这里会进行拆包。我认为写成下面这样比较好理解 ```java int a = (Integer) c.get(0); ``` --- # 补全该容器的实现(增删查改) ```java class MyCollection { //定于data对象用于存放数据 private Object[] data; //实际长度 private int size; //容器大小 private int maxSize = 10; public MyCollection(int maxSize) { this.maxSize = maxSize; this.data = new Object[maxSize]; } public MyCollection() { this.data = new Object[maxSize]; } public int getSize() { return this.size; } //根据索引取对象 public Object get(int index) { return this.data[index]; } //增 public void add(int index,Object obj) { if(index > size) { throw new ArrayIndexOutOfBoundsException(); } //扩容 if(index >= maxSize) { Object[] newData = new Object[maxSize + 10]; System.arraycopy(this.data, 0, newData, 0, this.data.length); this.data = newData; this.maxSize += 10; } for(int i = this.size; i>index;i--) { this.data[i] = this.data[i-1]; } this.data[index] = obj; this.size += 1; } //删 public Boolean remove(int index) { if(index > this.size) { System.out.println("error:越界!"); return false; } int i = index; for(;i<this.size;i++) { this.data[index] = this.data[index + 1]; } this.data[i] = 0; this.size -= 1; return true; } //查 public int find(Object obj) { int index = 0; while(index < this.size) { if(this.data[index].equals(obj)) { return index; } index += 1; } return -1; } //改 public void set(int index, Object obj) { this.data[index] = obj; } } ``` **以上内容纯属个人见解,如有错误,还请各位不吝指正!** 最后修改:2023 年 03 月 19 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏
2 条评论
你的文章内容非常卖力,让人点赞。 https://www.4006400989.com/qyvideo/55107.html
你的才华让人惊叹,请继续保持。 https://www.4006400989.com/qyvideo/96600.html