(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 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());
}
}
举一反三
- (6-3)阅读程序,写出程序运行结果。 class Book{ private static int counter=0; private int id=1; private String name; public Book(String name) { this.name = name; counter++; this.id=this.id+8; } public static int getCounter() { return counter; } public int getID() { return this.id; } } public class BookDemo{ public static void main(String[] args) { Book b1=new Book("红楼梦"); Book b2=new Book("西游记"); Book b3=new Book("儒林外史"); System.out.println(b3.getCounter()*Book.getCounter()*b3.getID()); } }
- 分析下面的程序,输出的结果是? 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 ; } }
- 下列程序的运行结果是( )class Demo{private String name;Demo(String name){this.name = name;}private static void show(){System.out.println(name)}public static void main(String[] args){Demo d = new Demo(“lisa”);d.show();}}
- public class Example { String str = new String("Java"); StringBuffer sb = new StringBuffer("C#"); public void strcat(String str, StringBuffer sb) { str = str + " study"; sb = sb.append(" exam"); } public static void main(String args[]) { Example ex = new Example(); ex.strcat(ex.str, ex.sb); System.out.println(ex.str); System.out.println(ex.sb); }}程序的输出结果是:______
- 【填空题】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()); } } 运行结果为:____