我有三个类,Organism
,Creature
和Plant
。 Creature
和Plant
都扩展Organism
。现在,当所有内容都捆绑到Organism
中时,此代码可以工作,因此我认为GUI类没有问题。基本上,所发生的只是生成了Creature
个特定的统计信息,而忽略了Organism
个统计信息,返回了0
。我找不到任何问题,所以我想你们可以帮忙。 (忽略混乱,我仍在玩耍。)
生物类
import java.awt.Color;
import java.util.Random;
public class Creature extends Organism
{
private char gender;
private int strength, speed, diet, aggression;
private int mated, maxMated;
public Creature()
{
super();
Random rand = new Random();
if(rand.nextInt(2) == 0)
{
gender = 'f';
}
else
{
gender = 'm';
}
mated = 0;
setStats();
}
public Creature(String gen[])
{
super(gen);
Random rand = new Random();
if(rand.nextInt(2) == 0)
{
gender = 'f';
}
else
{
gender = 'm';
}
x = rand.nextInt(maxX);
y = rand.nextInt(maxY);
setStats();
}
public Creature(String gen[], int newX, int newY)
{
super(gen, newX, newY);
this.gene = gen;
Random rand = new Random();
if(rand.nextInt(2) == 0)
{
gender = 'f';
}
else
{
gender = 'm';
}
isAlive = true;
x = newX;
y = newY;
setStats();
}
public int getAggro()
{
return aggression;
}
public int getDiet()
{
return diet;
}
public int getSpeed()
{
return speed;
}
public void setStats()
{
strength = (gene[1].charAt(0)-48) + 1 + ((gene[1].charAt(1)-48) * (gene[1].charAt(2)-48));
speed = (strength + (gene[2].charAt(0)-48) - (gene[2].charAt(1)-48) + (gene[2].charAt(2)-48))/(size + 1);
diet = (gene[7].charAt(0)-48) + 1 + ((gene[7].charAt(1)-48) * (gene[7].charAt(2)-48)*(3/4));
aggression = ((strength + size)/2) / (gene[8].charAt(0)-48 + gene[8].charAt(1)-48 + gene[8].charAt(2)-48 + 1);
maxHealth = 64 + size + (strength / 2);
maxHunger = 100 + (size - speed - aggression);
health = maxHealth;
hunger = maxHunger;
}
public Creature breed(Creature mate)
{
Random rand = new Random();
int x = rand.nextInt(gene.length);
int y = rand.nextInt(gene[x].length());
String geneY[] = new String[16];
int i;
for(i = 0; i < x; i++)
{
geneY[i] = gene[i];
}
geneY[x] = gene[x].substring(0,y);
geneY[x] = geneY[x] + mate.gene[x].substring(y);
for(i = x + 1; i < 16; i++)
{
geneY[i] = mate.gene[i];
}
char newGender;
if(rand.nextInt(2) == 0)
{
newGender = 'f';
}
else
{
newGender = 'm';
}
hunger = hunger /2;
mate.hunger = mate.hunger /2;
mated = mated + 1;
mate.mated = mate.mated + 1;
Creature temp = new Creature(geneY,this.getX(),this.getY());
temp.mutate();
return temp;
}
public void eat(Organism b) //A eats B
{
b.isAlive = false;
this.hunger = this.hunger + b.size;
}
public boolean isCompatible(Creature org)
{
int differences = 0;
for(int i = 0; i < this.gene.length; i++)
{
if(!this.gene[i].equals(org.gene[i]))
{
differences = differences + 1;
}
}
if(differences > 1 || this.gender == org.gender || mated == maxMated || org.mated == org.maxMated)
{
return false;
}
return true;
}
public void moveTo(Organism org)
{
int vectX, vectY, moveX = 0, moveY = 0;
double angle;
vectX = this.x - org.x;
vectY = this.y - org.y;
if(vectX == 0)
{
moveY = this.speed;
}
if(vectY == 0)
{
moveX = this.speed;
}
if(vectX == 0 && vectY == 0)
{
moveX = 0;
moveY = 0;
}
if(vectX != 0 && vectY != 0)
{
angle = ((Math.atan((vectY)/(vectX)))/(2*Math.PI))*360;
if(angle < 0)
{
angle = angle * - 1;
}
moveX = (int)(Math.sin(angle)*this.speed);
moveY = (int)(Math.cos(angle)*this.speed);
}
if(Math.sqrt((vectX*vectX)+(vectY*vectY)) < speed)
{
if(vectX > 0)
{
this.x = this.x - vectX;
}
else
{
this.x = this.x + vectX;
}
if(vectY > 0)
{
this.y = this.y - vectY;
}
else
{
this.y = this.y + vectY;
}
}
else
{
if(vectX > 0)
{
this.x = this.x - moveX;
}
else
{
this.x = this.x + moveX;
}
if(vectY > 0)
{
this.y = this.y - moveY;
}
else
{
this.y = this.y + moveY;
}
}
}
}
生物课
import java.awt.Color;
import java.util.Random;
public class Organism
{
protected String gene[] = new String[16];
protected boolean isAlive;
protected int x;
protected int y;
protected int size, sense, fertility, scent;
protected int health, hunger, maxHealth, maxHunger;
protected int maxX = 1000;
protected int maxY = 1000;
private Color color = new Color(255,0,0);
public Organism()
{
Random rand = new Random();
for(int i = 0; i < 16; i++)
{
gene[i] = ""+ rand.nextInt(4) + rand.nextInt(4) + rand.nextInt(4);
}
isAlive = true;
x = rand.nextInt(maxX);
y = rand.nextInt(maxY);
setStats();
}
public Organism(String gen[])
{
Random rand = new Random();
this.gene = gen;
isAlive = true;
x = rand.nextInt(maxX);
y = rand.nextInt(maxY);
setStats();
}
public Organism(String gen[], int newX, int newY)
{
this.gene = gen;
isAlive = true;
x = newX;
y = newY;
setStats();
}
public Color getColor()
{
return color;
}
public int getX()
{
return x;
}
public void setX(int tempX)
{
this.x = tempX;
}
public int getY()
{
return y;
}
public void setY(int tempY)
{
this.y = tempY;
}
public int getHunger()
{
return hunger;
}
public void setHunger(int hun)
{
this.hunger = hun;
}
public int getHealth()
{
return health;
}
public void setHealth(int heal)
{
this.health = heal;
}
public void setStats()
{
size = (gene[0].charAt(0)-48) + 1 + ((gene[0].charAt(1)-48) * (gene[0].charAt(2)-48));
sense = (gene[5].charAt(2)-48) + 1 + ((gene[5].charAt(1)-48) * (gene[5].charAt(2)-48));
fertility = 22 - size + (gene[6].charAt(0)-48) + 1 + ((gene[6].charAt(1)-48) * (gene[6].charAt(2)-48));
scent = (gene[8].charAt(0)-48 + gene[8].charAt(1)-48 + gene[8].charAt(2)-48);
}
public int getSize()
{
return size;
}
public int getSense()
{
return sense;
}
public boolean getAlive()
{
return isAlive;
}
public void setAlive(boolean live)
{
this.isAlive = live;
}
public String getInfo()
{
String info;
info = "Health: " + this.health + ", Hunger: "+ this.hunger + ", Status: " + this.isAlive;
return info;
}
/*public String getStats()
{
String info = "Size: " + this.size + ", Strength: " + this.strength +
", Speed: " + this.speed + ", Sight: " + this.sight +
", Smell: " + this.smell + ", Hearing: " + this.hearing +
", Fertility: " + this.fertility + ", Diet: " + this.diet +
", Aggression: " + this.aggression + ", Scent: " + this.scent;
return info;
}*/
public String displayGene()
{
String geneP = "|";
for(int i = 0; i < gene.length; i++)
{
for(int j = 0; j < gene[i].length(); j++)
{
switch (gene[i].charAt(j))
{
case '0': geneP = geneP + 'A';
break;
case '1': geneP = geneP + 'T';
break;
case '2': geneP = geneP + 'C';
break;
case '3': geneP = geneP + 'G';
break;
}
}
geneP = geneP + "|";
}
return geneP;
}
public void mutate()
{
Random rand = new Random();
int i = rand.nextInt(10000) + 1;
int affected;
if(i > 9999)
{
affected = rand.nextInt(gene.length);
i = rand.nextInt(gene[affected].length());
int j = rand.nextInt(4);
gene[affected] = gene[affected].substring(0,i)+j+gene[affected].substring(i+1);
}
}
public void hungerCheck()
{
hunger = hunger - (size / 10 + 1);
if(hunger <= 0)
{
health = health - 1;
}
if(hunger > (maxHunger * 3)/4)
{
health = health + 1;
}
}
public void validate()
{
if(x > maxX)
{
x = 100;
}
if(x < 0)
{
x = 0;
}
if(y > maxY)
{
y = 100;
}
if(y < 0)
{
y = 0;
}
if(hunger > maxHunger)
{
hunger = maxHunger;
}
if(hunger <= 0)
{
hunger = 0;
}
if(health <= 0)
{
isAlive = false;
}
if(health > maxHealth)
{
health = maxHealth;
}
}
}
最佳答案
您必须从Creature.setStats()
方法调用super方法
public class Creature extends Organism
public void setStats()
{
super.setStats(); // Call to Organism.setStats()
...
}
....
}
关于java - 扩展类似乎没有继承一些统计信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20748356/