# 我只有这么多了？不会让我头一次到Java里问兄弟们就不给面子哈？

NewOracler 2002-03-20 10:26:41
class Point{
int x;
int y;
Point(int x,int y){
this.x=x;
this.y=y;
}
Point(int x){
this(x,0);
}
Point(){
this(0,0);
}
double length(){
return Math.sqrt(x*x+y*y);
}
}
//以上是类Point的申明
class MyPoint extends Point{
//——————————Edit One--------------//
//int x;
//int y;
//——————————Edit One--------------//
int x=100;
int y=100;
MyPoint(int x,int y){
//----------------Edit Two-------------//
//this.x=super.x=x;
//this.y=super.y=y;
//_________________Edit Two-----------//
//学习于2002-3-20日
this.x=x;
this.y=y;
}
double length(){
return Math.sqrt(x*x+y*y);
}
double distance(){
return Math.abs(length()-super.length());
}
}
//以上是类MyPoint的申明
class PointTest{
public static void main(String[] args){
MyPoint mp = new MyPoint(4,3);
Point p = new Point(1,1);
Point q = mp;
mp.x=5;
mp.y=12;
System.out.println("\n\tData Access Test:\n");
System.out.println("mp=("+mp.x+","+mp.y+")");
System.out.println("p=("+p.x+","+p.y+")");
System.out.println("q=("+q.x+","+q.y+")");
System.out.println("\n\tCasting Test:\n");
System.out.println("(Point) mp=("+((Point)mp).x+","+((Point)mp).y+")");
System.out.println("(MyPoint)q="+((MyPoint)q).x+","+((MyPoint)q).y+")");
System.out.println("\n\tMethod Member Access Test:\n");
System.out.println("mp.length()="+mp.length());
System.out.println("p.length()="+p.length());
System.out.println("q.length()="+q.length());
System.out.println("mp.distance()="+mp.distance());
}
}

1：为什么在System.out.println("q=("+q.x+","+q.y+")");中输出是：4，3
2：我现在修改:参见Edit One 和Edit Two这里！结果输出是：System.out.println("q=("+q.x+","+q.y+")");是：0，0

...全文
30 点赞 收藏 10

10 条回复

fox_blue 2002-03-27

fox_blue 2002-03-27
MyPoint mp = new MyPoint(4,3);

Point.Point()---〉Point.Point(0,0)---〉MyPoint.MyPoint(4,3);

Point q = mp; 可是q.x,q.y 仍旧引用的是 超类中的成员 (0,0)

hujianok 2002-03-26

bluebird97073(烂鸟) 把this.x=super.x=x;　解释为下面这样是站不住脚的：
this.x=super.x; (super.x=0。所以this.x=0）
super.x=x;

super.x=x;
this.x=super.x;

JunyLee 2002-03-26
NewOracler(一半是青蛙，一半是MM) ：

NewOracler 2002-03-21
JunyLee(沙子) ：

bluebird97073 2002-03-20
1.mp.x=4.mp.y=3

q和mp指向同一对象~~所以q.x=4,q.y=3

2.

this.x=super.x; (super.x=0。所以this.x=0）
super.x=x;

promark 2002-03-20
to NewOracler

Data Access Test:

mp=(5,12)
p=(1,1)
q=(0,0)

Casting Test:

(Point) mp=(0,0)
(MyPoint)q=5,12)

Method Member Access Test:

mp.length()=13.0
p.length()=1.4142135623730951
q.length()=13.0
mp.distance()=13.0

（把子类对象转为父类对象）再使用x,y，就是父类的x,y,当然为0了

NewOracler 2002-03-20

JunyLee 2002-03-20

NewOracler 2002-03-20
To:promark()

class Point{
int x;
int y;
Point(int x,int y){
this.x=x;
this.y=y;
}
Point(int x){
this(x,0);
}
Point(){
this(0,0);
}
double length(){
return Math.sqrt(x*x+y*y);
}
}
//以上是类Point的申明
class MyPoint extends Point{
int x;
int y;
MyPoint(int x,int y){
//学习于2002-3-20日
this.x=super.x=x;
this.y=super.y=y;
//学习于2002-3-20日
//this.x=x;
//this.y=y;
}
double length(){
return Math.sqrt(x*x+y*y);
}
double distance(){
return Math.abs(length()-super.length());
}
}
//以上是类MyPoint的申明
class PointTest{
public static void main(String[] args){
MyPoint mp = new MyPoint(4,3);
Point p = new Point(1,1);
Point q = mp;
mp.x=5;
mp.y=12;
System.out.println("\n\tData Access Test:\n");
System.out.println("mp=("+mp.x+","+mp.y+")");
System.out.println("p=("+p.x+","+p.y+")");
System.out.println("q=("+q.x+","+q.y+")");
System.out.println("\n\tCasting Test:\n");
System.out.println("(Point) mp=("+((Point)mp).x+","+((Point)mp).y+")");
System.out.println("(MyPoint)q="+((MyPoint)q).x+","+((MyPoint)q).y+")");
System.out.println("\n\tMethod Member Access Test:\n");
System.out.println("mp.length()="+mp.length());
System.out.println("p.length()="+p.length());
System.out.println("q.length()="+q.length());
System.out.println("mp.distance()="+mp.distance());
}
}

（我在研究java的面向对象的特性）

2.3w+

Java 非技术区

2002-03-20 10:26