java中toString类题目求解答
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中toString类题目求解答相关的知识,希望对你有一定的参考价值。
定义类Person及其子类Employee,Employee的子类Manager,每个类定义下列成员变量:
Person类:姓名、年龄
Employee类:工号、工资
Manager类:职务名称
每个类定义构造方法初始化所有变量,重写toString()方法输出所有成员变量值。
定义测试类PolyTest:创建这些类的对象,调用toString()方法进行测试。
Person类:
public class Person
private String name;
private String age;
public String getName()
return name;
public String getAge()
return age;
public Person()
this.name = "张三";
this.age = "18";
@Override
public String toString()
return "Person" +
"name='" + name + '\\'' +
", age='" + age + '\\'' +
'';
Employee类:
public class Employee extends Person
//工号
private String no;
//工资
private double wages;
public String getNo()
return no;
public double getWages()
return wages;
public Employee()
this.no = "007";
this.wages = 3000;
@Override
public String toString()
return "Employee" +
"name='" + this.getName() + '\\'' +
", age=" + this.getAge() +
"no='" + no + '\\'' +
", wages=" + wages +
'';
Manager类:
public class Manager extends Employee
//职称
private String title;
public Manager()
this.title="普通员工";
@Override
public String toString()
return "Manager" +
"name='" + this.getName() + '\\'' +
"age='" + this.getAge() + '\\'' +
"no='" + this.getNo() + '\\'' +
"wages='" + this.getWages() + '\\'' +
"title='" + title + '\\'' +
'';
PolyTest类:
public class PolyTest
public static void main(String[] args)
Person person = new Person();
System.out.println(person.toString());
Employee employee = new Employee();
System.out.println(employee.toString());
Manager manager = new Manager();
System.out.println(manager.toString());
输出结果:
几道道简单的java题目 求解答,分析过程 在线等能够
第一:
which two statements declare an array capable of 10 int?
A. int[] foo;
B. int foo[];
C. int foo[10];
D. Object[] foo;
E. Object foo[10];
第二题:
public class X implements Runnable
public static void main(String[] args)
3) //insert code
public void run()
int x=0,y=0;
for(;;)
x++;
Y++;
System.out.println("x="+x+",y="+y);
You want to cause execution of the run method in a new thread of execution.
Which line(s) should be added to the main method at line 3?
A. X x=new X();
x.run();
B. X x=new X();
new Thread(x).run();
C. X x=new X();
new Thread(x).start();
D. Thread t=new Thread(x).run();
E. Thread t=new Thread(x).start();
第三题:
which two declaretions prevent the overriding of a method?
A. final void methoda()
B. void final methoda()
C. static void methoda()
D. static final void methoda()
E. final abstract void methoda()
第四题:
which two are true?
A. static inner class requires a static initializer
B. A static inner class requires an instance of the enclosing class
C. A static inner class has no reference to an instance of the enclosing class
D. A static inner class has accesss to the non-static member of the other class
E. static members of a static inner class can be referenced using the class name of the static inner class
第五题:
which two are true?
A. An anonymous inner class can be declared inside of a method
B. An anonymous inner class constructor can take arguments in some situations
C. An anonymous inner class that is a direct subclass of Object can implements multiple interface
D. Even if a class Super does not implement any interfaces, it is still possible to define an anonymous inner class that is an immediate subclass of Super that implements a single interface
E. Even if a class Super does not implement any interfaces, it is still possible to define an anonymous inner class that is an immediate subclass of Super that implements multipe interface
各位,选择自己的擅长分析一两道题目吧
1排除法 AB,声明不能指明大小C错,DE不是int
2 C 我没什么可以说的,如果有其他想法,你说说
3 AD,题目是防止方法被重写,Bfinal在返回类型后,C静态方法可以被重写,但是一个方法不能被重写成静态方法,E明显错误final和abstract不能同时
4 CD,Static inner class这种东西可以不算做内部类(不从位置说),它没有内部类的任何特性,它不必与外部类建立联系B错,它不能够使用Outter.this访问外部类实例C对,A不要求,让我纠结的是DE,选了D是因为不能访问的是外部类的非静态成员,但是其他类的非静态成员只要new了都应该是正常访问的。E感觉问题大过D,因为必须使用外部类和内部类名一起才可以
5 刚开始选AB基本是排除法先,匿名内部类不能实现多个接口C错,也不能即实现一个接口又继承一个类,但是可以做一样D错,E同样原因错 但是后来我发下可能是翻译问题D不是说继承一个类又实现一个接口,而是内部类是super的直接子类那么实现一个接口就是ok的了,再来看看AB,刚开始理解B以为是调用时给的参数,选了D后细读应该是只构造方法给参数,匿名类的构造方法是不是很可笑呢,所以5应该选AD 参考技术A 第一题,定义一个长度为10的整型数组,下面哪种命名方式是正确的?
第二题,插入下列哪行代码能使程序正常运行?
第三题,下面哪两种声明方式会使得方法不会被重载?
第四题,哪两个是正确的?
第五题,哪两个是正确的?
我只会告诉你题目是什么,而答案,你自己去寻找,记住,既然你是搞软件的,千万不要得过且过,软件是技术行业,行就行,不行就不行,千万不要得自己骗自己。追问
我知道,我都上班了,自己只是想知道为什么以及验证自己的想法,不是出于要答案。我有答案
参考技术B 第三题注意是“重写”而不是“重载”,静态方法可以被重载但不能被重写,这题选CD以上是关于java中toString类题目求解答的主要内容,如果未能解决你的问题,请参考以下文章