圆形类:包含圆心和半径,重写求周长和求面积的方法 矩形类:包含长和宽,重写求周长和求面积的方法 public class TestGrapher{ public static void main(String[] args) { Grapher c1=new Rectangle(3.0, 2.0, 3, 2);
c1.show();
System.out.println(\矩形的周
长:c=\矩形的面积:s=\ Grapher c2=new Circle(1.0, 2.0, 5.0);
c2.show();
System.out.println(\圆的周长:c=\圆的面积:s=\ }
}
abstract class Grapher{ abstract double getC(); abstract double getS(); abstract void show(); }
class Rectangle extends Grapher{
double l;
double d; double x; double y;
public Rectangle(double l, double d, double x, double y) {
@Override double getC() { }
@Override double getS() { }
@Override
// TODO Auto-generated method stub return l*d;
// TODO Auto-generated method stub return 2*(l+d); }
super(); this.l = l; this.d = d; this.x = x; this.y = y;
void show() {
System.out.println(\矩形,左顶点x:\左顶点y:\长:l=\宽:d=\ }
}
class Circle extends Grapher{ double x; double y; double r;
public Circle(double x, double y, double r) { super(); this.x = x; this.y = y; this.r = r; }
@Override double getC() { // TODO Auto-generated method stub return 2*Math.PI*r; }
@Override
double getS() {
}
// TODO Auto-generated method stub return Math.PI*r*r;
@Override void show() {
System.out.println(\圆心o(\半径:r=\ }
2:写出this和super的用法。 答:this:
1.this用在构造方法中,通过一个构造方法去调用另一个构造方法。必须出现在构造方法的第一行。
2.this能用在成员方法中,访问对象的其他成员方法和成员变量。
3.当局部变量和成员变量重名的时候可以使用this 指定调用成员变量。
4.this不能用在静态方法中。 super:
1.调用父类的构造方法
2.调用父类的成员方法和成员变量 3.super不能用在静态方法中
}
3:写出static和final的用法 答:static:
static修饰的变量叫做“静态变量”. static修饰的方法叫做“静态方法”. static还可以定义静态语句块.
static定义的静态语句块在类加载阶段执行, 并且只执行一次,并且是自上而下的顺序执行。 final:
1.采用final 修饰的类不能被继承
2.采用final 修饰的方法不能被重写(覆盖)
3.采用final 修饰的变量不能被修改,final 修饰的变量必须显示
4.构造方法不能被final 修饰