[英]Create a 2D array in java to store two kinds of objects
I have a superclass Agent
. 我有一个超一流的
Agent
。 The superclass Agent
has two subclasses: 1. User
; 超类
Agent
具有两个子类:1. User
; 2. Seller
. 2.
Seller
。 And these two sub classes are the superclass of User_action
and Seller_action
. 这两个子类是
User_action
和Seller_action
的超类。 I want to create two objects. 我想创建两个对象。 The first object is of class
User_action
. 第一个对象是
User_action
类。 The aim behind creating this object is that it should inherit the variables and methods of class Agent
and class User
(and, I hope I am achieving this aim.). 创建该对象的目的在于,它应该继承
Agent
类和User
类的变量和方法(并且,我希望可以实现这一目标。)。 And, I create the second object from class Seller_action
with the similar objective in mind (inherit variables and methods from class Agent
and class Seller
.). 并且,我从类
Seller_action
创建第二个对象,并牢记类似的目标(继承自类Agent
和类Seller
变量和方法)。
Once, I create these objects (or instances I believe) I want to store them in a 2D array. 一次,我创建了这些对象(或我相信的实例),然后将它们存储在2D数组中。 I don't know how to do this.
我不知道该怎么做。 And, this is my first attempt with java, so I have read very basic stuff and now I am trying to improve as I code.
而且,这是我第一次尝试使用Java,因此我已经阅读了非常基础的内容,现在我正在尝试改进代码。
Now, my problem is:How can I store two kinds of obejcts..in something. 现在,我的问题是:如何在某种对象中存储两种对象。 You see, the reason I want to do this is because I want all the objects to be present on a geographical coordinate and I want to know every one's coordinate or geographical position (x,y).
您知道,我这样做的原因是因为我希望所有对象都出现在地理坐标上,并且我想知道每个人的坐标或地理位置(x,y)。 And, as I come from MATLAB the best way to do this is to store it in an Matrix (2D array in JAVA).
而且,正如我来自MATLAB那样,最好的方法是将其存储在Matrix(JAVA中的2D数组)中。
I will appreciate any advice. 我将不胜感激任何建议。 Many thanks!
非常感谢!
I think what you should do is have 2 super classes: 1 - Agent, 2 - Action. 我认为您应该做的是拥有2个超级类:1-代理,2-动作。 The each has 2 sub classes and the action has a User value.
每个都有2个子类,并且操作具有User值。 You can then create an array of Actions
然后,您可以创建一个动作数组
I believe, you are looking to design the classes in the following manner such that objects ua and sa(defined below) inherit all the values from their parent: 我相信,您正在以以下方式设计类,以便对象ua和sa(在下面定义)继承其父级的所有值:
Agent is the Parent class: 代理是父类:
class Agent { //the parent class
}
Class User is sub class of Agent 类用户是代理的子类
class User extends Agent{ //user is subclass of Agent
int Agent_1,Agent_2;
User()
{
Agent_1 = 1;
Agent_2 = 2;
}
}
Class Seller is sub class of Agent 卖方类别是代理的子类别
class Seller extends Agent{ //Seller is sub class of Agent
int Seller_1, Seller_2;
Seller()
{
Seller_1 = 3;
Seller_2 = 4;
}
}
User_action is subclass of User User_action是User的子类
class User_action extends User{ //user action is subclass of User
int UA_1, UA_2;
User_action()
{
UA_1=5; UA_2 = 6;
}
}
Seller_action is subclass of Seller Seller_action是Seller的子类
class Seller_action extends Seller{ //Seller action is subclass of Seller
int SA_1, SA_2;
Seller_action()
{
SA_1=7; SA_2 = 8;
}
}
And then you create the following Objects(which inherit all the values from their parent): 然后创建以下对象(它们从其父级继承所有值):
User_action ua = new User_action();
Seller_action sa = new Seller_action();
ArrayList<Object>[][] table = new ArrayList[10][10];
table[0][0] = new ArrayList<Object>(); // add another ArrayList object to [0,0]
table[0][0].add(ua); // add object to that ArrayList
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.