java 动态创建属性_Java:动态添加类属性(Java: Add a Class Property Dynamically)

If you want the name of a class as commented by Jorn Vernee you can use instance.getClass().getName()

Since you mention dynamically creating a string, I guess you may mean that you want the name of the object itself. The simplest way to get the object name is to construct the object and pass a name to the constructor to initialise a member string.

Something like

public class Enemy

{

private String name;

public Enemy(String name)

{

this.name = name;

}

public getName()

{

return name;

}

}

Enemy orc = new Enemy("orc"); // could be an array of references instead of a var

orc.getName(); // returns the name of the object, obviously the name is whatever you construct the object with; so you could use any object identifier you like (such as a hash), this also means you can just construct the object and store its reference in an array

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值