class Point{ protected int x,y; public Point(){} public Point(int xx,int yy){setPoint(xx,yy);}public void setPoint(int m,int n){x=m;y=n;}public int getX(){return x;}public int getY(){return y;}}class Circle extends point{private double radius;public
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/19 23:06:29
class Point{ protected int x,y; public Point(){} public Point(int xx,int yy){setPoint(xx,yy);}public void setPoint(int m,int n){x=m;y=n;}public int getX(){return x;}public int getY(){return y;}}class Circle extends point{private double radius;public
class Point{ protected int x,y; public Point(){} public Point(int xx,int yy){setPoint(xx,yy);}
public void setPoint(int m,int n){x=m;y=n;}
public int getX(){return x;}
public int getY(){return y;}}
class Circle extends point{
private double radius;
public Circle(int x,int y,double r){this.x=x;this.y=y;steRadius(r);}
public void setRadius(double r){radius=r;}
public double getRadius(){retuen radius;}
public double getArea(){retuen 3.1415*radius*radius;}
public String toString()
{
retuen "Poistion("+x+","+y+")Radius="radius;}}
public class CircleTest
{
public static void main(String args[]){
Circle c=new Circle(50,50,10);
System.out.prinrln(c.toString());
c.setPoint(100,100);
c.setRadius(20);
System.out.println(c.toString());
}}
1:父类的public Point(){}构造方法为什么不能省略,它是初始化谁的?
2:父类的public Point(int xx,int yy){setPoint(xx,yy);}构造方法是初始化谁的?为什么形参是(xx,yy)而不是(g,h)?
class Point{ protected int x,y; public Point(){} public Point(int xx,int yy){setPoint(xx,yy);}public void setPoint(int m,int n){x=m;y=n;}public int getX(){return x;}public int getY(){return y;}}class Circle extends point{private double radius;public
1.子类构造函数一般要调用父类的构造函数;当子类没有显式调用时,默认调用父类的无参构造函数,此时要是没有父类的无参构造函数,会出错.
2.父类的public Point(int xx,int yy){setPoint(xx,yy);}是设置成员变量x,y的值,形参是什么无所谓
不建议构造函数这样写;一般应该这样写:
public Point (int x,int y) { this.x=x; this.y=y }
public setX (int x) { this.x=x; }
public setY( int y) { this.y=y; }
public int getX() { return this.x; }
public int getY() { return this.y; }