目的:用户输入姓名和年龄,并在结尾处打印一条消息:
"姓名是年龄。"
NameIsAge.java
function reload2(form)
{
var val=form.loc.options[form.loc.options.selectedIndex].value;
var val2 = form.category.options[form.category.options.selectedIndex].value;
self.location='upload.php?category=' + val2 + '&loc=' + val;
}
NameIsAgeLauncher.java
public class NameIsAge
{
static String userName;
static int userAge;
NameIsAge (String userName, int userAge)
{
userName = this.userName;
userAge = this.userAge;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getUserAge() {
return userAge;
}
public void setUserAge(int userAge) {
this.userAge = userAge;
}
final void run(String[] args)
{
System.out.println(userName + " is " + userAge);
}
}
这打印出来:
import java.util.Scanner;
public final class NameIsAgeLauncher {
private NameIsAgeLauncher()
{
}
public static void main(String[] args) {
String userName = null;
int userAge = 0;
Scanner stdIn = new Scanner(System.in);
System.out.println("Enter your name: "); //Prompts user to enter name.
NameIsAge.userName = stdIn.nextLine(); //Name from Practice should be equal to what the user enters.
System.out.println("Enter your age: "); //Prompts user to enter name.
NameIsAge.userAge = stdIn.nextInt(); //Name from Practice should be equal to what the user enters.
NameIsAge blah = new NameIsAge(userName, userAge);
NameIsAge halb = new NameIsAge ("Campbell", 97);
halb.setUserName("Campbell");
halb.setUserAge (97);
System.out.println(blah.userName + " is " + blah.userAge + ".");
System.out.println(halb.userName + " is " + halb.userAge + ".");
}
}
应打印出来:
Enter your name:
dsf
Enter your age:
32
Blah: Campbell is 97.
Halb: Campbell is 97.
Y是否设置halb change blah的值?
答案 0 :(得分:3)
这是由静态修饰符引起的。
static String userName;
static int userAge;
删除静态关键字就是诀窍 静态修改器表示它适用于所有实例 顺便说一句。构造函数不好,应该是:
NameIsAge (String userName, int userAge)
{
this.userName = userName;
this.userAge = userAge;
}
包含整个代码:
尝试将局部变量用于辅助变量
import java.util.Scanner;
public final class NameIsAgeLauncher {
private NameIsAgeLauncher()
{
}
public static void main(String[] args) {
String userName = null;
int userAge = 0;
Scanner stdIn = new Scanner(System.in);
System.out.println("Enter your name: "); //Prompts user to enter name.
userName = stdIn.nextLine(); //Name from Practice should be equal to what the user enters.
System.out.println("Enter your age: "); //Prompts user to enter name.
userAge = stdIn.nextInt(); //Name from Practice should be equal to what the user enters.
NameIsAge blah = new NameIsAge(userName, userAge);
NameIsAge halb = new NameIsAge ("Campbell", 97);
halb.setUserName("Campbell");
halb.setUserAge (97);
System.out.println(blah.userName + " is " + blah.userAge + ".");
System.out.println(halb.userName + " is " + halb.userAge + ".");
}
}
public class NameIsAge
{
String userName;
int userAge;
NameIsAge (String userName, int userAge)
{
this.userName = userName;
this.userAge = userAge;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getUserAge() {
return userAge;
}
public void setUserAge(int userAge) {
this.userAge = userAge;
}
final void run(String[] args)
{
System.out.println(userName + " is " + userAge);
}
}