What does this block of code do?

后端 未结 6 980
鱼传尺愫
鱼传尺愫 2021-01-24 13:19

I\'m not quite sure what this means or whats it doing, Could some one elaborate?

Player player = (Player) sender;
相关标签:
6条回答
  • 2021-01-24 13:28

    It converts sender to a Player object. Otherwise, the datatype of player wouldnt match the datatype of sender. Usually done if sender could initially have been declared as a subclass.

    0 讨论(0)
  • 2021-01-24 13:28

    This is an assignment, with a cast operation.

    You can learn a lot about java cast operator with the answers to this question: How does the Java cast operator work?

    0 讨论(0)
  • 2021-01-24 13:36

    That's a plain old java type cast. See the JLS Casting conversion for the full details.

    It assumes that sender is type-compatible with a Player.

    0 讨论(0)
  • 2021-01-24 13:43

    It takes the object referenced by sender, and attempts to cast it into the type Player. Java objects are strongly typed, which means you have to declare the type of the object.

    If the object referenced by sender cannot be cast to a Player object, than an exception will be thrown for an InvalidCast.

    0 讨论(0)
  • 2021-01-24 13:43

    this is a simple java typecast...

    is this in a bukkit minecraft server plugin? if it is, what it does is typecast the player sending the command to a Player object. Player Objects are used to target specific players in code. the player object will have the name of the player who sent the command.

    0 讨论(0)
  • 2021-01-24 13:47

    When you read it 'converts' sender to Player, don't think it literally converts them.

    There are many times where you pass a variable which COULD be many different things, then when you figure out what thing it is, you use the cast operator to actually MAKE one of those things.

    Heres a imperfect analogy:

    Imagine you get a phone call from your local computer club president, he says that a member of the club will be coming to see you about something.

    Now, you don't know which member is coming, if its female or male, how old or even their name, you know nothing about the future visitors attributes.

    Once they appear at your door, you realize its your buddy Frank, now in your mind, the 'visitor' (sender) from code above, is CAST to 'Frank', who you know lots of stuff about, age, how many kids, address etc.

    Before the CAST, you knew very little about the sender, but after the cast you can now access all kinds of info on the new Object, since you now know its TYPE.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题