请编写Java程序,实现Point类和ColorPoint类的代码继承重用之后的多态性,Point类可以携带x,y参数,有1个

2024-12-31 18:51:08
推荐回答(1个)
回答1:

public class TestColor{
public static void main(String[] args){
point p= new point(3.13,6.29);
System.out.println(p.print());

colorpoint cp = new colorpoint(4.80,8.19,"green");
System.out.println(cp.print());
}
}
class point{
public double x;
public double y;
public point(double x,double y){
this.x=x;
this.y=y;}
public String print(){
return "这是一个点point:( "+x+" , "+y+")";}
}
class colorpoint extends point{
public String color="green";
public colorpoint(double x,double y,String color){
super(x,y);
this.color=color;}
public String print(){
return "这是一个有颜色的点colorpoint:( "+x+" , "+y+") , color:"+color;}
}