Java入门:定义MyPoint类
定义MyPoint类(表示点),重载toString()方法。在TestMyPoint.java的main方法中实例化MyPoint类的对象start和end,并通过字符...
定义MyPoint类(表示点),重载toString()方法。在TestMyPoint.java的main方法中实例化MyPoint类的对象start和end,并通过字符串连接运算输出如下格式:
start point is start[x,y]
end point is end[x,y] 展开
start point is start[x,y]
end point is end[x,y] 展开
3个回答
展开全部
MyPoint.java
public class MyPoint{
private double x;
private double y;
public MyPoint(){
this.x=0;
this.y=0;
}
public MyPoint(double x, double y){
this.x=x;
this.y=y;
}
public double getX(){
return this.x;
}
public double getY(){
return this.y;
}
public void setX(double x){
this.x=x;
}
public void setY(double y){
this.y=y;
}
public String toString(){
return "["+this.x+","+this.y+"]";
}
}
TestMyPoint.java
public class TestMyPoint{
public static void main(String[] args){
Point start=new Point();
Point end=new Point(2,3);
System.out.println("start point is start"+start.toString());
System.out.println("end point is end"+end.toString());
}
}
public class MyPoint{
private double x;
private double y;
public MyPoint(){
this.x=0;
this.y=0;
}
public MyPoint(double x, double y){
this.x=x;
this.y=y;
}
public double getX(){
return this.x;
}
public double getY(){
return this.y;
}
public void setX(double x){
this.x=x;
}
public void setY(double y){
this.y=y;
}
public String toString(){
return "["+this.x+","+this.y+"]";
}
}
TestMyPoint.java
public class TestMyPoint{
public static void main(String[] args){
Point start=new Point();
Point end=new Point(2,3);
System.out.println("start point is start"+start.toString());
System.out.println("end point is end"+end.toString());
}
}
展开全部
class MyPoint {
String name;
int x;
int y;
public MyPoint(String name,int x,int y){
this.name = name;
this.x = x;
this.y = y;
}
public String toString(){
return this.name+" point is "+this.name+"["+this.x+","+this.y+"]";
}
}
public class TestMyPoint{
public static void main(String[] args) {
MyPoint start = new MyPoint("start",1,2);
MyPoint end = new MyPoint("end",3,4);
System.out.println(start);
System.out.println(end);
}
}
String name;
int x;
int y;
public MyPoint(String name,int x,int y){
this.name = name;
this.x = x;
this.y = y;
}
public String toString(){
return this.name+" point is "+this.name+"["+this.x+","+this.y+"]";
}
}
public class TestMyPoint{
public static void main(String[] args) {
MyPoint start = new MyPoint("start",1,2);
MyPoint end = new MyPoint("end",3,4);
System.out.println(start);
System.out.println(end);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class TestMyPoint {
public static void main(String[] args){
Mypoint start=new Mypoint(3,4);
Mypoint end=new Mypoint(7,10);
System.out.println("start point is start"+start.toString());
System.out.print("end point is end"+end.toString());
}
}
class Mypoint {
int x;
int y;
Mypoint(int x,int y){
this.x=x;
this.y=y;
}
public String toString(){
return ("["+x+","+y+"]");
}
}
public static void main(String[] args){
Mypoint start=new Mypoint(3,4);
Mypoint end=new Mypoint(7,10);
System.out.println("start point is start"+start.toString());
System.out.print("end point is end"+end.toString());
}
}
class Mypoint {
int x;
int y;
Mypoint(int x,int y){
this.x=x;
this.y=y;
}
public String toString(){
return ("["+x+","+y+"]");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询