所以我需要在这段代码中有一个继承的例子,我试图得到它,所以我在父类中实例化private BufferedImage image
,然后让它在子类中采用不同的形式,如玩家,敌人,可能还有钥匙。他们每个人都有自己的特定形象(这也是多态性的一个很好的例子吗?)。这是代码:
public class Entity { //hold things such as player, and values realted to that specific entity (nice especially for organization purposes)
Image i = new Image();
MazeModel model = new MazeModel();
private BufferedImage image; //they all have images
}
class Player extends Entity{
image = i.getPlayer();
public void setPlayerStart(){
model.setPlayerX(50); //sets the starting x position of the player's image
model.setPlayerY(50); //sets the starting y position of the player's image
}
}
//these next two I will possibly add later if I have the time
class Enemy extends Entity{
//nothing here for now
}
class Key extends Entity{
//nothing here for now
}
答案 0 :(得分:1)
private
类的实例不能从父类继承到子类,也不能在子类中访问。
因此private BufferedImage image;
课程的Entity
Player
private BufferedImage image;
课程无法在protected BufferedImage image;
课程中显示。
建议:尝试将此Player
设为function array_rekey($a, $column)
{
$array = array();
foreach($a as $keys) $array[$keys[$column]] = $keys;
return $array;
}
,以便您也可以访问您的孩子(Sub ColumnCheck()
Dim i, LastRow, LastRowA, LastRowB, response
'get last row in column A and B, rather than
'iterating through all possible rows
LastRowA = Range("A" & Rows.Count).End(xlUp).Row
LastRowB = Range("A" & Rows.Count).End(xlUp).Row
'get the greatest row between A and B
If LastRowA >= LastRowB Then
LastRow = LastRowA
Else
LastRow = LastRowB
End If
'iterate through all rows comparing them.
For i = 1 To LastRow
If Cells(i, "A").Value <> Cells(i, "B").Value Then
response = "The columns are not equal!"
Range("E1") = response
MsgBox (response)
Exit Sub
End If
Next
response = "The columns are equal!"
Range("E1") = response
MsgBox (response)
)课程,并且您的实例变量也是安全的。
答案 1 :(得分:0)
将私人更改为公开。
public BufferedImage image;
答案 2 :(得分:0)
我会使用getter和setter来使其可访问。
对于您的情况,您可以将每个内部值设为私有,并通过getter和setter访问它:
public class Entity {
private Image i;
private MazeModel model;
private BufferedImage image;
// initialize these values when calling new
public Entity() {
this.i = new Image();
this.model = new MazeModel();
}
// create getter and setter
public BufferedImage getImage(){
return this.image;
}
public void setImage(BufferedImage image){
this.image = image;
}
public Image getI(){
return this.i;
}
// other code...
}
class Player extends Entity {
public Player(){
this.setImage(getI().getPlayer());
}
//other code...
}
除了getter和setter之外,您还需要创建Constructor(public Entity()和public Player())来初始化值。