Java 在子类中设置方法
我需要通过调用超类中的set方法来设置子类中的值。我需要从子类中设置怪物名称和生命值 外星人类Java 在子类中设置方法,java,eclipse,Java,Eclipse,我需要通过调用超类中的set方法来设置子类中的值。我需要从子类中设置怪物名称和生命值 外星人类 public class Alien { //Instance variables public String monsterName; public int HP; //A method that sets monster properties public void setValues(String monsterN, int health) {
public class Alien {
//Instance variables
public String monsterName;
public int HP;
//A method that sets monster properties
public void setValues(String monsterN, int health) {
monsterName = monsterN;
HP = health;
}
//A method that returns the monster name
public String getName() {
return monsterName;
}
//A method that returns monster's health
public int getHP() {
return HP;
}
//ToString method that prints out the info
public String toString() {
return("Monster name: " + getName() + "Monster's current health point: " + getHP());
}
}
public class SnakeAlien extends Alien {
//Instance variable
//Set the snake values
Alien aObject = new Alien();
aObject.setvalues("Snake\n" 55));
}
public class Main {
public static void main(String[] args) {
Alien object = new Alien();
object.toString();
System.out.println(object);
}
}
蛇类
public class Alien {
//Instance variables
public String monsterName;
public int HP;
//A method that sets monster properties
public void setValues(String monsterN, int health) {
monsterName = monsterN;
HP = health;
}
//A method that returns the monster name
public String getName() {
return monsterName;
}
//A method that returns monster's health
public int getHP() {
return HP;
}
//ToString method that prints out the info
public String toString() {
return("Monster name: " + getName() + "Monster's current health point: " + getHP());
}
}
public class SnakeAlien extends Alien {
//Instance variable
//Set the snake values
Alien aObject = new Alien();
aObject.setvalues("Snake\n" 55));
}
public class Main {
public static void main(String[] args) {
Alien object = new Alien();
object.toString();
System.out.println(object);
}
}
主类
public class Alien {
//Instance variables
public String monsterName;
public int HP;
//A method that sets monster properties
public void setValues(String monsterN, int health) {
monsterName = monsterN;
HP = health;
}
//A method that returns the monster name
public String getName() {
return monsterName;
}
//A method that returns monster's health
public int getHP() {
return HP;
}
//ToString method that prints out the info
public String toString() {
return("Monster name: " + getName() + "Monster's current health point: " + getHP());
}
}
public class SnakeAlien extends Alien {
//Instance variable
//Set the snake values
Alien aObject = new Alien();
aObject.setvalues("Snake\n" 55));
}
public class Main {
public static void main(String[] args) {
Alien object = new Alien();
object.toString();
System.out.println(object);
}
}
我知道如何通过主类设置这些值,但我需要从子类
SnakeAlien
中进行设置,该子类源自Alien
类。因为所有内容都是公共的(顺便说一句,这很糟糕),您只需在子类中调用超类的方法即可。甚至不需要超级
不过,您应该将您不想接触的成员从外部更改为至少受保护的成员,如果您永远不想制作纯外星对象,您应该将其设置为avstract类。我没有理解要点。我如何从没有对象的超类调用方法?@TecHummer好吧,你有一个对象。事实上你就在里面。想象一下继承的函数,就好像它们被连接到子类的源代码中一样。我还是不明白。如何在子类中使用来自超级类的方法?@TecHummer您是否阅读过我的评论?超类中的方法基本上都在子类中,只需使用它们,不要创建新的外来对象