Test1.javaclass Base
{
int i = 47;
int f()
{
return i;
}
}class Derived extends Base
{
int i = 27;
int g()
{
return i;
}
}public class Test1
{
public static void main(String[] args)
{
Derived d = new Derived();
System.out.println(d.f());
}
}
Test2.javaclass Base
{
int i = 47;
int f()
{
return g();
}
int g()
{
return i;
}
}class Derived extends Base
{
int i = 27;
int g()
{
return i;
}
}public class Test2
{
public static void main(String[] args)
{
Derived d = new Derived();
System.out.println(d.f());
}
}
Test3.javaclass Base
{
int i = 47;
int f()
{
return g();
}
private int g()
{
return i;
}
}class Derived extends Base
{
int i = 27;
int g()
{
return i;
}
}public class Test3
{
public static void main(String[] args)
{
Derived d = new Derived();
System.out.println(d.f());
}
}
{
int i = 47;
int f()
{
return i;
}
}class Derived extends Base
{
int i = 27;
int g()
{
return i;
}
}public class Test1
{
public static void main(String[] args)
{
Derived d = new Derived();
System.out.println(d.f());
}
}
Test2.javaclass Base
{
int i = 47;
int f()
{
return g();
}
int g()
{
return i;
}
}class Derived extends Base
{
int i = 27;
int g()
{
return i;
}
}public class Test2
{
public static void main(String[] args)
{
Derived d = new Derived();
System.out.println(d.f());
}
}
Test3.javaclass Base
{
int i = 47;
int f()
{
return g();
}
private int g()
{
return i;
}
}class Derived extends Base
{
int i = 27;
int g()
{
return i;
}
}public class Test3
{
public static void main(String[] args)
{
Derived d = new Derived();
System.out.println(d.f());
}
}
解决方案 »
- 求一正则,两个数字和两个字符组成的四个字符,无序,谢谢
- 请问有支持VISTA操作系统的ORACLE版本吗?有的话给个链接好吗,谢谢
- 8.制作一个java应用程序,模拟实现数据链表的数据读写,即对某堆栈的setList()和getList();
- 请教JTree如何实现两棵树之间的互相选择?(内详)
- system.arraycopy定义好像有点错误
- 一个小算法问题,求救,四个数字相加哦
- 解决马上给分
- @#@!做了个画图可以保存和读取的java程序 但每次画的线都会覆盖上次画的 完美解决送100分啊
- java的变量问题。。。。。。。。。。。。。。。
- 协作开发:NetCaller(基于java技术的点对点聊天程序)
- 有个疑问,请大家帮忙解答
- java.lang.NullPointerException
楼主【tanlingyun】截止到2008-06-24 20:12:17的历史汇总数据(不包括此帖):
发帖数:23 发帖分:1585
结贴数:23 结贴分:1585
未结数:0 未结分:0
结贴率:100.00% 结分率:100.00%
敬礼!
Test2 27
Test3 47
{
int i = 47;
int f()
{
return i;//调用自己的i,这里无法返回 Derived中的i
}
}class Derived extends Base
{
int i = 27;
int g()
{
return i;
}
}public class Test1
{
public static void main(String[] args)
{
Derived d = new Derived();
System.out.println(d.f());
}
}
这里应该输出 27 , 具体描述见下面的注释class Base
{
int i = 47;
int f()
{
return g();//调用的是Derived中的g(),因为Base中的g()已经被子类Derived重载,而且在Test2中实例化的是Derived,所以应该调用Derived自己的g();构成重载的条件见以下注释
}
int g()
{
return i;
}
}class Derived extends Base
{
int i = 27;
int g()
{
return i;
}
}public class Test2
{
public static void main(String[] args)
{
Derived d = new Derived();
System.out.println(d.f());
}
}
这里应该输出 47 , 具体描述见下面的注释class Base
{
int i = 47;
int f()
{
return g();//调用的是自己的g(),如果大家对重载的概念很清晰的话,这里应该知道是怎么回事了,构成重载的条件见以下注释
}
private int g()
{
return i;;//调用自己的i,这里无法返回 Derived中的i
}
}class Derived extends Base
{
int i = 27;
int g() //由于这里g()的访问权限与父类的g()的访问权限不一样,所以不能构成重载。重载的条件是:访问权限、返回类型、方法名、参数个数都必须一样才能构成重载
{
return i;
}
}public class Test3
{
public static void main(String[] args)
{
Derived d = new Derived();
System.out.println(d.f());
}
}
27
47Test1 很简单,调用父类的f,return父类的i
Test2 调用父类的f,再调用子类的g,return子类的i
Test3 调用父类的f,再调用父类的g,(因为父类的g是private,不能被子类重写)return父类的i
2,为27,原因是d.f()调用g方法时是调用Derived的g方法(子类覆盖了父类)
3,为47,原因是由于父类的方法为private型的,不能被重写,可能为方法重载
27
47 Test1 调用父类的f,return父类的i
Test2 调用父类的f,再调用子类的g,(因为父类的g被子类重写)return子类的i
Test3 调用父类的f,再调用父类的g,(因为父类的g是private,不能被子类重写)return父类的i
2.27 . 父类的方法f()中调用了覆盖父类的方法g().所以返回子类的属性值27.
3.47 . 因为父类的方法g()是私有的,所以子类不能继承这个方法,也不能覆盖这个方法,所以调用的是父类的属性值47.
2.Test1:直接调用父类的f()
Test2:父类方法g()被重写,所以调用子类的g()
Test3:父类方法g()私有,所以并未被子类重写,仍然调用父类g()
子类不可以继承父类的private方法或属性?
是我理解错误还是?
jingulang ,看看这个:http://blog.csdn.net/sunyujia/archive/2008/06/01/2501709.aspx
{
static int i = 47;
int f()
{
return g();
}
static int g()
{
return i;
}
}class Derived extends Base
{
static int i = 27;
static int g()
{
return i;
}
}public class Test2
{
public static void main(String[] args)
{
Derived d = new Derived();
System.out.println(d.f());
}
}
1) Sun的《 Learning the Java Language》http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html2)《The Java Language Specification Third Edition》中没有找到明确说法,不过有一个例子
8.2.1.3 Inheritance with private
In the example:class Point {
int x, y;
void move(int dx, int dy) {
x += dx; y += dy; totalMoves++;
}
private static int totalMoves;
void printMoves() { System.out.println(totalMoves); }
}class Point3d extends Point {
int z;
void move(int dx, int dy, int dz) {
super.move(dx, dy); z += dz; totalMoves++;
}
}the class variable totalMoves can be used only within the class Point; it is not
inherited by the subclass Point3d. A compile-time error occurs because method
move of class Point3d tries to increment totalMoves.
27
47 Test1 很简单,调用父类的f,return父类的i
Test2 调用父类的f,再调用子类的g,return子类的i
Test3 调用父类的f,再调用父类的g,(因为父类的g是private,不能被子类调用)return父类的i
第三个应该是27
父类 用了private 此时子类就不是覆写了 而是从新定义一个新的方法
2. 静态方法和静态域只能被隐藏而不是覆盖,用哪个类名调用的就是哪个类中定义的版本。例如 Base.g() 返回 47 Derived.g() 返回 27。
3. 无论方法重载还是覆盖都不看异常抛出声明,写过 clone() 方法的人都应该注意到。
4. 方法重载(overload)只看方法签名,即方法名加参数类型列表,跟访问级别和返回值类型(统称修饰符)没关系。
5. 方法覆盖(也叫重写 override)的条件是两个方法分属子类和超类,且方法签名完全相同,返回值类型可以缩小范围例如超类方法返回值类型为 List 子类覆盖它的方法返回值类型可以是 ArrayList 类型(就是所谓的方法返回值协变 - 又一怪异的术语),方法的访问级别可以扩大例如超类方法访问级别是 protected 子类的就可以为 public。
6. 子类继承自超类的实例域虽然有值,但子类定义的方法无权访问,可以通过超类中定义的 public 或 protected 方法间接访问。
这个解释错了,应该是“Base中的g()已经被子类Derived覆盖了”,原因如上。
SCJP 不会考如此 BT 的题。
语法和语义上对继承的解释往往和 JVM 底层实现继承的原理理解上不同,《Java语言规范》和《JVM规范》是从不同层面上解释的,所以有人会感觉连官方的解释都怎么说的都有。
你的例子中注意这句 private static int totalMoves,totalMoves 是静态域不是实例域。
Test2:27 调用父类的f(),再调用子类的g(),return子类的i
Test3:47 调用父类的f(),再调用父类的g(),return父类的i