这是我到目前为止所拥有的
// Read in two names and print out information about each using
// methods in the Name class.
//------------------------------------------------------------------
public static void main(String[] args)
{
String first, middle, last;
// DECLARE AN OBJECT NAMED name1
// DECLARE AN OBJECT NAMED name2
Name name1 = new Name();
Name name2 = new Name();
Scanner scan = new Scanner(System.in);
System.out.println("\nName Program\n");
System.out.println ("Name of first person...");
System.out.print ("Enter the first name: ");
first = scan.nextLine();
System.out.print ("Enter the middle name: ");
middle = scan.nextLine();
System.out.print ("Enter the last name: ");
last = scan.nextLine();
// INSTANTIATE THE name1 OBJECT AND PASS ITS CONSTRUCTOR THE
// first, middle, AND last VARIABLES
public name1
this.Object = name1
答案 0 :(得分:1)
如果您有Java类名称:
public class Name {
private String first;
private String middle;
private String last;
public Name(String first, String middle, String last) {
this.first = first;
this.middle = middle;
this.last = last;
}
public String getFirst() {
return first;
}
public String getMiddle() {
return middle;
}
public String getLast() {
return last;
}
}
然后,您应实例化一个实例,如下所示:
String first = "First name";
String middle = "Middle name";
String last = "Last name";
Name name = new Name(first, middle, last);
现在您可以通过吸气剂(getFirst()
等)获取它的属性。