0125Java实训

本文介绍了Java中的Circle和Rect类如何扩展Shape抽象类,以及Account抽象类的使用,展示了如何创建和操作具有基本属性和方法的对象。
摘要由CSDN通过智能技术生成

package shape;

public class Circle extends Shape{
    private double r;

    public Circle(double x, double y, double r) {
        super(x,y);
        this.r = r;
    }
     public Circle(){
        super();
     }


    public double getR() {
        return r;
    }


    public void setR(double r) {
        this.r = r;
    }

    @Override
    public String toString() {
        return "Circle{" +
                "x=" + x +
                ", y=" + y +
                ", r=" + r +
                '}';
    }

    @Override
    public void show(){
        System.out.println("圆的半径为:"+getR());
    }
}


public class Rect extends Shape{

    private double length;
    private double width;

    public Rect(double x,double y, double length, double width) {
        super(x,y);
        this.length = length;
        this.width = width;
    }
    public Rect(){
        super();
    }

    public double getLength() {
        return length;
    }

    public double getWidth() {
        return width;
    }


    public void setLength(double length) {
        this.length = length;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    @Override
    public String toString() {
        return "Rect{" +
                "x=" + x +
                ", y=" + y +
                ", length=" + length +
                ", width=" + width +
                '}';
    }

    @Override
    public void show(){
        System.out.println("矩形的长为:"+getLength()+",宽为:"+getWidth());
    }
}


public class Shape {
    double x;
    double y;

    public Shape(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public Shape(){}

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public void setX(double x) {
        this.x = x;
    }

    public void setY(double y) {
        this.y = y;
    }

    public void show(){
        System.out.println("x="+x+",y="+y);
    }

}


public class TestShape {
    /**
     * public static void printRect(Rect rect){
     *         rect.show();
     *     }
     *     public static void printCircle(Circle circle){
     *         circle.show();
     *     }
     *
     */

    //既能打印矩形也能打印圆形
    public static void printShape(Shape shape){
        shape.show();
    }
    //static修饰的方法只能调用静态的方法
    public static void main(String[] args){
        /**
         *  Shape shape=new Shape(3,4);
         *         shape.show();
         *         Circle circle=new Circle(1,2,4);
         *         circle.show();
         *         Rect rect=new Rect(1,2,3,4);
         *         rect.show();
          */
        /**
         * printRect(new Rect(1,2,3,4));
         *        printCircle(new Circle(1,2,4));
         */
        //父类型 引用变量名=new 子类类型();
        /**
         * Shape shape=new Rect(1,2,3,4);
         *         shape.show();
         *         Shape shape1=new Circle(1,2,4);
         *         shape1.show();
         */
        printShape(new Circle(1,2,4));
    }
   ;


}
package account;

public abstract class  Account {
    private double balance;
    public abstract double getInterest(double index,double time);

    public Account(double balance) {
        this.balance = balance;
    }
    public Account(){}

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }


}


public class AccountTest {
    public static void main(String[] args){
        Account acc=new FixedAccount(1000);
        System.out.println("本金:"+acc.getBalance()+",利息为:"+acc.getInterest(0.003,1));
    }
}

public class FixedAccount extends Account{
    @Override
    public double getInterest(double index, double time) {
        return getBalance()*index*time;
    }

    public FixedAccount(double balance) {
        super(balance);
    }

    public FixedAccount() {
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值