当前位置: 技术问答>java相关
SCJP(YY3)
来源: 互联网 发布时间:2015-03-03
本文导语: What is the output of the following program? 1. class Test { 2. void show() { 3. System.out.println("non-static method in Test"); 4. } 5. } 6. public class Q3 extends Test { 7. static void show() { 8. System.out.println("Overridden non...
What is the output of the following program?
1. class Test {
2. void show() {
3. System.out.println("non-static method in Test");
4. }
5. }
6. public class Q3 extends Test {
7. static void show() {
8. System.out.println("Overridden non-static method in Q3");
9. }
10. public static void main(String[] args) {
11. Q3 a = new Q3();
12. }
13 }
a. Compilation error at line 2.
b. Compilation error at line 7.
c. No compilation error, but runtime exception at line 2.
d. No compilation error, but runtime exception at line 7.
答案是B,为什么呢?
You are creating a ToolBase class which will be extended by other programmers. The ToolBase class contains a single abstract method, createTool.
Which of the following statements are true?
a. The ToolBase class must be declared abstract.
b. Classes extending ToolBase must not be declared abstract.
c. The ToolBase class must not be declared final.
d. The following variable declaration is illegal in any context "ToolBase myTB;"
答案是A,C,D。我觉得D不对,因为TOOLBASE是抽象类啊。
1. class Test {
2. void show() {
3. System.out.println("non-static method in Test");
4. }
5. }
6. public class Q3 extends Test {
7. static void show() {
8. System.out.println("Overridden non-static method in Q3");
9. }
10. public static void main(String[] args) {
11. Q3 a = new Q3();
12. }
13 }
a. Compilation error at line 2.
b. Compilation error at line 7.
c. No compilation error, but runtime exception at line 2.
d. No compilation error, but runtime exception at line 7.
答案是B,为什么呢?
You are creating a ToolBase class which will be extended by other programmers. The ToolBase class contains a single abstract method, createTool.
Which of the following statements are true?
a. The ToolBase class must be declared abstract.
b. Classes extending ToolBase must not be declared abstract.
c. The ToolBase class must not be declared final.
d. The following variable declaration is illegal in any context "ToolBase myTB;"
答案是A,C,D。我觉得D不对,因为TOOLBASE是抽象类啊。
|
关于override是有规则的
静态的方法不能覆盖成非静态,所以会编译错误
2)关于答案D,是对的,他是声明myTB是TOOLBASE型的,并没有对他初始化成抽象类的事例
如果toolbase mytb = new toobase()就肯定是错的
现在声明了,还没初始化,缺省初始化为NULL,所以合法
懂了吗?
静态的方法不能覆盖成非静态,所以会编译错误
2)关于答案D,是对的,他是声明myTB是TOOLBASE型的,并没有对他初始化成抽象类的事例
如果toolbase mytb = new toobase()就肯定是错的
现在声明了,还没初始化,缺省初始化为NULL,所以合法
懂了吗?
|
1。不能用静态方法覆盖非静态方法
2。抽象类不能实例化,但“ToolBase myTB“只是声明了一个引用。
2。抽象类不能实例化,但“ToolBase myTB“只是声明了一个引用。
|
同上
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。