如何将14个Java基础面向对象练习改写成长尾词?
- 内容介绍
- 文章标签
- 相关推荐
本文共计255个文字,预计阅读时间需要2分钟。
pythonclass Point: def __init__(self, x, y): self.x=x self.y=y
def get_coordinates(self): return self.x, self.y
def set_coordinates(self, x, y): self.x=x self.y=y
def distance_to_origin(self): return (self.x 2 + self.y 2) ** 0.5
定义Point类,表示二维坐标中的一个点,有属性横 * 坐标x和纵坐标y,还有用来获取和设置坐标值,以 * 及计算到原点距离平方值的方法。定义一个构造方 * 法初始化x和y。在主类中创建两个点对象,分别使 * 用两个对象调用相应方法,输出x和ypublic 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()); } }
本文共计255个文字,预计阅读时间需要2分钟。
pythonclass Point: def __init__(self, x, y): self.x=x self.y=y
def get_coordinates(self): return self.x, self.y
def set_coordinates(self, x, y): self.x=x self.y=y
def distance_to_origin(self): return (self.x 2 + self.y 2) ** 0.5
定义Point类,表示二维坐标中的一个点,有属性横 * 坐标x和纵坐标y,还有用来获取和设置坐标值,以 * 及计算到原点距离平方值的方法。定义一个构造方 * 法初始化x和y。在主类中创建两个点对象,分别使 * 用两个对象调用相应方法,输出x和ypublic 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()); } }

