java - 如何对这个类进行类型转换?

标签 java class oop object inheritance

我有一个名为 BasketBallPlayer 的父类(super class)

我有一个名为 ProBasketBallPlayer 的子类

如果我创建一个对象

BasketBallPlayer bp1;
bp1=new BasketBallPlayer("Tim Duncan", "Center", "Spurs", 83, 220, 4, 5, 8);


public class BasketBallPlayer {
    protected String name;
    protected String position;
    protected String team;
    protected int height;
    protected int weight;
    protected int agility;
    protected int speed;
    protected int ballHandling;

    public BasketBallPlayer() {
        this.name = "unknown";
        this.position = "unknown";
        this.team = "unknown";
        this.height = 0;
        this.weight = 0;
        this.agility = 0;
        this.speed = 0;
        this.ballHandling = 0;
    }

    public BasketBallPlayer( String name, String position, String team)
    {
        this.name = name;
        this.position = position;
        this.team = team;
        this.height = 0;
        this.weight = 0;
        this.agility = 0;
        this.speed = 0;
        this.ballHandling = 0;
    }

    public BasketBallPlayer (String name, String position, String team, int height, int weight,
                int agility, int speed, int ballHandling)
    {
        this.name = name;
        this.position = position;
        this.team = team;
        this.height = height;
        this.weight = weight;
        this.agility = agility;
        this.speed = speed;
        this.ballHandling = ballHandling;
    }

如何将其类型转换为 ProBasketballPlayer 而不会出现 ClassCastException

这是 ProBasketballPlayer 构造函数

public class ProBasketballPlayer extends BasketBallPlayer {
    protected int yearsInLeague;
    protected String role;

    public ProBasketballPlayer()
    {
        super();
        yearsInLeague = 0;
        role = "bench";
    }

    public ProBasketballPlayer( String name, String position, String team )
    {
        super(name, position, team);
        this.name = name;
        this.position = position;
        this.team = team;
        yearsInLeague = 0;
        role = "bench";
    }

    public ProBasketballPlayer(String name, String position, String team, int height, int weight,
            int agility, int speed, int ballHandling, int yearsInLeague, String role)
    {
        super(name, position, team, height, weight, agility, speed, ballHandling);
        this.yearsInLeague = yearsInLeague;
        this.role = role;
    }

最佳答案

你不能。类型转换不会改变对象的任何内容,它只是告诉编译器它可以将对象解释为类层次结构中更向上的类,但不能向下。一旦实例化了一个对象,就这样了——该对象绝对且不可撤销地是您实例化的类的成员。它是一个BasketballPlayer,并且永远不会是一个ProBasketballPlayer。如果您想要一台 Pro,请实例化一个 - 您将能够像普通篮球运动员一样使用 Pro,但反之则不然。

举个例子:

class Foo
{
    int a;
}
class Bar extends Foo
{
    int b;
}

Foo obj = new Foo();
obj.a = 0; // our obj has an "a" field because it is a Foo.
obj.b = 0; // but no "b" field, because it is not a Bar.

// It therefore makes no sense to do this:
((Bar)obj).b = 0; // because that's the same as trying to do "obj.b"

// in either case, "obj" is not a "Bar", and cannot have a "b" field. "obj" will always be a "Foo", never a "Bar".

//
// If however we make a Bar:
Bar obj2 = new Bar();

// we can access both fields, since Bar has both of them
obj2.a = 0;
obj2.b = 0;

// and, since Bar is a SUPERSET of Foo (all Bar are Foo, not all Foo are Bar), 
// we can even use obj2 as a Foo, and pass it to methods which accept a Foo.

关于java - 如何对这个类进行类型转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36879858/

相关文章:

java - 将 Java 构造函数与 Hibernate 查询结合使用

java - 为包含数组的类覆盖 java 中的 hashCode()

java - Spring - 普通 RabbitMQ 比普通 RabbitMQ + JMS 快很多?

java - 如何仅从所需的类(流)获取数据?

python - 如何在python中使用print显示类实例

java - 是先创建一个对象然后执行它的构造函数吗?

C++ 类 this-> 错误

php - 我应该将对象或普通数据传递到 View 中吗?

java - 如何在java小程序中像模拟时钟一样制作360度的线条动画

java - 如何在不是已配置 bean 的类中 Autowiring bean?