• 2021-04-14
    分析下面的程序,输出的结果是?
    public class Test {
    public static void main(String[] args) {
    final Person p = new Person("张三", 23);
    p.setName("李四");
    p.setAge(24);
    System.out.println(p);
    }
    }
    class Person {
    private String name;
    private int age;
    public Person() {
    super();
    }
    public Person(String name, int age) {
    super();
    this.name = name;
    this.age = age;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    @Override
    public String toString() {
    return "我的姓名是:" + name + ",我的年龄是:" + age ;
    }
    }
  • 我的姓名是:李四","我的年龄是:24

    举一反三

    内容

    • 0

      有如下类的定义,创建Employee对象错误的是() public class Employee{ private int age; private String name; public Employee(int age){ this.age = age; } public Employee(String name){ this.name = name; } public Employee(int age,String name){ this(age); this.name = name; } } A: Employee emp = new Employee(10); B: Employee emp = new Employee("Tom"); C: Employee emp = new Employee(); D: Employee emp = new Employee(10,"Tom");

    • 1

      4.6 若有以下程序代码: 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 ){ //执行此代码 } 下面那个选项可以添加到“//执行此代码”前面( )

    • 2

      在以下代码中,( )是类Teacher的方法。 public class Teacher { int age=33; private string name; public string Name{ get{return name;} set{name=value;} } public void SaySomething(){ //….. }} A: Name B: name C: age D: SaySomething

    • 3

      阅读下面代码: Person 类: public class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } Servlet1: request.getSession().setAttribute("name","zhangsan"); Person p = new Person(); p.setName("lisi"); request.getSession().setAttribute("person",p); Servlet2: String name1 = (String)request.getSession().getAttribute("name"); Person p = (Person)request.getSession().getAttribute("person"); response.getWriter().write(name1); if(p!=null)response.getWriter().write(p.getName()); 浏览器先访问Servlet1,再去访问Servlet2,输出的结果是:()

    • 4

      在以下代码中,( )是类Teacher的方法。 public class Teacher { int age=33; private string name; public string Name { get{return name;} set{name=value;} } public void SaySomething(){ //….. } }