Java 将实例强制转换为特定类型有什么意义?

Java 将实例强制转换为特定类型有什么意义?,java,types,Java,Types,对于将实例声明为特定类型的目的,我有点困惑。比如说, Integer intOb1 = new Integer(3); Object intOb2 = new Integer(4); 我知道intOb1的类型是Integer,而intOb2的类型是Object,但是将intOb2声明为Object允许做什么呢?使用对象中的方法?您是否已经可以将这些方法用作整数?或者主要目的仅仅是为了能够“将”intOb2视为一个对象 正如你所看到的,我很困惑。当涉及到对象时,你永远不会得到任何有用的东西,因为

对于将实例声明为特定类型的目的,我有点困惑。比如说,

Integer intOb1 = new Integer(3);
Object intOb2 = new Integer(4);
我知道
intOb1
的类型是
Integer
,而
intOb2
的类型是
Object
,但是将
intOb2
声明为
Object
允许做什么呢?使用
对象中的方法
?您是否已经可以将这些方法用作
整数
?或者主要目的仅仅是为了能够“将”
intOb2
视为一个对象


正如你所看到的,我很困惑。

当涉及到
对象时,你永远不会得到任何有用的东西,因为它是一种通用类型,在任何地方都没有帮助

但想想另一种情况:

List<String> list = new ArrayList<String>();
List List=new ArrayList();
这意味着一些特定的东西:
list
是一个
ArrayList
,将其声明为泛型
list
集合将强制您在整个代码中不使用任何
ArrayList
特定方法


这有效地允许您声明您不依赖任何特定的实现,稍后您将被允许切换到
ArrayList()
,例如,
LinkedList()
,而无需修改任何其他内容。

当涉及
对象时,您永远不会得到任何有用的东西,因为它是一种泛型类型,在任何地方都没有帮助

但想想另一种情况:

List<String> list = new ArrayList<String>();
List List=new ArrayList();
这意味着一些特定的东西:
list
是一个
ArrayList
,将其声明为泛型
list
集合将强制您在整个代码中不使用任何
ArrayList
特定方法


这实际上允许您声明您不依赖任何特定的实现,稍后您将被允许切换到
ArrayList()
,例如,
LinkedList()
,而无需修改任何其他内容。

此实际操作不称为强制转换这是多态性的一个示例。它允许变量根据它们与其他类的继承关系采用不同的类型

例如,假设您正在编写一个模拟动物园的程序。您将有一个名为
Zoo
的类和一个名为
Animal
的类。还有几个类是从
动物
类扩展而来的:
狮子
斑马
,和
大象

将所有这些对象集中在一个列表中非常有用,但由于它们的类型不同,例如:
狮子
斑马
、大象
,因此不能将它们存储在一个列表中,因此必须为每种动物维护一个单独的列表。这就是多态性发挥作用的地方

由于
狮子
斑马
大象
都是从
动物
类扩展而来,我们可以简单地将它们存储在
动物
类型的列表中

代码示例:

public class Zoo
{
   private List<Animal> animals; 

   public Zoo()
   {
      this.animals = new ArrayList<>();
   }

   //notice this method takes an Animal object as a parameter
   public void add(Animal a)
   {
      this.animals.add(a);
   }
}

public abstract class Animal
{
   private String name;
   private String type;

   public Animal(String name, String type)
   {
      this.name = name;
      this.type = type;
   }

   //all subclasses must implement this method
   public abstract void speak();
}

public class Lion extends Animal
{
   private String animalType = "Lion";

   public Lion(String name)
   {
      super(name, animalType);
   }

   public void speak()
   {
      System.out.println("ROAR");
   }
}

//....etc for the other two animals

public class TestZoo
{
   public static void main(String[] args)
   {
      Zoo myZoo  = new Zoo();
      Lion l     = new Lion("Larry");
      Elephant e = new Elephant("Eli");
      Zebra    z = new Zebra("Zayne");

      myZoo.add(l);  //<-- note that here I don't pass Animal objects to the 
      myZoo.add(e);  //    add method but subclasses of Animal
      myZoo.add(z);
   }
}
公共类动物园
{
私人动物名录;
公共动物园()
{
this.anives=新的ArrayList();
}
//请注意,此方法将动物对象作为参数
公共空间添加(动物a)
{
本.动物.加入(a);
}
}
公共抽象类动物
{
私有字符串名称;
私有字符串类型;
公共动物(字符串名称、字符串类型)
{
this.name=名称;
this.type=type;
}
//所有子类都必须实现此方法
公开摘要无效发言();
}
公营动物
{
私有字符串animalType=“Lion”;
公共Lion(字符串名称)
{
超级(名称,动物类型);
}
公开演讲
{
System.out.println(“ROAR”);
}
}
//其他两种动物的
公共类TestZoo
{
公共静态void main(字符串[]args)
{
动物园myZoo=新动物园();
狮子l=新狮子(“拉里”);
大象e=新象(“Eli”);
斑马z=新斑马(“Zayne”);

myZoo.add(l);//这个实际值不叫做casting这是多态性的一个例子。它允许变量根据它们与其他类的继承关系采用不同的类型

例如,假设您正在编写一个模拟动物园的程序。您将有一个名为
zoo
的类和一个名为
Animal
的类。还有几个类是从
Animal
类扩展而来的:
Lion
Zebra
Elephant

将所有这些对象集中在一个列表中是非常有用的,但由于它们的类型不同,例如:
狮子
斑马
、大象
,因此不能将它们存储在一个列表中,必须为每种动物维护一个单独的列表。这就是多态性的原因玩

由于
狮子
斑马
大象
都是从
动物
类扩展而来,我们可以简单地将它们存储在
动物
类型的列表中

代码示例:

public class Zoo
{
   private List<Animal> animals; 

   public Zoo()
   {
      this.animals = new ArrayList<>();
   }

   //notice this method takes an Animal object as a parameter
   public void add(Animal a)
   {
      this.animals.add(a);
   }
}

public abstract class Animal
{
   private String name;
   private String type;

   public Animal(String name, String type)
   {
      this.name = name;
      this.type = type;
   }

   //all subclasses must implement this method
   public abstract void speak();
}

public class Lion extends Animal
{
   private String animalType = "Lion";

   public Lion(String name)
   {
      super(name, animalType);
   }

   public void speak()
   {
      System.out.println("ROAR");
   }
}

//....etc for the other two animals

public class TestZoo
{
   public static void main(String[] args)
   {
      Zoo myZoo  = new Zoo();
      Lion l     = new Lion("Larry");
      Elephant e = new Elephant("Eli");
      Zebra    z = new Zebra("Zayne");

      myZoo.add(l);  //<-- note that here I don't pass Animal objects to the 
      myZoo.add(e);  //    add method but subclasses of Animal
      myZoo.add(z);
   }
}
公共类动物园
{
私人动物名录;
公共动物园()
{
this.anives=新的ArrayList();
}
//请注意,此方法将动物对象作为参数
公共空间添加(动物a)
{
本.动物.加入(a);
}
}
公共抽象类动物
{
私有字符串名称;
私有字符串类型;
公共动物(字符串名称、字符串类型)
{
this.name=名称;
this.type=type;
}
//所有子类都必须实现此方法
公开摘要无效发言();
}
公营动物
{
私有字符串animalType=“Lion”;
公共Lion(字符串名称)
{
超级(名称,动物类型);
}
公开演讲
{
System.out.println(“ROAR”);
}
}
//其他两种动物的
公共类TestZoo
{
公共静态void main(字符串[]args)
{