单选题Given the uncompleted code of a class:     class Person {  String name, department;     int age;  public Person(String n){  name = n; }  public Person(String n, int a){  name = n;  age = a;  }  public Person(String n, String d, int a) {  // doing the same as two arguments version of constructor     // including assignment name=n,age=a    department = d;     }     }  Which expression can be added at the "doing the same as..." part of the constructor?()A Person(n,a);B this(Person(n,a));C this(n,a);D this(name,age);

单选题
Given the uncompleted code of a class:     class Person {  String name, department;     int age;  public Person(String n){  name = n; }  public Person(String n, int a){  name = n;  age = a;  }  public Person(String n, String d, int a) {  // doing the same as two arguments version of constructor     // including assignment name=n,age=a    department = d;     }     }  Which expression can be added at the "doing the same as..." part of the constructor?()
A

 Person(n,a);

B

 this(Person(n,a));

C

 this(n,a);

D

 this(name,age);


参考解析

解析: 在同一个类的不同构造方法中调用该类的其它构造方法需要使用this(…)的形式,而且必须是在构造方法的第一行调用,这个和普通的方法重载调用的方式不同,普通的方法可以直接使用方法名加参数来调用,而且调用位置没有限制,因此答案A是不行的,B的语法就是错误的,D的错误在于在父类型的构造函数被调用前不能引用类的成员。构造方法是一个类对象实例化的起点(虽然严格来说首先执行的并不是构造方法的第一个语句,而是内存的分配),因此在构造方法中不能将成员作为参数引用。

相关考题:

为使下列代码正常运行,应该在下划线处填入的选项是abstract class Person{public Person(String n)!name=n;}public____String getDescription();public String getName(){return name;}private String name;}A.staticB.privateC.abstractD.final

能将程序补充完整的选项是( )。class Person{ private int a; public int change(int m){ return m; }}public class Teacher extends Person{ public int b; public static void main(String arg[]) { Person p = new Person(); Teacher t = new Teacher(); int i; ______ }} B.A.i=mB.i=bC. i=p.aD.i=p. change(50)

能将程序补充完整的选项是______。 class Person{ private int a; phblic int change(int m){return m;} } public class Teacher extends Person{ public int b; public static void main(String arg[ ]){ Person p=new Person( ); Teacher t=new Teacher( ); int i; ______; } }A.i=mB.i=bC.i=p.aD.i=p.change(50)

阅读下列代码段 abstract class Person{ public Person(String n){ name=n; } public______String getDescription(); public String getName(){ } private String name; } 在下画线处应填入的修饰符是A.staticB.abstractC.protectedD.final

class Person{ String name,department; int age; public Person(Stringn){name=n;} public Person(String n, int a){name=n;age=a;} publicPerson(String n,String d,int a){ doing the same as two argumentsversion of constructer including assignment name=n,age=a } } 下列哪一个选项可以添加到“doing the same....”处?()APerson(n,a)Bthis(Person(n,a))Cthis(n,a)Dthis(name,age

给出下列的不完整的类代码,则哪个语句可以被加到横线处? ( ) class Person{ String name,department; int age; public Person(String n){name=n;} public Person(String n,int s){name=n; age=a;} public Person(String n,String d,int a){ department=d;______ } }A.Person(n,a);B.this(Person(n,a));C.this(n,s);D.this(name,age);

有以下程序:includeincludeusingnameSpacestd;classperson{ intage; Char*nam 有以下程序:include <iostream>include <string>using nameSpace std;class person{int age;Char * name;public:person ( int i, Char * str ){int j;j = strlen( str ) + 1;name = new char[ j ];strcpy( name, str );age = i;}~person(){delete name;cout<<"D";}void display(){cout<<name<<":"<<age;}};int main(){person demo( 30,"Smith" );demo.display();return 0;}则该程序的输出结果为:【 】。

下列程序运行后的输出结果是( )。#include#includeusing namespace std;class Person{public:Person(string n):name(n) { coutprivate:int year,month,day;};class Student:public Person{public:Student(string n,int y,int m,int d,char c):birthday(y,m,d),sex(c),Person(n) { coutA. SB.PSC.DPSD.PDS

阅读下面代码 abstract class Person { public Person(String n) { name=n; } public______String getDescription(); public String getName() { return name; } private String name; } 在下画线处应填入的修饰符是A.staticB.abstractC.protectedD.final

给出下列的不完整的类代码,则下列的( )语句可以加到横线处。 class Person{ String name,department; int age public Person(String n){name=n;} public Person(String n,int a){name=n;age=a;} pubilc Person(String n,String d,int a) { _______________ department=d; } }A.Person(n,a);B.this(Person(n,a));C.this(n,a);D.this(name,age);

给出下面不完整的类代码,则横线处的语句应该为( )。 class Person { String name,department; int age; public Person (Strings) {name=s;} public Person (String s,intA.{name=s;age=a;} public Person (String n,String d,intA){ __________ department=d; } }A)Person (n,A);B.this (Person(n,A));C.this(n,A);D.this(name,age);

阅读以下说明和Java 码,将应填入(n)处的字名写在的对应栏内。[说明] 编写一个学生类Student,要求:(1) 学生类Student 属性有:id: long 型,代表学号name: String类对象,代表姓名age: int 型,代表年龄sex: boolen 型,代表性别(其中:true 表示男,false 表示女)phone: String 类对象,代表联系电话(2) 学生类Student 的方法有:Student (long i,String n,int a,boolean s,String p):有参构造函数,形参表中的参数分别初始化学号、姓名、年龄、性别和联系电话。int getAge ():获取年龄作为方法的返回值。boolean getSex ():获取性别作为方法的返回值。String getPhone ():获取联系电话作为方法的返回值。public String to String ():以姓名:性别:学号:联系电话的形式作为方法的返import java. applet. Applet;import java. awt.* ;public class Student extends Applet {long id;String name, phone;int age;boolean sex;Student(long i, String n, int a, boolean s, String p){id=i;name = n;age = a;sex= s;phone = p;{public void paint( Graphics g){Student x= new Student (5000," xiaoliu" , 89, true, " 8989898" );(1);(2)g. drawstring( x. getPhone( ), 140,140);}int getAge( ){ return age; }boolean getsex ( ){ return sex; }String getPhone( ){ return phone; }String ToString( ){(3)}}

为使下列代码正常运行,应该在下画线处填入的选项是( )。 abstract class person{ public Person(String n){ name=n: } Public String getDescription; public String getName{ return name; } private string name; }A.staticB.privateC.abstractD.final

给出下列的不完整的类代码,则下列的哪个语句可以加到横线处? class Person { String name,department; int age; public Person( String n ){ name = n;} public Person( String n,int a ) { name = n;age = a;} public Person( String n,String d,int a ) { _____________ department = d; } }A.Person(n,a);B.this(Person(n,a) );C.this(n,a);D.this(name,age);

Person p = new Person(“张三”,23);这条语句会调用下列哪个构造方法给属性进行初始化() A.public Person(){}B.public Person(String name,int age) { this.name = name; this.age = age; }C.public Person(int age,String name) { this.age = age; this.name = name; }D.public Person(String name) { this.name = name; }

public class Person {  private String name, comment;  private int age;  public Person(String n, int a, String c) {  name = n; age = a; comment = c;  }  public boolean equals(Object o) {  if(! (o instanceof Person)) return false;  Person p = (Person)o;  return age == p.age  name.equals(p.name);  }  }  What is the appropriate definition of the hashCode method in class Person?() A、 return super.hashCode();B、 return name.hashCode() + age * 7;C、 return name.hashCode() + comment.hashCode() /2;D、 return name.hashCode() + comment.hashCode() / 2 - age * 3;

Which are syntactically valid statement at// point x?()     class Person {     private int a;  public int change(int m){  return m;  }     }  public class Teacher extends Person {     public int b;  public static void main(String arg[]){     Person p = new Person();     Teacher t = new Teacher();    int i;  // point x     }    } A、 i = m;B、 i = b;C、 i = p.a;D、 i = p.change(30);E、 i = t.b.

public class Person {  private name;  public Person(String name) {  this.name = name;  }  public int hashCode() {  return 420;  }  }  Which is true?() A、 The time to find the value from HashMap with a Person key depends on the size of the map.B、 Deleting a Person key from a HashMap will delete all map entries for all keys of typePerson.C、 Inserting a second Person object into a HashSet will cause the first Person object to beremoved as a duplicate.D、 The time to determine whether a Person object is contained in a HashSet is constant and does NOT depend on the size of the map.

public class Employee{       private String name;  public Employee(String name){           this.name = name;      }  public String getName(){         return name;      } }  public class Manager extends Employee{       private String department;  public Manager(String name,String department){          this.department = department;          super(name);  System.out.println(getName());      }  }  执行语句new Manager(“smith”,”SALES”)后程序的输出是哪项?() A、 smithB、 nullC、 SALESD、 编译错误

public class Employee{   private String name;   public Employee(String name){   this.name = name;  }   public void display(){   System.out.print(name);  }  }   public class Manager extends Employee{   private String department;   public Manager(String name,String department){   super(name);   this.department = department;  }   public void display(){   System.out.println( super.display()+”,”+department);  }   }   执行语句new Manager(“smith”,”SALES”)后程序的输出是哪项?() A、 smith,SALESB、 null,SALESC、 smith,nullD、 null,null

Given the uncompleted code of a class:     class Person {  String name, department;     int age;  public Person(String n){  name = n; }  public Person(String n, int a){  name = n;  age = a;  }  public Person(String n, String d, int a) {  // doing the same as two arguments version of constructor     // including assignment name=n,age=a    department = d;     }     }  Which expression can be added at the "doing the same as..." part of the constructor?() A、 Person(n,a);B、 this(Person(n,a));C、 this(n,a);D、 this(name,age);

11. class Person {  12. String name = “No name‟;  13. public Person(String nm) { name = nm; }  14. }  15.  16. class Employee extends Person {  17. String emplD = “0000”;  18. public Employee(String id) { empID = id; }  19. }  20.  21. public class EmployeeTest {  22. public static void main(String[] args) {  23. Employee e = new Employee(”4321”);  24. System.out.println(e.empID);  25. }  26. }  What is the result?() A、 4321B、 0000C、 An exception is thrown at runtime.D、 Compilation fails because of an error in line 18.

多选题Which are syntactically valid statement at// point x?()     class Person {     private int a;  public int change(int m){  return m;  }     }  public class Teacher extends Person {     public int b;  public static void main(String arg[]){     Person p = new Person();     Teacher t = new Teacher();    int i;  // point x     }    }Ai = m;Bi = b;Ci = p.a;Di = p.change(30);Ei = t.b.

单选题11. class Person {  12. String name = “No name‟;  13. public Person(String nm) { name = nm; }  14. }  15.  16. class Employee extends Person {  17. String emplD = “0000”;  18. public Employee(String id) { empID = id; }  19. }  20.  21. public class EmployeeTest {  22. public static void main(String[] args) {  23. Employee e = new Employee(”4321”);  24. System.out.println(e.empID);  25. }  26. }  What is the result?()A 4321B 0000C An exception is thrown at runtime.D Compilation fails because of an error in line 18.

单选题public class Employee{       private String name;  public Employee(String name){           this.name = name;      }  public String getName(){         return name;      } }  public class Manager extends Employee{       private String department;  public Manager(String name,String department){          this.department = department;          super(name);  System.out.println(getName());      }  }  执行语句new Manager(“smith”,”SALES”)后程序的输出是哪项?()A smithB nullC SALESD 编译错误

单选题public class Person {  private name;  public Person(String name) {  this.name = name;  }  public int hashCode() {  return 420;  }  }  Which is true?()A The time to find the value from HashMap with a Person key depends on the size of the map.B Deleting a Person key from a HashMap will delete all map entries for all keys of typePerson.C Inserting a second Person object into a HashSet will cause the first Person object to beremoved as a duplicate.D The time to determine whether a Person object is contained in a HashSet is constant and does NOT depend on the size of the map.

单选题public class Person {  private String name, comment;  private int age;  public Person(String n, int a, String c) {  name = n; age = a; comment = c;  }  public boolean equals(Object o) {  if(! (o instanceof Person)) return false;  Person p = (Person)o;  return age == p.age  name.equals(p.name);  }  }  What is the appropriate definition of the hashCode method in class Person?()A return super.hashCode();B return name.hashCode() + age * 7;C return name.hashCode() + comment.hashCode() /2;D return name.hashCode() + comment.hashCode() / 2 - age * 3;

单选题Given the uncompleted code of a class:     class Person {  String name, department;     int age;  public Person(String n){  name = n; }  public Person(String n, int a){  name = n;  age = a;  }  public Person(String n, String d, int a) {  // doing the same as two arguments version of constructor     // including assignment name=n,age=a    department = d;     }     }  Which expression can be added at the "doing the same as..." part of the constructor?()A Person(n,a);B this(Person(n,a));C this(n,a);D this(name,age);