Java 接口及其实现
Java Interface and its Implementation
我在做一个练习,需要DogSchool来实现PetSchool。
我打算做一个在宠物学校注册的动物的数组列表,狗学校需要区分狗和其他动物。狗的特征就是它们的叫声"Wau! Wau!"。
我已经更正了。但是还是分不清猫狗
等级 = 动物
这是接口代码
import java.util.ArrayList;
public interface PetSchool {
boolean add(Tier tier);
boolean addAll(ArrayList<Tier> tiere);
boolean remove(Tier tier);
ArrayList<Tier> getTiere();
}
This is the code of Implementation.
Please tell me what's wrong with it.
import java.util.ArrayList;
public class DogSchool implements PetSchool {
public ArrayList<Tier> tiere= new ArrayList<Tier>();
@Override
public boolean add(Tier t){
if(t.gibLaut().equalsIgnoreCase("Wau! Wau!")){
return tiere.add(t);
}
else {
return false;
} }
@ Override
public boolean addAll(ArrayList<Tier> tiere){
return this.tiere.addAll(tiere);
}
@Override
public boolean remove(Tier t){
if(!t.gibLaut().equalsIgnoreCase("Wau! Wau!")){
return tiere.remove(t);
}
else{
return false;
}
}
@Override
public ArrayList<Tier> getTiere() {
return new ArrayList<Tier>(this.tiere);
}
}
嗯,问题出现在demoTest中:
import java.util.ArrayList;
public class TierDemo {
public static void main(String[] args) {
System.out.println("Test Teil 2:");
DogSchool schule = new DogSchool();
schule.add(new Hund());
schule.add(new Katze());
schule.add(new Hund());
schule.add(new Katze());
schule.addAll(tiere);
for (Tier t : schule.getTiere()) {
System.out.println(t.gibLaut());
}
}
编译后显示:
Test Teil 2:
Wau! Wau!
Wau! Wau!
Miau!
Wau! Wau!
哪个更好,但还是分不清猫狗
你有很多错误。需要注意的主要事情是,您应该初始化层列表并在所有方法中使用它,而不是在每个方法中创建新的 ArrayList。
public ArrayList<Tier> tiere; // you forgot to initialize this ArrayList
@Override
public boolean add(Tier t){
ArrayList<Tier> neu= new ArrayList<Tier>();
if(t.gibLaut().equalsIgnoreCase("Wau! Wau!")){
return neu.add(t); // this list is local to the method, you should be adding to tiere
}
else {
return false;
} }
@ Override
public boolean addAll(ArrayList<Tier> tiere){
ArrayList<Tier> neu= new ArrayList<Tier>(); // remove this list
return tiere.addAll(neu); // should be this.tiere.addAll(tiere);
}
@Override
public boolean remove(Tier t){
ArrayList<Tier> neu= new ArrayList<Tier>(tiere.size()); // remove this
if(!t.gibLaut().equalsIgnoreCase("Wau! Wau!")){
return neu.remove(t); // should remove from tiere
}
else{
return false;
}
}
@Override
public ArrayList<Tier> getTiere() {
return new ArrayList<Tier>(); // should either return tiere or a copy of it (i.e. new ArrayList<Tier>(tiere))
}
基本上你的代码一切都错了。 None 的实施修改了您的层级。另外测试动物的声音可能不是最安全的检查方法。
假设你有一只狗class
class Dog implements Tier {
}
尽管您的描述缺少一些细节,这可能有效。
public class DogSchool implements PetSchool {
// make your internal list private to prevent unqualidfied modification
private ArrayList<Tier> dogs = new ArrayList<>();
public boolean add(Tier tier) {
// check the type of the animal and add it to your internal list
if (tier instanceof Dog) {
return this.dogs.add(tier);
}
return false;
}
public boolean addAll(ArrayList<Tier> tiere) {
// only add the dogs if every animal in the list is a dog
for (Tier t: tiere) {
if (!(t instanceof Dog))
return false;
}
return this.dogs.addAll(tiere);
}
public boolen remove(Tier tier) {
return this.dogs.remove(tier);
}
public ArrayList<Tier> getTiere() {
// return a copy so no one can modify your internal list
return new ArrayList<>(this.dogs);
}
}
我在做一个练习,需要DogSchool来实现PetSchool。 我打算做一个在宠物学校注册的动物的数组列表,狗学校需要区分狗和其他动物。狗的特征就是它们的叫声"Wau! Wau!"。 我已经更正了。但是还是分不清猫狗
等级 = 动物
这是接口代码
import java.util.ArrayList;
public interface PetSchool {
boolean add(Tier tier);
boolean addAll(ArrayList<Tier> tiere);
boolean remove(Tier tier);
ArrayList<Tier> getTiere();
}
This is the code of Implementation.
Please tell me what's wrong with it.
import java.util.ArrayList;
public class DogSchool implements PetSchool {
public ArrayList<Tier> tiere= new ArrayList<Tier>();
@Override
public boolean add(Tier t){
if(t.gibLaut().equalsIgnoreCase("Wau! Wau!")){
return tiere.add(t);
}
else {
return false;
} }
@ Override
public boolean addAll(ArrayList<Tier> tiere){
return this.tiere.addAll(tiere);
}
@Override
public boolean remove(Tier t){
if(!t.gibLaut().equalsIgnoreCase("Wau! Wau!")){
return tiere.remove(t);
}
else{
return false;
}
}
@Override
public ArrayList<Tier> getTiere() {
return new ArrayList<Tier>(this.tiere);
}
}
嗯,问题出现在demoTest中:
import java.util.ArrayList;
public class TierDemo {
public static void main(String[] args) {
System.out.println("Test Teil 2:");
DogSchool schule = new DogSchool();
schule.add(new Hund());
schule.add(new Katze());
schule.add(new Hund());
schule.add(new Katze());
schule.addAll(tiere);
for (Tier t : schule.getTiere()) {
System.out.println(t.gibLaut());
}
}
编译后显示:
Test Teil 2:
Wau! Wau!
Wau! Wau!
Miau!
Wau! Wau!
哪个更好,但还是分不清猫狗
你有很多错误。需要注意的主要事情是,您应该初始化层列表并在所有方法中使用它,而不是在每个方法中创建新的 ArrayList。
public ArrayList<Tier> tiere; // you forgot to initialize this ArrayList
@Override
public boolean add(Tier t){
ArrayList<Tier> neu= new ArrayList<Tier>();
if(t.gibLaut().equalsIgnoreCase("Wau! Wau!")){
return neu.add(t); // this list is local to the method, you should be adding to tiere
}
else {
return false;
} }
@ Override
public boolean addAll(ArrayList<Tier> tiere){
ArrayList<Tier> neu= new ArrayList<Tier>(); // remove this list
return tiere.addAll(neu); // should be this.tiere.addAll(tiere);
}
@Override
public boolean remove(Tier t){
ArrayList<Tier> neu= new ArrayList<Tier>(tiere.size()); // remove this
if(!t.gibLaut().equalsIgnoreCase("Wau! Wau!")){
return neu.remove(t); // should remove from tiere
}
else{
return false;
}
}
@Override
public ArrayList<Tier> getTiere() {
return new ArrayList<Tier>(); // should either return tiere or a copy of it (i.e. new ArrayList<Tier>(tiere))
}
基本上你的代码一切都错了。 None 的实施修改了您的层级。另外测试动物的声音可能不是最安全的检查方法。
假设你有一只狗class
class Dog implements Tier {
}
尽管您的描述缺少一些细节,这可能有效。
public class DogSchool implements PetSchool {
// make your internal list private to prevent unqualidfied modification
private ArrayList<Tier> dogs = new ArrayList<>();
public boolean add(Tier tier) {
// check the type of the animal and add it to your internal list
if (tier instanceof Dog) {
return this.dogs.add(tier);
}
return false;
}
public boolean addAll(ArrayList<Tier> tiere) {
// only add the dogs if every animal in the list is a dog
for (Tier t: tiere) {
if (!(t instanceof Dog))
return false;
}
return this.dogs.addAll(tiere);
}
public boolen remove(Tier tier) {
return this.dogs.remove(tier);
}
public ArrayList<Tier> getTiere() {
// return a copy so no one can modify your internal list
return new ArrayList<>(this.dogs);
}
}