访问存储在对象类型的ArrayList内的不同类对象的属性和方法

huangapple go评论59阅读模式
英文:

Accessing attributes, methods of different class objects stored inside an object type Arraylist

问题

I have different weapon classes with various attributes and functions (some of which are similar). I am trying to iterate through objects of classes and trying to access those attributes based on certain conditions. Below are some of the objects I created and am storing inside an ArrayList of type Object.

Ak117 ak117 = new Ak117();
Ak47 ak47 = new Ak47();
Bk57 bk57 = new Bk57();
ArrayList<Object> weaponObjects = new ArrayList<>(Arrays.asList(ak117, ak47, bk57));
int damage = weaponObjects.get(0).damageStats;
//damage stats is an integer inside AK117 class that returns its damage

When I do this, Eclipse can't identify .damageStats and throws an error.
Is there any way wherein I can access all attributes or methods of these objects?

英文:

I have different weapon classes with various attributes and functions (some of which are similar). I am trying to iterate through objects of classes and trying to access those attributes based on certain conditions. Below are some of the objects I created and am storing inside an ArrayList of type Object.

Ak117 ak117 = new Ak117(); 
Ak47 ak47 = new Ak47(); 
Bk57 bk57 = new Bk57();
ArrayList &lt;Object&gt; weaponObjects = new ArrayList&lt;&gt;(Arrays.asList(ak117, ak47, bk57);
int damage = weaponObjects.get(0).damageStats; 
//damage stats is an integer inside AK117 class that returns its damage

When I do this Eclipse can't identify .damageStats; and throws an error.
Is there any way wherein I can access all attributes or methods of these objects?

答案1

得分: 1

You can use Interface or Abstract class

Example

  1. 创建一个包含所有类的常用方法的接口:
public interface Weapon{
    int getDamageStats();
    void shoot();
    
    // more method
}
  1. 创建实现该接口的类
class Ak47 implements Weapon{

    @Override
    public int getDamageStats() {
        return 100;
    }

    @Override
    public void shoot() {
        System.out.println("Ak射击");
    }
}

class Bk47 implements Weapon{

    @Override
    public int getDamageStats() {
        return 500;
    }

    @Override
    public void shoot() {
        System.out.println("Bk射击");
    }
}
  1. 通过Weapon调用列表:
Ak47 ak47 = new Ak47();
Bk47 bk47 = new Bk47();
ArrayList<Weapon> weaponsList = Arrays.asList(ak47, bk47);
int damage = weaponsList.get(0).getDamageStats();
英文:

You can use Interface or Abtract class

Example

  1. Create an interface that contains all the common methods of the classes:
public interface Weapon{
    int getDamageStats();
    void shoot();
    
    // more method
}
  1. Creat class implements this interface
class Ak47 implements Weapon{

    @Override
    public int getDamageStats() {
        return 100;
    }

    @Override
    public void shoot() {
        System.out.println(&quot;Ak shoot&quot;);
    }
}

class Bk47 implements Weapon{

    @Override
    public int getDamageStats() {
        return 500;
    }

    @Override
    public void shoot() {
        System.out.println(&quot;Bk shoot&quot;);
    }
}
  1. Call List by Weapon:
Ak47 = new Ak47();
Bk47 = new Bk47();
ArrayList &lt;Weapon&gt; weaponsList = Arrays.asList(ak47, bk47);
int damage = weaponsList .get(0).getDamageStats();

</details>



# 答案2
**得分**: 0

你需要在每个武器类中编写一个获取器。

```java
public class Ak47 {
    int damageStats;
    
    public int getDamageStats() {
        return damageStats;
    }
}

你可以通过创建Ak47的实例并调用它的getDamageStats来在另一个类中访问damageStats。

public class Example {
    Ak47 ak47 = new Ak47();
    
    int example = ak47.getDamageStats();
}
英文:

You need to write a getter in every weapon class.

public class Ak47 {
	int damageStats;
	
	public int getDamageStats() {
		return damageStats;
	}
}

You can access damageStats in another class by creating an instance of Ak47 and calling the getDamgeStats on it.

public class Example {
	Ak47 ak47 = new Ak47();
	
	int example = ak47.getDamageStats();
}

答案3

得分: 0

你需要类似以下的内容:

ArrayList<Weapon> weaponsList = Arrays.asList(ak117, ak47, bk47);

如果只存储Object,则只能为每个数组列表条目调用Object的方法。如果使用Weapon(用于所有武器的接口,定义了getDamageStats()),则所有子类都可以调用此方法。

英文:

you need something like the below:

ArrayList &lt;Weapon&gt; weaponsList = Arrays.asList(ak117, ak47, bk47);

If you only store the Objects you can only call the Object's methods for each array list entry. If you use Weapon (an interface for all weapons that defines getDamageStats()) all subclasses can then have this method called.

答案4

得分: 0

我认为你应该重新考虑添加继承,我不是枪械专家,但也许可以根据国家来区分,我不知道。另一种选择是使用抽象类,只抛出共有的方法并修改其他方法。然后,就像Rob Evans说的那样,遍历列表,但我建议使用新的ArrayList<>(new Ak47(), ...等等),而不是Arrays.asList。

英文:

I think you should rethink adding inheritance, I am not a gun expert but maybe by country idk. Another option is with abstract class and just throw the methods that are common and modify the others. Then just like Rob Evans said iterate over a list, but my advise is to use new ArrayList<>(new Ak47(), ...etc) and not Arrays.asList.

答案5

得分: 0

是的,这是Object类的一个常见问题。如果你将ArrayList初始化为Object,正如其他人已经说过的,你只能访问Object类的方法。根据你发送的内容,我们可以认为你的其他类共享一些通用属性。因此,最好的方法是通过一个超类(SuperClass)来传递,每个其他类都将扩展它,并在其中添加一个getter方法(在这种情况下是getDamageStats())。然后,将ArrayList初始化为超类而不是Object,你就能够访问你所想要的内容。

英文:

Yep, this is a casual problem with the Object class. If you initiate your ArrayList as an Object, as someone already said, you'll only be able to reach Object Class's methods. By what you sent, we can think that your other classes share communs attributes. So the best idea would be to pass by a SuperClass, which will be extended by every others classes and have a getter on it (in this case, a getDamageStats()). Then you initiate your ArrayList as the SuperClass instead of Object and you'll be able to reach what you seem to want

huangapple
  • 本文由 发表于 2020年7月28日 16:29:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63130026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定