Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 879eac1c authored by 66160245's avatar 66160245
Browse files

add Person class

parent ad7676d6
No related branches found
No related tags found
No related merge requests found
/Main.class
/Pet.class
/Cat.class
/Dog.class
/Rabbit.class
No preview for this file type
File added
public class Cat {
public class Cat extends Pet {
public Cat() {
super();
// TODO Auto-generated constructor stub
}
public Cat(String name, String color) {
super(name, color);
// TODO Auto-generated constructor stub
}
@Override
public String voice() {
// TODO Auto-generated method stub
return "Meow Meow";
}
}
......@@ -6,9 +6,10 @@ public class Main {
Person o = new Person("Aekapop","Bunpeng");
o.addPet(new Dog("Uncle","Gray"));
o.addPet(new Cat("Kai Jeaw","Orange"));
o.addPet(new Rabbit("Kai dao","Orange"));
System.out.println("Ownerasdadsa Name is " + o.getFullname() + " have " + o.countPets() + " pet");
System.out.println("Owner Name is " + o.getFullname() + " have " + o.countPets() + " pet");
for (int i = 0; i < o.countPets(); i++) {
System.out.println(i+1 + ". Name is " + o.getPetNameByIndex(i));
}
......
import java.util.ArrayList;
public class Person {
private String fristname;
private String lastname;
private ArrayList<Pet> petlist;
public Person() {
this.petlist = new ArrayList<Pet>() ;
}
public Person(String fristname, String lastname) {
super();
this.fristname = fristname;
this.lastname = lastname;
this.petlist = new ArrayList<Pet>() ;
}
public String getFristname() {
return fristname;
}
public void setFristname(String fristname) {
this.fristname = fristname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getFullname() {
return this.fristname + " " + this.lastname;
}
public int countPets() {
return this.petlist.size();
}
public int addPet(Pet p) {
this.petlist.add(p);
return this.petlist.size();
}
public String getPetNameByIndex(int i) {
return this.petlist.get(i).getName();
}
}
public class Rabbit {
public class Rabbit extends Pet {
public Rabbit() {
super();
// TODO Auto-generated constructor stub
}
public Rabbit(String name, String color) {
super(name, color);
// TODO Auto-generated constructor stub
}
@Override
public String voice() {
// TODO Auto-generated method stub
return "Rabbit Rabbit";
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment