java中调用父类构造方法?
classPoint{intx,y;staticintc;publicPoint(intm,intn){x=m;y=n;}publicPoint(){c++;System...
class Point{ int x,y; static int c; public Point(int m,int n) { x=m; y=n; } public Point() { c++; System.out.println("第"+c+"次被创建子类对象"); }}class Point3D extends Point{ public static void main(String [] args) { Point3D m1=new Point3D(); Point3D m2=new Point3D(); Point3D m3=new Point3D(); System.out.println(Point.c); }}怎么调用父类的x和y来子类输出??带参数那个构造方法怎么调用进子类来输出- -谁帮我改改
展开
展开全部
非静态的变量在别的类里面是不能直接用的。。若要使用的话,就要通过实例化该父类,通过【实例名.属性名】来访问、、、你自己看一下把!! class
Point{
int
x,y;
static
int
c;
public
Point(int
m,int
n){
x=m;
y=n;
System.out.println("带参数的父类给实例化了。x:"+x+"
y:"+y);
}
public
Point(){
c++;
System.out.println("第"+c+"次被创建子类对象");
}
}
public
class
Point3D
extends
Point{
public
static
void
main(String
[]
args){
Point
m1=new
Point3D();
Point
m2=new
Point3D();
Point
m3=new
Point3D();
System.out.println(Point.c);
Point
m4
=
new
Point(7
,
5);
System.out.println("父类x属性的值为:"+m4.x);
System.out.println("父类y属性的值为:"+m4.y);
}
}
Point{
int
x,y;
static
int
c;
public
Point(int
m,int
n){
x=m;
y=n;
System.out.println("带参数的父类给实例化了。x:"+x+"
y:"+y);
}
public
Point(){
c++;
System.out.println("第"+c+"次被创建子类对象");
}
}
public
class
Point3D
extends
Point{
public
static
void
main(String
[]
args){
Point
m1=new
Point3D();
Point
m2=new
Point3D();
Point
m3=new
Point3D();
System.out.println(Point.c);
Point
m4
=
new
Point(7
,
5);
System.out.println("父类x属性的值为:"+m4.x);
System.out.println("父类y属性的值为:"+m4.y);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询