Object as an array
I need to create class Dog and PurebredDog extending Dog. Problem is that Dog can be at once single object and a开发者_如何学Pythonrray of objects (Dogs and PurebreedDogs :
Dog pack[]={new Dog(76589,"As","black",18,
"Ann","Kowalsky"),
new PurebreedDog(45321,"Labrador","Elf","black",25,
"Angus","Mati","Barbara","Smith"),
new Dog(102467,"Gamma","brown",89,
"Josh","Coke"),
new PurebreedDog(9678,"York","Theta","brown",8,
"Emka","Figaro","Alice","Cat")};
for(int i=0; i < pack.length; i++)
System.out.println(pack[i]+"\n\n");
How to write proper constructor for Dog
?
You could do :
public Dog(String name, etc){
}
but how to write constructor for array of dogs ?
public Dog(Dog[]tab) ?
And then how to recall it's elements ? Is pack[]
a 2d array ?
To simplify things, an instance of Dog
really should refer to a single Dog. So your constructor should look similar to (the data types are just examples):
Dog(int ID, String color, String name, ...)
PurebreedDog
would subclass Dog
and provide any additional constructor parameters (and members) such as breed
, etc.
To deal with multiple dogs, I recommend you store instances of the class in a List
, HashTable
, or other type of data structure that is designed to hold multiple elements. The actual structure you use will depend upon your requirements.
In your specific case, a Dog
should represent one dog, and I agree with Justin's answer.
Just for the sake of completeness, I think it's worth mentioning the composite pattern though.
There are indeed some circumstances where an aggregation of objects can acts the same way as its individual parts. A traditional example is the folder/file hierarchy. Both responds to the same methods such as size
, delete
, etc.
That could even be used with you Dog
example if you plan to model a genealogy tree. A dog is a dog, but can have N children.
The way to make uniform the representation of one dog and many dogs is for each entity to be a list. The one-dog entity is simply a list with one dog in it; the multiple-dog entity, of course, has more than one dog in it. But all you need to code is the (single) Dog, and then, as Justin and ewernli said, use a Collection - probably an ArrayList of Dog.
Trying to make the un-enlisted single Dog the "same" as a list of dogs is just crazy.
Okay, so did it the following way :
PurebreedDog class:
public class PurebreedDog extends Dog {
private String breed;
private String mother;
private String father;
public PurebreedDog(int id, String breed, String name, String color, int age,
String owner_name, String owner_surname, String mother, String father){
super(id, name, color, age, owner_name, owner_surname);
this.breed = breed;
this.mother = mother;
this.father = father;
}
@Override
public String toString(){
return super.toString()+"\n Pure breed dog \n"+"-------------------------------\n"+
"BREED: "+this.breed+"\n"+"MOTHER: "+this.mother+"\n"+
"FATHER: "+this.father+"\n-------------------------------";
}
}
Dog class:
public class Dog {
private int id;
private String name;
private String owner_name;
private String owner_surname;
private String color;
private int age;
private Dog[]pack;
public Dog(int i, String n, String c, int w, String on, String os){
id = i;
name = n;
color = c;
age = w;
owner_name = on;
owner_surname = os;
}
public Dog(Dog[]p){
Dog[]group = new Dog[p.length];
for(int i=0; i < p.length; i++){
group[i] = p[i];
}
this.pack = group;
}
@Override
public String toString(){
return "-------------------------------\n"+
"idr: "+this.id+"\n"+"OWNER: "+this.owner_name+
this.owner_surname+"\n"+"NAME: "+this.name+"\n"+
"color: "+this.color+"\n-------------------------------";
}
精彩评论