题目: 请找出以下代码中的10处错误,写出行号并说明错误原因。1. public class ErrorTest2 {
2. public static void main(String[] args) {
3. // 实例化错误4. Animal animal = new Animal();
5. Dog dog = new Animal();
6.
7. // 静态使用错误
8. Counter c1 = new Counter();
9. Counter c2 = new Counter();
10. System.out.println(c1.count + " " + c2.count);
11.
12. // 接口实现错误
13. MyInterface obj = new MyInterface() {
14. // 缺少method2的实现
15. };
16.
17. // 访问权限错误
18. Student student = new Student();
19. student.name = "John";
20. System.out.println(student.getPassword());
21. }
22. }
23.
24. // 抽象类定义
25. abstract class Animal {
26. public abstract void sound();
27.
28. public void eat() {
29. System.out.println("动物吃东西");
30. }
31. }
32.
33. class Dog extends Animal {
34. public void sound() {
35. System.out.println("汪汪");
36. }
37.
38. public void bark() {
39. System.out.println("大声叫");
40. }
41. }
42.
43. // 静态变量使用
44. class Counter {
45. public static int count = 0; // 应为static
46.
47. public Counter() {
48. count++;49. }
50. }
51.
52. // 接口定义
53. interface MyInterface {
54. void method1();
55. void method2();
56. }
57.
58. // 封装性错误
59. class Student {
60. private String name;
61. private String password;
62.
63. public String getName() {
64. return name;
65. }
66.
67. private String getPassword() {
68. return password;
69. }
70. }
71.
72. // final使用错误
73. class Base {
74. public final void display() {
75. System.out.println("Base display");
76. }
77. }
78.
79. class Derived extends Base {
80. public void display() {
81. System.out.println("Derived display");
82. }
83. }
84.
85. // 构造方法错误
86. class TestClass {
87. private int value;
88.
89. public TestClass(int value) {
90. this.value = value;
91. }
92.
93. public void show() {94. System.out.println(value);
95. }
96.
97. // 缺少无参构造方法,但main中尝试使用 // 错误9
98. }
99.
100. // 数组协变错误
101. class Main {
102. public static void main(String[] args) {
103. Object[] objArray = new String[5];
104. objArray[0] = new Integer(10);
105. }
106. }