简体   繁体   中英

Can we use method of other class in constructor of other class?

I am just a Java beginner. While doing some practice exercises, I encountered confusion: can we use a method of one class in constructor of another class with just class reference? If not, why so? Below let me try to explain:

public class MyCircle {

    private int radius;
    private Point centre;

    public MyCircle(int x,int y,int r){
        **centre.setX(x);**
        **centre.setY(y);**
        radius=r;
    }
    public MyCircle(Point p,int r){
        centre=p;
        radius=r;
    }
    public int getRaduis(){
        return radius;
    }
    public void setRadius(int r){
        radius=r;
    }
    public Point getCentre(){
        return centre;

    }
    public void setCentre(Point p){
        centre= p;

    }
    public int getCenterX(){
        return centre.getX();
    }
    public int getCenterY(){
        return centre.getY();

    }
     public void setCentreXY(int x,int y){
        centre.setXY(x,y);

    }
     public double getArea(){
         return Math.PI*radius*radius;
     }
}

In the first constructor centre.setX() and center.setY() shows error while compiling, were as if I put center = new Point(x,y) the program compiles easily.

Also the methods center.setX() and center.setY() in setcenterXY() doesn't create any problem.

below is Point class:

public class Point {

     private int x;
     private int y;

    public Point(){
        x=8;
        y=9;
    }
    public Point(int x,int y){
        this.x=x;
        this.y=y;
    }

    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }

    public void setX(int x){
       this.x=x;
    }
    public void setY(int y){
        this.y=y;       
    }
    public void setXY(int x,int y){
        this.x=x;
        this.y=y;
    }

    public double distanceXY(int x,int y){
        int diffX=this.x-x;
        int diffY=this.y-y;
        return Math.sqrt(diffX*diffX+diffY*diffY);
    }

    public double distanceXY(Point p){
        int diffX=this.x-p.x;
        int diffY=this.y-p.y;
        return Math.sqrt(diffX*diffX+diffY*diffY); 
    }
}

As the comments to your post described, calling centre.setX(x) and centre.setY(y) will not work because you have not initialised centre , so there is no object for which to call the setX(x) and setY(y) methods on.

It's a simple fix though, just initialise centre with the values passed into the constructor, like so:

public MyCircle(int x, int y, int r){
    this.centre = new Point(x, y);
    this.radius = r;
}

The way to do what you want is to use the Point constructor from the MyCircle constructor:

public class MyCircle {

  private int radius;
  private Point centre;

  public MyCircle(int x, int y, int r){
    center = new Point(x,y);
    radius = r;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM