对象练习:
A:声明玩家类(Player)
属性:姓名,等级,能量值
方法:定义方法用于玩家发表评论
B:在main()中实例化三个玩家,分别为每一个玩家属性赋值
每个玩家分别发表评论
控制台输出:
天仙娃娃 等级:1 能量: 100
发言到: 大家好啊,我来了
神秘人 等级:12 能量: 2100
发言到: 我也来了
龙珠 等级:15 能量: 3400
发言到: 我来了
java代码:
public class Player {
//定义属性
//数据类型 成员变量名;
String gameName;
int grade;
int energy;
String speak;
// public void speak(){
// System.out.println("我来了");
// }
//定义方法
public void consolePrinting(){
System.out.println(gameName + "\t" + "等级:" + grade+"能量:"+energy);
System.out.println("发言到:" + speak);
//错误点:应该定义一个speak()方法来进行调用------>待解决
}
}
public class PlayerText {
public static void main(String[] args) {
//实例化一个具体的对象,对象名是:gameOne
Player gameOne = new Player();
gameOne.gameName = "天仙娃娃";
gameOne.grade = 1;
gameOne.energy = 100;
gameOne.speak = "大家好,我来了"; //不正确的,需要调用speak()
gameOne.consolePrinting();
Player gameTwo = new Player();
gameTwo.gameName = "神秘人";
gameTwo.grade = 12;
gameTwo.energy = 2100;
gameTwo.speak = "我也来了"; //不正确的,需要调用speak()
gameTwo.consolePrinting();
Player gameThree = new Player();
gameThree.gameName = "龙珠";
gameThree.grade = 15;
gameThree.energy = 3400;
gameThree.speak = "我来了"; //不正确的,需要调用speak()
gameThree.consolePrinting();
}
}
------仅供参考------