如何从新对象调用字段
How to call on a field from a new object
我在这里创建了一个名为 "Person" 的 class:(忽略 toString。我还没有用它做任何事情)
public class Person {
public String firstName;
public String middleName;
public String lastName;
public Person() {
firstName = "first";
middleName = "middle";
lastName = "last";
}
public Person(String first, String middle, String last) {
firstName = first;
middleName = middle;
lastName = last;
}
public String toString() {
return (firstName + " " + middleName + " " + lastName);
}
public String getFirstName() {
return firstName;
}
public String getMiddleName() {
return middleName;
}
public String getLastName() {
return lastName;
}
}
然后我在一个新文件中创建了一个实现 class,就是这样:
import java.util.*;
public class TestProgPerson
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
String first;
String middle;
String last;
Person name = new Person("Joe", "Smith", "Blow");
System.out.println("Name: " + name);
System.out.println("Please enter a last name, to check if it corresponds with the persons last name: " );
last = console.nextLine();
if (last == (objectReference.lastName))
System.out.println("The last name you entered matches the persons last name");
else
System.out.println("The last name you entered does not match the persons last name");
}
}
所以我想让它做的是:拥有一个对象,其中包含名字、中间名和姓氏。输出那个名字。 (该程序到目前为止有效)。然后我想让用户输入姓氏,程序检查输入的姓氏是否与对象中的姓氏相同。我该如何着手从该对象中调用一个单独的字符串?
此处您调用的是 class 的名称对象,而不是 class 的任何字段。
System.out.println("Name: " + name);
您可以使用class的对象和点运算符来调用此处的字段。
例如。
System.out.println("Name: " + name.firstName + " " + name.middleName + " " + name.lastName);
另外因为字符串是Objects
,应该用equals
的方法比较
我在这里创建了一个名为 "Person" 的 class:(忽略 toString。我还没有用它做任何事情)
public class Person {
public String firstName;
public String middleName;
public String lastName;
public Person() {
firstName = "first";
middleName = "middle";
lastName = "last";
}
public Person(String first, String middle, String last) {
firstName = first;
middleName = middle;
lastName = last;
}
public String toString() {
return (firstName + " " + middleName + " " + lastName);
}
public String getFirstName() {
return firstName;
}
public String getMiddleName() {
return middleName;
}
public String getLastName() {
return lastName;
}
}
然后我在一个新文件中创建了一个实现 class,就是这样:
import java.util.*;
public class TestProgPerson
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
String first;
String middle;
String last;
Person name = new Person("Joe", "Smith", "Blow");
System.out.println("Name: " + name);
System.out.println("Please enter a last name, to check if it corresponds with the persons last name: " );
last = console.nextLine();
if (last == (objectReference.lastName))
System.out.println("The last name you entered matches the persons last name");
else
System.out.println("The last name you entered does not match the persons last name");
}
}
所以我想让它做的是:拥有一个对象,其中包含名字、中间名和姓氏。输出那个名字。 (该程序到目前为止有效)。然后我想让用户输入姓氏,程序检查输入的姓氏是否与对象中的姓氏相同。我该如何着手从该对象中调用一个单独的字符串?
此处您调用的是 class 的名称对象,而不是 class 的任何字段。
System.out.println("Name: " + name);
您可以使用class的对象和点运算符来调用此处的字段。
例如。
System.out.println("Name: " + name.firstName + " " + name.middleName + " " + name.lastName);
另外因为字符串是Objects
,应该用equals
的方法比较