小编syg*_*ede的帖子

java中的私有实例和getter可见性

并感谢每个人修复格式等,这里全新

我最近开始学习java,在一次练习中发生了一个问题,抱歉,如果我错过了发布规则:

从一个MyPoint到另一个Mypoint计算距离,我决定使用用于MyPoint吸气另一个因为x和y为另一个应该是私有的,并且不能在点操作(another.x another.y)中使用;

public class MyPoint {
    private int x;
    private int y;                   

    public int getX() {             
        return x;
    }

    public int getY() {            
        return y;
    } 

    public double distance(MyPoint another) { 
        int xDiff = this.x - another.getX();           //getter
        int yDiff = this.y - another.getY();          // getter

        return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
    }
}

public class TestMyPoint {
    public static void main(String[] args) {
        MyPoint a = new MyPoint(3,0);
        MyPoint b = …
Run Code Online (Sandbox Code Playgroud)

java oop getter private

2
推荐指数
1
解决办法
179
查看次数

标签 统计

getter ×1

java ×1

oop ×1

private ×1