我有一个基本的任务,但我对OOP很新,并且正在努力.其他在线资源开始增加我的困惑.
我被要求:
>为类Person编写代码. Person对象具有属性名称,年龄和地址.
>为类Dog编写代码. Dog对象具有属性名称和年龄.
>在Person对象和Dog对象之间设置双向关联所需的Person和Dog类中提供任何其他代码. Person对象充当Dog对象的所有者,Dog对象充当Person对象的宠物.
>修改Person类,以便Person对象可以充当最多20个Dog对象的所有者.
显然这是一个非常简单的例子.
我的代码到目前为止:
人员类:
public class Person
{
// instance variables - replace the example below with your own
private String name;
private int age;
private String address;
/**
* Constructor for objects of class Person
*/
public Person()
{
this.name = name;
this.age = age;
this.address = address;
}
//Set Methods:
public void setName () {
this.name = name;
}
public void setAge () {
this.age = age;
}
public void setAddress () {
this.address = address;
}
//Get Methods:
public String getName () {
return name;
}
public int getAge () {
return age;
}
public String getAddress () {
return address;
}
}
狗类:
public class Dog
{
// instance variables - replace the example below with your own
private String name;
private int age;
public Dog()
{
this.name = name;
this.age = age;
}
//Set Methods:
public void setName () {
this.name = name;
}
public void setAge () {
this.age = age;
}
//Get Methods:
public String getName () {
return name;
}
public int getAge () {
return age;
}
}
主要:
public class Main
{
//Blank
}
我知道这段代码目前没用,并且没有做任何事情,但我不确定如何“关联”对象&在哪里做.作业规范指定一个人充当狗的“所有者”.
这就是我的问题所在.设置对象之间的关系.