从另一个类(java)访问数据?

时间:2015-06-02 02:10:16

标签: java class public

我创建了一个"父母" class(不确定这是否是正确的术语)代表3d空间中的一个点,如下所示:

(loop [urls '(start-url) index n]

我有两个类定义了两个不同的点(x,y,z) 头等舱:

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;
    }

}

第二课

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() + ")");



    }

}

现在我想创建一个新类,它获取信息为x,y,z坐标的信息 从第一类(公共类第一点)以及第二类(公共类第二点)

并用它来计算两点之间的距离。

如何在新班级中访问该信息?

6 个答案:

答案 0 :(得分:2)

如果要计算两点之间的距离,可以在Point3d类中创建一个函数。例如:

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)
    { 
        // ......
    }
    //......
}

然后你只需要创建一个Test Class,在Test Class中你可以创建两个Point3d实例,例如:

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); 

   }
 }

答案 1 :(得分:1)

public static void main不属于您定义的类,它是java程序的第一个执行点。如果您需要访问两个Point3d类,您可以在main方法中执行以下操作。

    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() + ")");

   }

答案 2 :(得分:1)

您的FirstPointSecondPoint课程根本不需要存在。它们只是Point3D的对象实例。距离计算可以是Point3D类中的方法。

所以:

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));

另请注意toString实现为点和ORIGIN常量提供格式化输出。

这种方法的一个(巨大的)优势在于包括' setter'成员变量的方法是Point3D是不可变的。这意味着传递Point3D对象引用的任何方法都可以依赖它永远不会改变值,这使得使用它们变得更加简单。理想情况下,你应该没有“吸气者”。要么 - 你可能需要获得单独坐标的所有内容应该是Point3D内的方法。但是,在所有情况下都不可能。

答案 3 :(得分:0)

你的两个非Point"类"只不过是静态主要方法的存储库,这些野兽无法以任何有意义的方式组合在一起。如果你想要任何有意义的信息和数据共享,那么这些类应该是真正的OOP兼容类,包括字段,haivng Point3D字段,根据需要完成getter和setter方法,以及需要的构造函数和方法。

如,

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;
   }
}

也许......

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

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

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

答案 4 :(得分:0)

您不能拥有多个主要课程。你的意思是这样吗?

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 + ")");
    }

}

答案 5 :(得分:0)

每次实例化对象时都不需要创建新类。此外,您只需要public static void main()作为程序的入口点。这是执行开始的地方,但并不意味着每个类都需要一个。

此外,这是构建Point3d

的正确方法
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()));
    }
}