第1题
【分录题】(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()); } }
第2题
【分录题】(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); } }
第3题
【分录题】(7-7)阅读程序,写出程序运行结果。 //写出程序运行结果 class Leg {// 腿 private int length; public Leg(int length) { this.length = length; } public int getLength() { return length; } public void setLength(int length) { this.length = length; } } class Head {// 头 private String type;// 类型 public Head(String type) { this.type = type; } public String getType() { return type; } public void setType(String type) { this.type = type; } } class Person { private String name;// 姓名 private char sex;// 性别 private Leg leg;//腿 private Head head;//头 public Person(String name) { this.name = name; } public Person(String name, char sex) { this(name); this.sex=sex; } public Leg getLeg() { return leg; } public void setLeg(Leg leg) { this.leg = leg; } public Head getHead() { return head; } public void setHead(Head head) { this.head = head; } public String getPerson() { StringBuffer sb=new StringBuffer(); sb.append(this.name+leg.getLength()+head.getType()); return sb.toString(); } } //测试类 public class DogDemo { public static void main(String[] args) { Leg leg=new Leg(30); Head head=new Head("国字脸"); Person zhangfei=new Person("张飞",'男'); zhangfei.setLeg(leg); zhangfei.setHead(head); leg.setLength(80); System.out.println(zhangfei.getPerson()); } }
第4题
【分录题】(7-7)阅读程序,写出程序运行结果。 //写出程序运行结果 class Eye {// 猫的眼睛类 private String color; public Eye(String color) { this.color = color; } public boolean equals(Object obj) { Eye e=(Eye) obj; if(this.color.equals(e.color)) return true; return false; } } class Cat {// 猫类 private String name; private Eye eye; public Cat(String name, Eye eye) { this.name = name; this.eye = eye; } public boolean equals(Object obj) { Cat cat = (Cat) obj; if (this.name.equals(cat.name) && this.eye.equals(cat.eye)) return true; return false; } } public class CatDemo { public static void main(String[] args) { Eye e1=new Eye("蓝色"); Eye e2=new Eye("蓝色"); Cat tom1=new Cat("Tom",e1); Cat tom2=new Cat("Tom",e1); System.out.println(tom1==tom2); System.out.println(tom1.equals(tom2)); } }
第5题
【分录题】以下程序段的输出结果为 (注意空格) public class Father { String name, address, tel; int age; public Father(String name, int age) { this.name = name; this.age = age; } void out() { System.out.print("姓名:" + name); System.out.print(" 年龄:" + age); } void outOther() { System.out.print(" 家庭住址:" + address); System.out.print(" 电话:" + tel); } public static void main(String args[]) { Son son = new Son("Tom", 15); son.address = "金水区"; son.school = "九中"; son.tel = "66123456"; son.out(); } } class Son extends Father { String school; public Son(String name, int age) { super(name, age); } void out() { super.out(); super.outOther(); System.out.println(" 学校:" + school); } }
第6题
第7题
第8题
第9题
【分录题】下面的程序定义了一个线程TimeThread,该线程每隔1秒钟输出显示一次当前系统时间,在main方法中使用TimeThread类创建3个新线程,并启动这些线程,请将下面的程序填写完整。 import java.util.*; class TimeThread implements (1) { public void run() { while (true) { Date currentTime = new Date(); try { (2) ; // 休眠1秒钟 } catch (Exception e) {System.out.println(e.toString());} System.out.println(Thread.currentThread().getName()+": "+currentTime); } } } public class Ex2 { public static void main(String[] args){ String[] names={"first","second","third"}; TimeThread myThread = new TimeThread(); for(int i=0;i<3;i++){ thread threadobj="new" thread(mythread,names[i]); (3) ; 启动线程 }>
第10题
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!