从 2 类 继承不同的布尔值
Inheriting different booleans from 2 classes
我建立了一个犬舍系统,使用 .txt 文件将狗登记到系统中,允许您执行各种操作,例如搜索、删除、全部打印等。
我目前正在使用继承来为猫添加功能,因为它们共享许多相同的变量(名称、最喜欢的食物等)。
我使用的是狗和猫特有的布尔值,狗是 "does the dog like bones",猫是 "can cats share a run"。
我 运行 遇到了使用继承的问题,因为这两个布尔值不相同(显然)并且在代码中使用了不同的 variables/identifiers (不确定这里的术语是否正确,如果可能,请告诉我)它会抛出错误。
这是我目前所拥有的
import java.util.ArrayList;
public class Pet {
protected ArrayList<Owner> originalOwners;
//private boolean likesBones;
protected String petName;
protected String favFood;
protected int foodPerDay;
public Pet(String name, ArrayList<Owner> owners, String food, int mealsPerDay) {
petName = name;
this.favFood = food;
this.foodPerDay = mealsPerDay;
originalOwners = new ArrayList<Owner>();
for(Owner o: owners){
Owner copy = new Owner(o.getName(), o.getPhone());
originalOwners.add(copy);
}
}
public String getName() {
return petName;
}
public void setName(String newName) {
petName = newName;
}
/**
* Returns a copy of the original owners
* @return A copy of the original owners as an array
*/
public Owner[] getOriginalOwners(){
Owner[] result = new Owner[originalOwners.size()];
result = originalOwners.toArray(result);
return result;
}
/**
* How many times a day to feed the dog
* @param feeds The number of feeds per day
*/
public void setFeedsPerDay(int feeds){
foodPerDay = feeds;
}
/**
* The number of feeds per day the dog is fed
* @return The number of feeds per day
*/
public int getFeedsPerDay(){
return foodPerDay;
}
/**
* What's his favourite food?
* @param food The food he likes
*/
public void setFavouriteFood(String food){
favFood = food;
}
/**
* The food the dog likes to eat
* @return The food
*/
public String getFavouriteFood(){
return favFood;
}
/**
* Note that this only compares equality based on a
* dog's name.
* @param The other dog to compare against.
*/
@Override
public boolean equals(Object obj) { // Generated by Eclipse to be more robust
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Dog other = (Dog) obj;
if (petName == null) {
if (other.petName != null)
return false;
} else if (!petName.equals(other.petName))
return false;
return true;
}
/**
* A basic implementation to just return all the data in string form
*/
public String toString() {
return "Dog name:" + petName + "Original Owner:" + originalOwners + "Favfood:" + favFood
+ "FoodPerDay:" + foodPerDay;
}
}
狗class:
import java.util.ArrayList;
public class Dog extends Pet {
public static boolean likesBones;
public Dog(String name, ArrayList<Owner> owners, String food, int mealsPerDay, boolean likeBones) {
super(name, owners, food, mealsPerDay);
Dog.likesBones = likeBones;
}
/**
* Does the dog like bones?
* @return true if he does
*/
public static boolean getLikesBones() {
return likesBones;
}
}
猫class
import java.util.ArrayList;
public class Cat extends Pet {
public static boolean shareRun;
public Cat(String name, ArrayList<Owner> owners, String food, int mealsPerDay, boolean shareRun) {
super(name, owners, food, mealsPerDay);
Cat.shareRun = shareRun;
}
public boolean getShareRun(boolean doesShare) {
return shareRun;
}
}
我无法将 likeBones 和 shareRun 的布尔值都添加到 Pet() 中,因为它们来自不同的 classes,所以如果我尝试添加狗,我会得到一个空点例外(我相信),反之亦然。
我的问题:如何在 Pet() class 中同时支持猫和狗的布尔值而不抛出异常?
请让我知道这个问题是否有意义,我之前在解释我的问题时遇到了一些麻烦(编程新手)。
提前谢谢大家。
因为 likebones 是狗特有的,所以它应该只存在于狗 class 中,而不应该存在于父宠物 class 中。同样,shareRun 应该只存在于 Cat class,而不是父 Pet class.
从逻辑上考虑结构。父 class 应该包含其子 all 使用的信息 - 它的重点是允许共享功能。未共享的功能 不属于 旨在帮助共享功能的地方。
如果您需要为 Pet 的任何子级使用该布尔值的函数,您可以做的是在父级中定义一个泛型 boolean special
并实现使用它的函数,然后在其他地方跟踪每种类型的宠物 special
是什么 - 但由于不了解您的系统,我不确定它是否最适合您。
嘿,我想你可能想强制 Dog 和 Cat 设置在 Pet 中声明的布尔值。或许在Pet中做一个抽象方法,然后放到Pet的构造函数中。
class abstract Pet {
...
public Pet() {
...
setRandomBoolean();
}
...
protected abstract void setRandomBoolean();
...
}
很明显,阅读这段代码,属性喜欢骨头(什么狗不喜欢?)和分享跑步太抽象和无关了。
就像BIU说的,他们应该有完全独立的实现。
您的 Cat class 设置几乎正确,带有私有 shareRun 字段和 public getter 和 setter。但为什么这个 class 也有一个 public 静态 shareRun 字段?
你的狗 class 在 likesBones 方面应该遵循与猫相同的模式。目前,您将其设置为静态 属性 这是错误的。
如果您在尝试执行类似
的操作时遇到错误
Pet fido = new Pet();
dodo.setLikesBones(true);
您将遇到编译器错误,因为 Pet 没有声明该方法。
您可以在 Pet 上声明这些字段和方法,但是猫狗都可以选择喜欢骨头并能够共享 运行。
使用基础 class 共享共同属性,使用子 class 区分。
这有意义吗?
我建立了一个犬舍系统,使用 .txt 文件将狗登记到系统中,允许您执行各种操作,例如搜索、删除、全部打印等。
我目前正在使用继承来为猫添加功能,因为它们共享许多相同的变量(名称、最喜欢的食物等)。
我使用的是狗和猫特有的布尔值,狗是 "does the dog like bones",猫是 "can cats share a run"。
我 运行 遇到了使用继承的问题,因为这两个布尔值不相同(显然)并且在代码中使用了不同的 variables/identifiers (不确定这里的术语是否正确,如果可能,请告诉我)它会抛出错误。
这是我目前所拥有的
import java.util.ArrayList;
public class Pet {
protected ArrayList<Owner> originalOwners;
//private boolean likesBones;
protected String petName;
protected String favFood;
protected int foodPerDay;
public Pet(String name, ArrayList<Owner> owners, String food, int mealsPerDay) {
petName = name;
this.favFood = food;
this.foodPerDay = mealsPerDay;
originalOwners = new ArrayList<Owner>();
for(Owner o: owners){
Owner copy = new Owner(o.getName(), o.getPhone());
originalOwners.add(copy);
}
}
public String getName() {
return petName;
}
public void setName(String newName) {
petName = newName;
}
/**
* Returns a copy of the original owners
* @return A copy of the original owners as an array
*/
public Owner[] getOriginalOwners(){
Owner[] result = new Owner[originalOwners.size()];
result = originalOwners.toArray(result);
return result;
}
/**
* How many times a day to feed the dog
* @param feeds The number of feeds per day
*/
public void setFeedsPerDay(int feeds){
foodPerDay = feeds;
}
/**
* The number of feeds per day the dog is fed
* @return The number of feeds per day
*/
public int getFeedsPerDay(){
return foodPerDay;
}
/**
* What's his favourite food?
* @param food The food he likes
*/
public void setFavouriteFood(String food){
favFood = food;
}
/**
* The food the dog likes to eat
* @return The food
*/
public String getFavouriteFood(){
return favFood;
}
/**
* Note that this only compares equality based on a
* dog's name.
* @param The other dog to compare against.
*/
@Override
public boolean equals(Object obj) { // Generated by Eclipse to be more robust
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Dog other = (Dog) obj;
if (petName == null) {
if (other.petName != null)
return false;
} else if (!petName.equals(other.petName))
return false;
return true;
}
/**
* A basic implementation to just return all the data in string form
*/
public String toString() {
return "Dog name:" + petName + "Original Owner:" + originalOwners + "Favfood:" + favFood
+ "FoodPerDay:" + foodPerDay;
}
}
狗class:
import java.util.ArrayList;
public class Dog extends Pet {
public static boolean likesBones;
public Dog(String name, ArrayList<Owner> owners, String food, int mealsPerDay, boolean likeBones) {
super(name, owners, food, mealsPerDay);
Dog.likesBones = likeBones;
}
/**
* Does the dog like bones?
* @return true if he does
*/
public static boolean getLikesBones() {
return likesBones;
}
}
猫class
import java.util.ArrayList;
public class Cat extends Pet {
public static boolean shareRun;
public Cat(String name, ArrayList<Owner> owners, String food, int mealsPerDay, boolean shareRun) {
super(name, owners, food, mealsPerDay);
Cat.shareRun = shareRun;
}
public boolean getShareRun(boolean doesShare) {
return shareRun;
}
}
我无法将 likeBones 和 shareRun 的布尔值都添加到 Pet() 中,因为它们来自不同的 classes,所以如果我尝试添加狗,我会得到一个空点例外(我相信),反之亦然。
我的问题:如何在 Pet() class 中同时支持猫和狗的布尔值而不抛出异常?
请让我知道这个问题是否有意义,我之前在解释我的问题时遇到了一些麻烦(编程新手)。 提前谢谢大家。
因为 likebones 是狗特有的,所以它应该只存在于狗 class 中,而不应该存在于父宠物 class 中。同样,shareRun 应该只存在于 Cat class,而不是父 Pet class.
从逻辑上考虑结构。父 class 应该包含其子 all 使用的信息 - 它的重点是允许共享功能。未共享的功能 不属于 旨在帮助共享功能的地方。
如果您需要为 Pet 的任何子级使用该布尔值的函数,您可以做的是在父级中定义一个泛型 boolean special
并实现使用它的函数,然后在其他地方跟踪每种类型的宠物 special
是什么 - 但由于不了解您的系统,我不确定它是否最适合您。
嘿,我想你可能想强制 Dog 和 Cat 设置在 Pet 中声明的布尔值。或许在Pet中做一个抽象方法,然后放到Pet的构造函数中。
class abstract Pet {
...
public Pet() {
...
setRandomBoolean();
}
...
protected abstract void setRandomBoolean();
...
}
很明显,阅读这段代码,属性喜欢骨头(什么狗不喜欢?)和分享跑步太抽象和无关了。
就像BIU说的,他们应该有完全独立的实现。
您的 Cat class 设置几乎正确,带有私有 shareRun 字段和 public getter 和 setter。但为什么这个 class 也有一个 public 静态 shareRun 字段?
你的狗 class 在 likesBones 方面应该遵循与猫相同的模式。目前,您将其设置为静态 属性 这是错误的。
如果您在尝试执行类似
的操作时遇到错误Pet fido = new Pet();
dodo.setLikesBones(true);
您将遇到编译器错误,因为 Pet 没有声明该方法。
您可以在 Pet 上声明这些字段和方法,但是猫狗都可以选择喜欢骨头并能够共享 运行。
使用基础 class 共享共同属性,使用子 class 区分。
这有意义吗?