「java定义点」java点的作用
今天给各位分享java定义点的知识,其中也会对java点的作用进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
Java定义一个Point(点)类
public class Point
{
public static void main(String[] args)
{
Point p1=new Point();
Point p2=new Point(1,2);
p1.show();
p1.move(3,4);
p1.show();
p2.show();
p2.move(5,6);
p2.show();
}
Point()
{
this(0,0);
}
Point(float x,float y)
{
this.x=x;
this.y=y;
}
void move(float x,float y)
{
this.x=x;
this.y=y;
}
void show()
{
System.out.printf("(%f,%f)",x,y);
System.out.println();
}
private float x,y;
}
用JAVA定义一个描述点的point类.它有两个成员变量:横坐标x,纵坐标y
实现思路就是有一个类Point,之后这个类有两个私有成员变量,之后可以通过set和get方法进行赋值和数值读取:
public
class
Point
{
private
String
x;
private
String
y;
/**
*
默认构造函数
*/
public
Point(){
}
/**
*有x和y的构造方法。
*/
public
Point(String
x,String
y){
this.x=x;
this.y=y;
}
public
String
getX()
{
return
x;
}
public
void
setX(String
x)
{
this.x
=
x;
}
public
String
getY()
{
return
y;
}
public
void
setY(String
y)
{
this.y
=
y;
}
}
JAVA编程定义一个点类。
你的第二个方法要求描述不太明白,我就按照自己的理解写了一个。
我偷懒就直接在main方法里面写测试代码了,你需要的话就再自己定义一个Test类写个mian方法,内容其实没什么区别。
public class Point {
private double x;
private double y;
public Point(double x, double y) {
super();
this.x = x;
this.y = y;
}
public double distance(Point point) {
return Math.sqrt((point.x - this.x) * (point.x - this.x) + (point.y - this.y) * (point.y - this.y));
}
public Point move(double x, double y) {
return new Point(this.x + x, this.y + y);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "(" + x + ", " + y + ")";
}
public static void main(String[] args) {
Point p1 = new Point(5, 6);
Point p2 = new Point(-2, -9);
System.out.println(p1.distance(p2));
System.out.println(p1.move(11, -2));
}
}
测试结果:
16.55294535724685
(16.0, 4.0)
Java的概念和定义是什么?
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程 。
java定义点的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java点的作用、java定义点的信息别忘了在本站进行查找喔。
发布于:2022-11-26,除非注明,否则均为
原创文章,转载请注明出处。