• 2021-04-14
    (6-2)定义如下Person类,(1)处合理的代码是。
    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, int a, String d) {

    department = d;
    }
    }
  • this(n,a);

    举一反三

    内容

    • 0

      (6-9)定义了如下Person类,下面程序段能够正确初始化Person数组。 class Person{ private String name; public Person(String name){ this.name=name; } }

    • 1

      class Person { String name; int age; [br][/br] public Person(String name, int age) { super(); this.name = name; this.age = age; } @Override public boolean equals(Object obj) { Person person=null; if(obj instanceof Person) person=(Person)obj; if(name.equals(person.name)&&age==person.age) return true; return false; } } class Test{ public static void main(String[] args) { Person person1 = new Person("张三", 12); Person person2 = new Person("张三", 12); System.out.println(person1.equals(person2));//运行结果是? } } 程序运行结果是什么? A: true B: false

    • 2

      给定如下代码,要在第三个构造方法的空白处填上一行代码使之能够调用第二个构造方法,这行代码应该是() class Circle{ String name,radius; int r; public Circle(String n){ name=n; } public Circle(String n,int a){ name=n; r=a; } public Circle(String n,String d,int a){ radius=d; } }

    • 3

      【填空题】public class ThisTest{ private String name; private int age; public ThisTest() { System.out.println("产生一个新的Person对象。"); } public ThisTest (String name, int age) { this(); this.name = name; this.age = age; } public String getInf() { return "姓名:" + name + ",年龄:" + age; } public static void main(String[] args) { ThisTest per = new ThisTest("张三", 20); System.out.println(per.getInf()); } } 运行结果为:____

    • 4

      以下代码执行后的结果是: public class Person { String name = “小芳”; public Person(String name) { name = “小兰”; } public void show() { this.name = “小翠”; } public static void main(String[] args) { Person p = new Person(“小凤”); System.out.print(p.name); p.show(); System.out.print(p.name); } }