(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 Student { private static int sid = 0; private String name; int id; Student(String name) { this.name = name; id = sid++; } public void info() { System.out.println("My name is "+name+" No."+id); } public static void infoSta() { System.out.println(sid); } public static void main(String arg[]) { Student.sid = 100; Student st1 = new Student("huahua"); st1.sid = 2000; Student st2 = new Student("meimei"); st1.info(); st2.info(); } }
- (7-12)请阅读程序,写出程序运行结果。 class A{ static String name="tom"; static int getX() { return 2; } int getY() { return 3; } } class B extends A{ static String name="Tuny"; static int getX() { return 4; } int getY() { return 5; } } class C extends A{ static String name="Tuny"; static int getX() { return 4; } int getY() { return 5; } } interface D{ int getY(); } class E implements D{ public int getY() { return 6; } } public class Demo{ public static void main(String[] args) { A a=new A(); A b=new B(); A c=new C(); D d=new E(); int sum=a.name.length()+b.getX()+c.getY()+d.getY(); System.out.println(sum); } }
- (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()); } }
- 【填空题】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()); } } 运行结果为:____
- 分析下面的程序,输出的结果是? 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 ; } }