简体   繁体   中英

Accessing data from another class (java)?

I have created a "parent" class (not sure if this is proper terminology) which represents a point in 3d space like this:

public class Point3d {

    private double xCoord;
    private double yCoord;
    private double zCoord;


    public Point3d(double x,double y, double z ){
        xCoord = x;
        yCoord = y;
        zCoord = z;
    }

    public Point3d() {
        this (0,0,0);
    }


    public double getxCoord() {
        return xCoord;
    }


    public double getyCoord() {
        return yCoord;
    }



    public double getzCoord() {
        return zCoord;
    }

    public void setX(double val){
        xCoord= val;

    }
    public void setY(double val){
        yCoord= val;
    }
    public void setZ(double val){
        zCoord= val;
    }

}

I have two classes which have defined two different points (x,y,z) First class:

package threedimpoint;

public class FirstPoint {

    public static void main(String[] args) {

        Point3d frst = new Point3d();

        frst.setX(0);
        frst.setY(0);
        frst.setZ(0);

        System.out.println("(" + frst.getxCoord() + "," + frst.getyCoord() + "," + frst.getzCoord() + ")");



    }

}

second class

package threedimpoint;

public class Secondpoint {

    public static void main(String[] args) {

        Point3d secnd = new Point3d();

        secnd.setX(2);
        secnd.setY(12);
        secnd.setZ(24);

        System.out.println("(" + secnd.getxCoord() + "," + secnd.getyCoord() + "," + secnd.getzCoord() + ")");

    }

}

Now I wish to create a new class which takes the information that information being the x,y,z coordinates from the first class (public class first point) as well as from the second class (public class second point)

and use that to calculate the distances between the two points.

How do I access that information in a new class?

If you want to calculate the distances between the two points, you can create a function in the class Point3d. For example:

public class Point3d {

    private double xCoord;
    private double yCoord;
    private double zCoord;


    public Point3d(double x,double y, double z ){
        xCoord = x;
        yCoord = y;
        zCoord = z;
    }

    public double calucateDistance(Point3d compareObj)
    { 
        // ......
    }
    //......
}

Then you only need to create a Test Class, in the Test Class you can create two instance of Point3d, For Example :

public class Test{

   public static void main(String[] args) {

      Point3d first = new Point3d();        
      first .setX(0);

      first .setY(0);

      first .setZ(0); 

      Point3d second = new Point3d(); 

      second.setX(2);

      second.setY(12);

      second.setZ(24);

      double result = first.calucateDistance(second); 

   }
 }

public static void main is not belongs to the class you defined, it's the first execution point of java program. If you need to access two Point3d classes you can do as below in the main method.

    public static void main(String[] args) {

       Point3d frst = new Point3d();

       frst.setX(0);
       frst.setY(0);
       frst.setZ(0);

       System.out.println("(" + frst.getxCoord() + "," + frst.getyCoord() + "," + frst.getzCoord() + ")");


       Point3d secnd = new Point3d();

       secnd.setX(2);
       secnd.setY(12);
       secnd.setZ(24);

       System.out.println("(" + secnd.getxCoord() + "," + secnd.getyCoord() + "," + secnd.getzCoord() + ")");

   }

Your FirstPoint and SecondPoint classes really don't need to exist at all. They are just object instances of Point3D . The distance calculation can be a method in the Point3D class.

So:

class Point3D {
    private final double x;
    private final double y;
    private final double z;

    public static final Point3D ORIGIN = new Point3D(0, 0, 0);

    public Point3D(double x, double y, double z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public String toString() {
        return String.format("(%.2f,%.2f,%.2f)", x, y, z);
    }

    public double distanceTo(Point3D other) {
        double xDelta = other.x - this.x;
        double yDelta = other.y - this.y;
        double zDelta = other.z - this.z;
        return Math.sqrt(xDelta * xDelta + yDelta * yDelta + zDelta * zDelta);
    }
}

Point3D point = new Point3D(2, 12, 24);
System.out.println("point = " + point);
double distanceToOrigin = point.distanceTo(Point3D.ORIGIN);
double distanceToOtherPoint = point.distanceTo(new Point3D(100, 100, 100));

Also note the toString implementation to provide a formatted output for points and the ORIGIN constant.

One (massive) advantage of this approach over including 'setter' methods for your member variables is that Point3D is immutable. This means that any method passed a Point3D object reference can rely on it never changing value which makes using them much simpler. Ideally you should have no 'getters' either - everything that you might need to get the individual coordinates for should be a method inside Point3D . That's not possible in all cases however.

Both of your non-Point "classes" are nothing more than repositories for static main methods, and these beasts can't really be combined in any meaningful way. If you want any meaningful information and sharing of data, then these classes should be true OOP compliant classes with fields, say haivng Point3D fields, complete with getter and setter methods as needed, and constructors and methods as needed.

eg,

public class FirstPoint {
   private Point3d pt;

   public FirstPoint(Point3d pt) {
      this.pt = pt;
   }

   public Point3d getPt() {
      return pt;
   }

   public void setPt(Point3d pt) {
      this.pt = pt;
   }
}

and perhaps...

public class PointCollection {
   private List<Point3d> points = new ArrayList<>();

   public void add(Point3d p) {
      points.add(p);
   }

   // ..... methods to get, to iterate through,...
}

You cannot have more than one main class. Did you mean something like this?

package threedimpoint;

public class testPoint {

    public static void main(String[] args) {
        Point3d frst = new Point3d();

        frst.setX(0);
        frst.setY(0);
        frst.setZ(0);

        Point3d secnd = new Point3d();

        secnd.setX(2);
        secnd.setY(12);
        secnd.setZ(24);

        int x = frst.getxCoord() + secnd.getxCoord()
        int y = frst.getyCoord() + secnd.getyCoord()
        int z = frst.getzCoord() + secnd.getzCoord()

        System.out.println("(" + x + "," + y + "," + z + ")");
    }

}

You don't need to create a new class every time you instantiate an object. Also, you only need public static void main() as an entry point to your program. This is where the execution starts but it doesn't mean you need one for every class.

Also, here's the right way of constructing your Point3d class

public class Point3d {
    private double xCoord, yCoord, zCoord;
    public Point3D(double x, double y, double z) {
        this.xCoord = x;
        this.yCoord = y;
        this.zCoord = z;
    }
    public double getX(){return this.xCoord;}
    public double getY(){return this.yCoord;}
    public double getZ(){return this.zCoord;}

    public void setX(x){this.xCoord = x;}
    public void setY(y){this.yCoord = y;}
    public void setZ(z){this.zCoord = z;}
}

public class FirstPoint {
    public static void main(String[] args) {
        Point3d frst = new Point3d();

        frst.setX(0);
        frst.setY(0);
        frst.setZ(0);

        Point3d secnd = new Point3d();

        secnd.setX(2);
        secnd.setY(12);
        secnd.setZ(24);

        double distance = euclidean_distance(frst,secnd);
    }
    public double euclidean_distance(Point3d a, Point3d b){
        return Math.sqrt((a.getX()-b.getX())*(a.getX()-b.getX()) + (a.getY()-b.getY())*(a.getY()-b.getY()) + (a.getZ()-b.getZ())*(a.getZ()-b.getZ()));
    }
}

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