给出程序的运行结果()
classPerson{
Stringname;
intage;
Person(){
System.out.println('Person()');
}
voidPerson(){
System.out.println('method()');
}
publicvoidtell(){
System.out.println('姓名:'+name+',年龄:'+age);
}
}
publicclassClassTest02{
publicstaticvoidmain(String[]args){
Personperson=newPerson();
person.name='张三';
person.age=30;
person.tell();
}
}
classPerson{
Stringname;
intage;
Person(){
System.out.println('Person()');
}
voidPerson(){
System.out.println('method()');
}
publicvoidtell(){
System.out.println('姓名:'+name+',年龄:'+age);
}
}
publicclassClassTest02{
publicstaticvoidmain(String[]args){
Personperson=newPerson();
person.name='张三';
person.age=30;
person.tell();
}
}
举一反三
- 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
- 分析下面的程序,输出的结果是? 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 ; } }
- 【填空题】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()); } } 运行结果为:____
- 给出程序的运行结果()class Person {String name;...;person.tell();}}
- class Person{ static{ System.out.println(name); } private static String name = "hello"; } class Demo{ public static void main(String[] args){ Person p = null; } }