Java面向对象编程基础练习有哪些?
- 内容介绍
- 文章标签
- 相关推荐
本文共计280个文字,预计阅读时间需要2分钟。
定义Point类,表示二维坐标系中的一个点,具有属性横坐标x和纵坐标y,并提供用来获取和设置坐标值以及计算到原点距离平方的方法。定义一个构造方法初始化x和y,在主类中创建Point对象并初始化x和y值。
public class point { double x,y; public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } public point(double x, double y) { super(); this.x = x; this.y = y; } double distance(){ return Math.sqrt((x*x+y*y)); } } package gh; public class text4 { public static void main(String[] args) { point mypoint1=new point(3.2,4.5); point mypoint2=new point(5.2,4.1); System.out.println("X:"+mypoint1.getX()+" Y:"+mypoint1.getY()+" distance:"+mypoint1.distance()); System.out.println("X:"+mypoint2.getX()+" Y:"+mypoint2.getY()+" distance:"+mypoint2.distance()); } }
本文共计280个文字,预计阅读时间需要2分钟。
定义Point类,表示二维坐标系中的一个点,具有属性横坐标x和纵坐标y,并提供用来获取和设置坐标值以及计算到原点距离平方的方法。定义一个构造方法初始化x和y,在主类中创建Point对象并初始化x和y值。
public class point { double x,y; public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } public point(double x, double y) { super(); this.x = x; this.y = y; } double distance(){ return Math.sqrt((x*x+y*y)); } } package gh; public class text4 { public static void main(String[] args) { point mypoint1=new point(3.2,4.5); point mypoint2=new point(5.2,4.1); System.out.println("X:"+mypoint1.getX()+" Y:"+mypoint1.getY()+" distance:"+mypoint1.distance()); System.out.println("X:"+mypoint2.getX()+" Y:"+mypoint2.getY()+" distance:"+mypoint2.distance()); } }

