阅读程序,下面的哪个选项是正确的?( )。 class ExSuper{ String name; String nick_name; public ExSuper(String s,String t) { name = s; nick_name = t; } public String toString(){ return name; } } public class Example extends ExSuper{ public Example(String s,String t){ super(s,t); } public String toString(){ return name +"a.k.a"+nick_name; } public static void main(String args[]){ ExSuper a = new ExSuper(“First”,“1st”); ExSuper b = new Example(“Second”,“2nd”); System.out.println(“a is”+a.toString()); System.out.println("b is"+b.toString()); } }
举一反三
- 分析下面的程序,输出的结果是? 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 ; } }
- (06-06)分析如下所示的Java代码,其中this关键字的意思是( )。 public class Test { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; //this关键字所在的行 } } Class Main{ Public static void main(String []args){ Test T1 = new Test(); T1.setname(“zhangsan”); } }
- 【填空题】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()); } } 运行结果为:____
- (6-9)请阅读程序,写出程序运行结果。 class Phone{ private String name; private int price; public Phone(String name, int price) { this.name = name; this.price = price; } public String toString() { return ""+this.price; } } public class PhoneDemo { public static void main(String[] args) { StringBuffer sb=new StringBuffer(); Phone [] list=new Phone[3]; list[0]=new Phone("华为",3000); list[1]=new Phone("小米8",4000); list[2]=new Phone("vivo R10",2300); for(Phone p:list) sb.append(p); System.out.println(sb.toString()); } }
- 阅读下段代码, class Dog { public String name; Dog(String name){ this.name =name; }} public class Demo1 { public static void main(String[] args){ Dog dog1 = new Dog("xiaohuang"); Dog dog2 = new Dog("xiaohuang"); String s1 = dog1.toString(); String s2 = dog2.toString(); String s3 = "xiaohuang"; String s4 = "xiaohuang"; } } 返回值为true的是()