简体   繁体   English

Java:抽象类的对象

[英]Java: Object of abstract class

I got confused about the code stated below, where Player is an abstract class: 我对下面所述的代码感到困惑,其中Player是一个abstract类:

private void cbxTeamAItemStateChanged(java.awt.event.ItemEvent evt) {                                          
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            Player p = (Player) evt.getItem();
            if(P.getTipo().contains("Defense")){
                lblDefense1.setText("Skill:");
            }
        }
}

If in Java I can not create an Object of an abstract class, why is this working? 如果在Java 中无法创建abstract类的Object ,为什么这样做有效?

Isn't this line a instance of Player ? 这行不是Player的实例吗?

Player p = (Player) evt.getItem();

The variable p holds a reference to an instance of a subclass of the abstract class Player. 变量p包含对抽象类Player的子类的实例的引用。 Since the Player is an abstract class, no instance of it can be created. 由于Player是抽象类,因此无法创建其实例。 However, instances of the subclass of the abstract Player class can still be assigned to the variable p. 但是,抽象Player类的子类实例仍可以分配给变量p。 See https://www.computingnotes.net/java/abstract-class-in-java/ 参见https://www.computingnotes.net/java/abstract-class-in-java/

You're confusing variable with instance. 您正在将变量与实例混淆。 p is a Player variable, and evt.getItem() returns a concrete instance of a type that extends Player (or implements Player if Player were an interface). p是一个Player变量,并且evt.getItem()返回扩展Player的类型的具体实例 (如果Player是接口,则实现Player)。 What exact sub-type of Player isn't important since all that you need to know is that it adhere's to the Player abstract class contract and thus has all the necessary Player behaviors. Player的确切子类型并不重要,因为您需要知道的是它遵守Player抽象类协定,因此具有所有必要的Player行为。

You can have objects that have an abstract class as their type even though you can't directly construct abstract classes. 即使不能直接构造抽象类,也可以使对象具有抽象类作为其类型。 evt.getItem() has a specific, non-abstract type that it returns under the hood, and that type implements Player with some specific code. evt.getItem()具有一个特定的,非抽象的类型,可以在evt.getItem()返回它,并且该类型使用某些特定的代码实现Player If, someday, they wanted to change the name of that class, they could, and it wouldn't affect this code as long as it continued to implement all the methods in Player . 如果有一天他们想更改该类的名称,可以这样做,并且只要它继续实现Player所有方法,它就不会影响此代码。 Basically, this abstracts the specifics of that object away from you and only assures you that somewhere, someone implemented all the methods that a Player ought to have in the object. 基本上,这从您那里抽象了该对象的详细信息,并且仅向您保证在某处某个人实现了Player应该在该对象中拥有的所有方法。

You can not create instance of abstract class but you can up cast child to it's parent. 您不能创建abstract类的实例,但是可以将子类强制转换为它的父类。 In your case evt.getItem() is returning the child ( ie TennisPlayer ) and you are casting it to super class Player which is valid. 在您的情况下, evt.getItem()返回child TennisPlayer ),并且您将其强制转换为有效的超类Player

Consider following example, 考虑以下示例,

class Mango extends Fruit {}

abstract class Fruit {}

public class Test {

    public static void main(String[] args) {
        Mango mango = new Mango();
        //Note that mango is also fruit
        Fruit fruit = (Fruit) mango;
    }

}

Note it is valid to up cast child to it's parent because ( ie ) every Mango is surely a Fruit . 请注意,将子级强制转换为其父级是有效的,因为( )每个Mango 肯定都是Fruit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM