Java抽象字段模式
我不确定如何描述我想要的这种模式,但我想这样:Java抽象字段模式,java,oop,abstract,Java,Oop,Abstract,我不确定如何描述我想要的这种模式,但我想这样: public abstract class Parent { protected abstract boolean foo = false; //this doesn't compile } public class Child1 extends Parent { protected boolean foo = true; } 我该怎么做 假设我有一个父类类,但有20个子类类。对于绝大多数孩子来说,foo应该是false。然而,
public abstract class Parent {
protected abstract boolean foo = false; //this doesn't compile
}
public class Child1 extends Parent {
protected boolean foo = true;
}
我该怎么做
假设我有一个父类
类,但有20个子类
类。对于绝大多数孩子来说,foo
应该是false
。然而,Child1
(以及其他一些)是带有foo=true的奇怪的一个代码>
最合适的OO设计和最有效的编码方式是什么?如果要使用Child1
类扩展父类
类,必须键入:
public class Child1 extends Parent {
}
关于foo
参数,您不能设置它abstract
,因为它不是函数(也就是说,只能声明函数abstract
)。但是,您可以在子类中重写它
public abstract class Parent {
protected boolean foo = false;
}
public class Child1 extends Parent {
@Override
protected boolean foo = true;
}
如果要使用Child1
类扩展Parent
类,必须键入:
public class Child1 extends Parent {
}
关于foo
参数,您不能设置它abstract
,因为它不是函数(也就是说,只能声明函数abstract
)。但是,您可以在子类中重写它
public abstract class Parent {
protected boolean foo = false;
}
public class Child1 extends Parent {
@Override
protected boolean foo = true;
}
首先,实例变量不能是抽象的,只有方法可以
要具有重写行为,您需要方法。我将在Parent
中定义一个方法,例如,isFoo
,该方法被定义为返回false
。没有子类需要重写它,除了“怪异的一个”(s),它可以重写它以返回true
或者,您可以有一个子类Parent
,名为WeirdOne
(当然不必是该名称)。它所做的唯一事情就是重写isFoo
以返回true
。然后是Child1
和任何其他“怪异”类的子类WeirdOne
。这样,它只在一个地方被重写。首先,实例变量不能是抽象的,只有方法可以
要具有重写行为,您需要方法。我将在Parent
中定义一个方法,例如,isFoo
,该方法被定义为返回false
。没有子类需要重写它,除了“怪异的一个”(s),它可以重写它以返回true
或者,您可以有一个子类Parent
,名为WeirdOne
(当然不必是该名称)。它所做的唯一事情就是重写isFoo
以返回true
。然后是Child1
和任何其他“怪异”类的子类WeirdOne
。这样,它只在一个位置被重写。您可以使用一两个构造函数来实现这一点:
public abstract class Parent {
protected boolean foo;
protected Parent() {
this(false); // initialize foo to default value
}
protected Parent(boolean fooValue) {
this.foo = fooValue;
}
}
public class Child1 extends Parent {
public Child1() {
super(true);
}
}
public class Child2 extends Parent {
// no explicit super(boolean) call in c'tor gives foo the default value
}
您可以使用一个或两个构造函数执行此操作:
public abstract class Parent {
protected boolean foo;
protected Parent() {
this(false); // initialize foo to default value
}
protected Parent(boolean fooValue) {
this.foo = fooValue;
}
}
public class Child1 extends Parent {
public Child1() {
super(true);
}
}
public class Child2 extends Parent {
// no explicit super(boolean) call in c'tor gives foo the default value
}
那么不要使用字段。看看这些类的组合:
public abstract class Vehicle {
public abstract boolean isAerial();
}
public abstract Flyer extends Vehicle {
@Override
public final boolean isAerial() {
return true;
}
}
// Add Airplane, Helicopter, Dirigible, Rocket, etc.
public abstract Terrestrial extends Vehicle {
@Override
public final boolean isAerial() {
return false;
}
}
// Add Car, Truck, Boat, Bicycle, etc.
那么不要使用字段。看看这些类的组合:
public abstract class Vehicle {
public abstract boolean isAerial();
}
public abstract Flyer extends Vehicle {
@Override
public final boolean isAerial() {
return true;
}
}
// Add Airplane, Helicopter, Dirigible, Rocket, etc.
public abstract Terrestrial extends Vehicle {
@Override
public final boolean isAerial() {
return false;
}
}
// Add Car, Truck, Boat, Bicycle, etc.
我认为你需要这样做
public abstract class Parent {
protected boolean check = false;
}
public class Child extends Parent
{
public void method()
{
this.check=true;
}
}
//你也可以把它放在构造函数中我想你需要这样做
public abstract class Parent {
protected boolean check = false;
}
public class Child extends Parent
{
public void method()
{
this.check=true;
}
}
//您也可以将其放入构造函数中您忘记了一个类型,boolean
。而且,只有方法和类可以是抽象的。还有,在构造函数中这样做。这有什么意义?你不能在创建子类时将其初始化为正确的值吗?@SotiriosDelimanolis噢,thanx用于捕获。是的,因此我在评论中写了它,它没有编译。对构造函数部分的要求很好。@Jeroenvanevel如果我的问题可能让人困惑,很抱歉,但你有什么建议?我想知道是否有一种面向对象的方式来实现我想要描述的内容。基本上,约束只是少数子类应该对一个公共/可重写变量执行不同的操作。您忘记了一个类型,boolean
。而且,只有方法和类可以是抽象的。还有,在构造函数中这样做。这有什么意义?你不能在创建子类时将其初始化为正确的值吗?@SotiriosDelimanolis噢,thanx用于捕获。是的,因此我在评论中写了它,它没有编译。对构造函数部分的要求很好。@Jeroenvanevel如果我的问题可能让人困惑,很抱歉,但你有什么建议?我想知道是否有一种面向对象的方式来实现我想要描述的内容。基本上,约束只是少数子类应该对公共/可重写变量执行不同的操作。这不会更改Parent.foo
变量。编译器将拒绝您的批注。这不会更改Parent.foo
变量。你的注释将被编译器拒绝。我真的很喜欢这样-你在子构造函数中强制变量初始值。我真的很喜欢这样-你在子构造函数中强制变量初始值。