我只有这么多了?不会让我头一次到Java里问兄弟们就不给面子哈?
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
谢谢大家了!
我只有这么多分了!