【填空题】读下面程序,写出程序运行结果。 class Point{ int x; public Point(){x=0;} public Point(int x){this.x=x;} public void show(){System.out.println(x);}} class SPoint extends Point{ int y; public SPoint(){y=0;} public SPoint(int x,int y){super(x); this.y=y;} public void show(){System.out.println(x+","+y);}} public class tempDemo{ public static void main(String []args){ Point p=new Point(); p.show(); Point p1=new Point(2); p1.show(); SPoint sp=new SPoint(); sp.show(); SPoint sp1=new SPoint(2,3); sp1.show(); }} 运行结果为:
第1题
第2题
【填空题】读下面程序,写出程序运行结果。 class Father { int num1=0; public Father(){ System.out.println("num1:"+num1);} public Father(int num1){ this.num1=num1; System.out.println("num1:"+num1); } } class Son extends Father{ int num2=10; public Son(){System.out.println("num2:"+num2);} public Son(int i,int j){ super(i); num2=j; System.out.println( "num1:"+num1+",num2:"+num2); } } public class Demo{ public static void main(String []args){ Father father=new Father(); Son son=new Son(); Son son1=new Son(20,30); } } 运行结果为:
第3题
【填空题】读下面程序,写出程序运行结果。 abstract class Graph{ int a; public Graph(){a=1;} public Graph(int a){this.a=a; } public abstract void showArea(); } class Rect extends Graph{ int b; public Rect(int a,int b){super(a); this.b= b; } public void showArea(){ System.out.println("Area:"+(a*b));} } class Circle extends Graph{ public Circle(){}; public Circle(int a){super(a);} public void showArea(){ System.out.println("Area:"+(3.14*a*a)); } } public class classDemo1{ public static void main(String []args){ Graph g1; g1=new Rect(2,3); g1.showArea(); g1=new Circle(2); g1.showArea(); g1= new Circle(); g1.showArea(); } } 运行结果为:
第4题
【分录题】(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()); } }
第5题
【分录题】(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题
【分录题】(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()); } }
第7题
【分录题】(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)); } }
第9题
7、【简答题】9.3读程序写出运行结果
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!