Java-数组的子类
免责声明:我是一个初学者,所以请随意指出 我有一个由8个值的int数组组成的超类,现在我想创建一个子类,在数组中随机选取4个项,并将它们存储在另一个对象中 超类:Java-数组的子类,java,arrays,Java,Arrays,免责声明:我是一个初学者,所以请随意指出 我有一个由8个值的int数组组成的超类,现在我想创建一个子类,在数组中随机选取4个项,并将它们存储在另一个对象中 超类: public class SideDeck{ public static final int MaxValue = 6; public static final int MinValue = -6; public static final int MaxArrayValue = 8; public fi
public class SideDeck{
public static final int MaxValue = 6;
public static final int MinValue = -6;
public static final int MaxArrayValue = 8;
public final int[] sidecards = new int[MaxArrayValue];
public SideDeck(){
for(int i=0;i<MaxArrayValue;i++){
sidecards[i]=0;
}
}
public SideDeck(int sidecards1,int sidecards2,int sidecards3,int sidecards4,int sidecards5,int sidecards6, int sidecards7, int sidecards8){
sidecards[0]=sidecards1;
sidecards[1]=sidecards2;
sidecards[2]=sidecards3;
sidecards[3]=sidecards4;
sidecards[4]=sidecards5;
sidecards[5]=sidecards6;
sidecards[6]=sidecards7;
sidecards[7]=sidecards8;
}
public boolean ValidSidedeck(){
int check=0;
if (sidecards[0]!=0) {
for(int i=0;i<MaxArrayValue;i++){
if ((sidecards[i] > MinValue) && (sidecards[i] < MaxValue)){
check=1;
} else{
check=0;
break;
}
}
} else {
check=0;
}
if (check==1){
return true;
} else {
return false;
}
}
public String toString(){
String s="";
for(int i=0;i<MaxArrayValue;i++){
s+=(" || Card n° " + (i+1) + " = " + sidecards[i]);
}
return s;
}
public void ResetSidedeck(){
if (sidecards[0]!=0) {//why check it? what if we just run it?
for(int i=0;i<MaxArrayValue;i++){
sidecards[i]=0;
}
}
}
}
公共类侧面板{
公共静态最终int MaxValue=6;
公共静态final int MinValue=-6;
公共静态最终int MaxArrayValue=8;
公共最终整数[]边卡=新整数[MaxArrayValue];
公共侧甲板(){
对于(int i=0;i我不确定您打算如何使用PlayableSideck
,因此我将用两种方法回答这个问题,您可以选择最合适的答案
首先,作为《有效的Java》(Josh Bloch著)指出,你应该倾向于组合而不是继承。通过使用组合,你可以回答是否应该将SideDeck
的实例传递给playablesideck
的构造函数的问题-你必须这样做,因为你不会继承对sideck
的任何访问权。无论如何,我建议你阅读书中的第16项(谷歌搜索,网上有副本),看看构图是否更适合你的需要
其次,如果您决定使用继承,则不需要将SideDeck
的实例传递给playablesideck
的构造函数。这是因为当您创建playablesideck
的实例时,您会自动创建sideck
的实例如果您自己没有显式提供另一个这样的调用,则将隐式调用super()
(这是超类的默认构造函数)。例如,您可以像这样阻止对super()的隐式调用:
在本例中,如果调用new子类(“foobar”)
,则会看到控制台上打印的strValue=foobar
如果BaseClass
没有零参数构造函数,事实上,您需要调用super(str)
,因为编译器无法为您找到执行方法
另外,既然你问了,这里还有一些其他的提示和建议:
在构造函数SideDeck()
中,您显式地将数组的所有值初始化为0,这是不必要的。它们都将是0。如果您需要将它们初始化为0,则最好通过调用resetsideck
来避免代码重复。说到这里,您可以将代码缩短为Arrays.fill(sidecards,0);
(请确保导入java.util.array)
是的,您可以从构造函数调用私有方法,但只能调用属于本地类的私有方法,而不能调用任何超类(但是,您可以调用超类的受保护方法)
您没有检查sidecards[0]==0
是对的,因为除非MaxArrayValue
变得非常大,否则效率很低
您的类成员变量(如sidecards
)应该是私有的(或者如果需要从子类访问它们,可能会受到保护)。使用getter/setter方法访问它们
最后,Java命名约定将告诉您使用小写字母作为方法名称(例如setDeck、pickDeck、resetDeck等),对于更惯用的Java,您可以将ValidDeck重命名为isValidDeck(因为它返回布尔值)。对于MaxArrayValue等常量,惯例是在单词之间使用带下划线的所有大写字母,例如MAX_ARRAY_VALUE
希望这些都能有所帮助!使用数组将其转换为列表。asList()
现在您可以使用整个列表,包括Collections.shuffle方法。感谢您提供的提示和答案,我将阅读这本书的这一部分并尝试实现您的建议。
import java.lang.Math;
public final class PlayableSideDeck extends SideDeck{
private final static int MaxCArrayValue=4;
public final int[] sidecardsPlay = new int[MaxCArrayValue];
public PlayableSideDeck(SideDeck sidecards){
/* sidecardsPlay[0]=0;
sidecardsPlay[1]=0;
sidecardsPlay[2]=0;
sidecardsPlay[3]=0;*/
// SetDeck();//<-Can i call a private method in the constructor
}
public void SetDeck(){
/* for(int j=0;j<4;j++){
int position=(super.sidecards[PickDeck()]);//<--this is the main problem.. since it's gonna call the object i guess.
sidecards[j]=position;
System.out.println(/*"i= " + i + *//* " ||| j= " + j + "|||| new sidecard= " + sidecards[j] + " |||| old sidecard=" + super.sidecards[PickDeck()]);
}*/
for(int j=0;j<MaxCArrayValue;j++){
sidecardsPlay[j]=(super.sidecards[PickDeck()]);
System.out.println(/*"i= " + i + */ " ||| j= " + j + "|||| new sidecard= " + sidecardsPlay[j] + " |||| old sidecard=" + super.sidecards[PickDeck()] + "|| random= " + PickDeck());
}
}
public int PickDeck(){
return ((int)(Math.random() * 8));
}
public String toString(){
String s="";
for(int i=0;i<MaxCArrayValue;i++){
s+=(" || Card n° " + (i+1) + " = " + sidecards[i]);
}
return s;
}
}
public class BaseClass {
protected String strValue;
public BaseClass () {
strValue = "";
}
public BaseClass (String str) {
strValue = str;
}
}
public class SubClass extends BaseClass {
private int intValue;
SubClass (String str, int i) {
super (str);
intValue = i;
// note that since strValue is protected, SubClass can access directly
System.out.println ("strValue = " + strValue);
}
}