JAVA&Note

`方法可以在沿着继承链的多个类中实现

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;
    }
 }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值