JAVA学习日记31-0805

今天学了什么

基类 Point 和派生类 Rectangle

Point类:

package rectangle;

public class Point {
protected double x,y;
public Point(double x,double y) {
this.x=x;
this.y=y;
}
public void show() {
System.out.println("Point(x="+x+",y="+y+")");
}
public double area() {
return 0;
}
public void setx(double x) {
this.x=x;
}
public double getx() {
return x;
}
public void sety(double y) {
this.y=y;
}
public double gety() {
return y;
}
}

Retangle类:

package rectangle;

public class Rectangle extends Point {
private double w,h;
public Rectangle(double x, double y,double w, double h) {
super(x, y);
this.w=w;
this.h=h;
}
public void show2() {
System.out.println("Retangle:");
show();
System.out.println("width="+w+",hight="+h);
}
public void setw(double w) {
this.w=w;
}
public double getw() {
return w;
}
public void seth(double h) {
this.h=h;
}
public double geth() {
return h;
}
public double area() {
return w*h;
}
public double length() {
return 2*(w+h);
}


public static void main(String[] args) {
Rectangle r = new Rectangle(1, 2, 3, 4);
r.show2();
System.out.println("area="+r.area()+"length="+r.length());
r.setw(5);
r.seth(6);
r.show2();
System.out.println("area="+r.area()+"length="+r.length());

}

}

运行截图:

遇到的问题

Rectangle类中 show2()开始为show()

public void show() {
System.out.println("Retangle:");
show();
System.out.println("width="+w+",hight="+h);
}

导致无限循环打印Retangle:

将show()改为show2()后,正常运行。

明天计划

练习面向对象练习题

原文地址:https://www.cnblogs.com/a8047/p/13439888.html