Java下行困境

Java下行困境,java,Java,请看一下这里的代码 class Vehicle { public void printSound() { System.out.print("vehicle"); } } class Car extends Vehicle { public void printSound() { System.out.print("car"); } } class Bike extends Vehicle{ // also tried to e

请看一下这里的代码

class Vehicle {
    public void printSound() {
        System.out.print("vehicle");
    }
}

class Car extends Vehicle {
    public void printSound() {
        System.out.print("car");
    }
}

class Bike extends Vehicle{ // also tried to extend Car
    public void printSound() {
        System.out.print("bike");
    }
}

public class Test {
    public static void main(String[] args) {
        Vehicle v = new Car();
        Bike b = (Bike)v;
        v.printSound();
        b.printSound();

        Object myObj = new String[]{"one", "two", "three"};
        for (String s : (String[])myObj) System.out.print(s + ".");


    }
}
执行此代码将给出
ClassCastException
说明
heritation.Car不能转换为heritation.Bike

现在看一行
objectmyobj=newstring[]{“一”、“二”、“三”}。该行与
车辆v=新车()相同对吗?在这两行中,我们都将子类对象指定给超类引用变量。但是向下投射
字符串[]myObj
是允许的,但是
(Bike)v
是不允许的。正如我在评论中提到的,我还试图推广使用自行车的汽车。根据这里的一些讨论,自行车不是汽车,因为它是汽车的延伸。如果我用自行车扩展汽车,那就意味着自行车是汽车的一种类型,但例外情况仍然存在

请帮我了解这里发生了什么事


注意-请不要把整辆车换成自行车,自行车换成汽车;)

否,提供的代码与示例中的代码在一个基本句子中有所不同:

//you're declaring a Object class variable
Object myObj = new String[]{"one", "two", "three"};
//you're declaring a Car class instance, not a Vehicle
Vehicle v = new Car();
他们不一样。在第一个示例中,您使用父类保存值,在第二个示例中,您使用子类并指定父值,但对象将是子对象,而不是父对象

让我们看一下课程组成以了解进一步的解释:

Object
- String[]
- Vehicle
  - Car
  - Bike
如您所见,每个
字符串[]
都将是
对象
,现在每个
汽车
都将是
车辆
,但
汽车
不是
自行车
。用代码解释它

Vehicle v = new Car();
//v contains an instance of Car
Car c = v;
//a Car is not a Bike, this line will throw an error
Bike b = c;
//v2 contains an instance of Vehicle
Vehicle v2 = new Vehicle();
//a Car is a Vehicle
Car c2 = v2;
//a Bike is a Vehicle
Bike b2 = v2;

不,提供的代码与示例中的代码在一个基本句子中有所不同:

//you're declaring a Object class variable
Object myObj = new String[]{"one", "two", "three"};
//you're declaring a Car class instance, not a Vehicle
Vehicle v = new Car();
他们不一样。在第一个示例中,您使用父类保存值,在第二个示例中,您使用子类并指定父值,但对象将是子对象,而不是父对象

让我们看一下课程组成以了解进一步的解释:

Object
- String[]
- Vehicle
  - Car
  - Bike
如您所见,每个
字符串[]
都将是
对象
,现在每个
汽车
都将是
车辆
,但
汽车
不是
自行车
。用代码解释它

Vehicle v = new Car();
//v contains an instance of Car
Car c = v;
//a Car is not a Bike, this line will throw an error
Bike b = c;
//v2 contains an instance of Vehicle
Vehicle v2 = new Vehicle();
//a Car is a Vehicle
Car c2 = v2;
//a Bike is a Vehicle
Bike b2 = v2;

两者之间的主要区别是示例是
objectmyobj=newstring[]{“一”、“二”、“三”}
此处
myObj
将引用一个字符串数组,由于引用的值实际上是一个字符串数组,您可以将其强制转换为一个字符串数组。
在另一个例子中,
Bike b=(Bike)v
b
的参考值设置为a
Car
。由于
汽车
不是一辆完整的
自行车
。自行车可能实现比汽车更多的功能,而汽车不知道这些功能。因此,您不能将
汽车
转换为
自行车

这两者之间的主要区别是,例如
对象myObj=newstring[]{“一”、“二”、“三”}
此处
myObj
将引用一个字符串数组,由于引用的值实际上是一个字符串数组,您可以将其强制转换为一个字符串数组。
在另一个例子中,
Bike b=(Bike)v
b
的参考值设置为a
Car
。由于
汽车
不是一辆完整的
自行车
。自行车可能实现比汽车更多的功能,而汽车不知道这些功能。因此,您不能将
汽车
转换为
自行车

这两者是不同的:
(String[])myObj
是允许的,因为
myObj
String[]
实例。但是不允许
(Bike)v
,因为
v
不是
Bike
或其任何超类的实例(它是
Car
实例)。

两者不相同:
(String[])myObj
是允许的,因为
myObj
String[/code>实例。但是不允许使用
(Bike)v
,因为
v
不是
Bike
或其任何超类的实例(它是
Car
实例)。

您试图将
Car
变成
Bike
。但是
汽车
并不是从
自行车
继承的。不幸的是,这种语言听起来不像英语那么现实和合乎逻辑。所以,是的,我正试图把一辆汽车变成一辆自行车,或者说,大多数编程语言…:)请看编辑。正如Cratylus提到的,汽车和自行车是不相关的。我把自行车从一辆车上伸出来。我仍然得到同样的例外与您的编辑。从概念上讲,
汽车
不是
自行车
,因此不能将它们相互转换是有道理的。你试图将
汽车
转换成
自行车
。但是
汽车
并不是从
自行车
继承的。不幸的是,这种语言听起来不像英语那么现实和合乎逻辑。所以,是的,我正试图把一辆汽车变成一辆自行车,或者说,大多数编程语言…:)请看编辑。正如Cratylus提到的,汽车和自行车是不相关的。我把自行车从一辆车上伸出来。我仍然得到同样的例外与您的编辑。从概念上讲,
汽车
不是
自行车
,因此不能将它们相互转换是有意义的。不,实际上根据代码,它类似于
车辆->汽车
车辆->自行车
。你提到它就像
Car->Vehicle
。事实并非如此。不,实际上,根据代码,它类似于
车辆->汽车
车辆->自行车
。你提到它就像
Car->Vehicle
。事实并非如此。