`方法可以在沿着继承链的多个类中实现
class Test{
public static void main (String[] args){
A a = new A(3);
}
}
class A extends B{
public A (int t){
System.out.println("A's constructor is invoked");
}
}
class B{
public B(){
System.out.println("B's constructor is invoked");
}
}
结果:
B’s constructor is invoked
A’s constructor is invoked
import java.util.*;
public abstract class Geo{//抽象类
abstract double calcArea();
}
class Point{//point类
public double x;
public double y;
}
class Cricle extends Geo{//Cricle
private Point centre;
private double radius;
public Cricle(){
centre = new Point();
centre.x = centre.y = 0.0;
radius=1.0;
}
public Cricle (Point c,double r){
centre = new Point();
centre.x=c.x;
centre.y=c.y;
if(r<=0){
throw new IllegalArgumentException();
}
else{
radius = r;
}
}
public Point getCentre(){
return centre;
}
public void setCentre(Point c){
centre.x=c.x;
centre.y=c.y;
}
public void setRadius(double r){
radius =r;
}
public double calcArea(){
return Math.PI *radius * radius;
}
}
interface CanRotate{//接口
void horizonRotate();
}
class Rectangle extends Geo implements CanRotate{
private Point a,b;
public Rectangle(Point a,Point b){
if((a.x-b.x==0)||(a.y-b.y==0)){
throw new IllegalArgumentException();
}else{
this.a.x=a.x;
this.a.y=a.y;
this.b.x=b.x;
this.b.y=b.y;
}
}
public double calcArea(){
return Math.abs((b.x - a.x) * (b.y - a.y));
}
public void horizonRotate(){
double tmp;
tmp=a.x;
a.x=b.x;
b.x=tmp;
}
}