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

时间:2016-04-27 03:41:44

标签: java class oop object inheritance

我有一个名为BasketBallPlayer的超级类

我有一个名为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;
    }

如何在不获取ClassCastException

的情况下将其强制转换为ProBasketballPlayer

以下是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;
    }

3 个答案:

答案 0 :(得分:1)

你做不到。转换不会改变对象的任何内容,它只是告诉编译器它可以将对象解释为进一步 up 类层次结构的类,而不是向下。一旦你实例化一个对象,那就是它 - 该对象肯定是不可撤销地成为你实例化的类的成员。它是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.

答案 1 :(得分:0)

  

如何在不获取的情况下将其强制转换为ProBasketballPlayer   ClassCastException异常

你不能向错误的方向投掷。想想以下两个陈述......

  1. ISA BasketballPlayer a ProBasketballPlayer?
  2. 所有篮球球员ProBasketBall球员吗?
  3. 没有。我打篮球,我不是职业篮球运动员。

    每个BirdDuck

    Bird b = new Bird();
    Duck daffy = (Bird)b;
    daffy.quack(); // Error here, not all birds quack?
    

答案 2 :(得分:0)

您无法将对象强制转换为子类。对象只能从它们的构造函数类型“向下”投射。每个Cat都是Animal,但不是每个Animal都是Cat。施法只是单向的。

可以做的是在ProBasketballPlayer中创建一个接受BasketBallPlayer并在相关字段中复制的构造函数。请务必初始化ProBasketballPlayer可能包含的任何其他字段。

public ProBasketballPlayer(BasketBallPlayer bbp, int yearsInLeague, String role) {
    super(bbp.getName(), bbp.getPosition(), bbp.getTeam(), bbp.getHeight(), bbp.getWeight(), bbp.getAgility(), bbp.getSpeed(), bbp.getBallHandling());
    this.yearsInLeague = yearsInLeague;
    this.role = role;
}

public public ProBasketballPlayer(BasketBallPlayer bbp) {
    this(bbp, 0, "bench");
}

如果ProBasketballPlayerBasketBallPlayer位于同一个包中,则您可以直接访问这些字段(例如bbp.name),而不是使用getter。