Topic: OO question.

  Print this page

1.OO question. Copy to clipboard
Posted by: jam
Posted on: 2003-06-17 16:30

class A {
int i = 1;
int f() {
return i;
}
static char g() {
return 'A';
}
}

class B extends A {
int i = 2;
int f() {
return -i; // ----- -i not -1 , sorry
}
static char g() {
return 'B';
}
}

public class Test {
public static void main(String args[]) {
B b = new B();
A a = (A) b;
System.out.println(a.i); // 1 ------------ can use ? to explain
System.out.println(a.f()); // -2 ------------ can use 'polymorphism' to explain
System.out.println(a.g()); // A ------------ can use ? to explain
System.out.println(A.g()); // A ------------- can use 'static' to explain
}
}

I had been post before. I was uncertain of reason.

2.Re:OO question. [Re: jam] Copy to clipboard
Posted by: qindf
Posted on: 2003-06-17 17:23

属性变量和静态方法不会被覆写。调用将根据当时“标识的类”来判断其成员。

3.Re:OO question. [Re: jam] Copy to clipboard
Posted by: samx
Posted on: 2003-06-17 18:06

I run this file and found the second row's output is -1, not -2.

4.Re:OO question. [Re: samx] Copy to clipboard
Posted by: jam
Posted on: 2003-06-17 18:45

samx wrote:
I run this file and found the second row's output is -1, not -2.


sorry method f() is -i not -1

5.Re:OO question. [Re: jam] Copy to clipboard
Posted by: archonLing
Posted on: 2003-06-18 09:27

Inheritance, I think. When you cast the child class into its parent object, it uses the parent's methods & variables.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923