java上机。。。类与对象

public class Test {

    public static void main(String[] args) {
        Fruit f = new Fruit("橘子",1500);
        Fruit f1 = new Fruit();
        System.out.println(f.toString());
        System.out.println(f.gain());
        System.out.println(f1.toString());
        System.out.println(f1.gain());

        Apple apple = new Apple("红富士",15000);
        System.out.println(apple.toString()+apple.gain());

        Banana ba = new Banana("小香蕉",20000);
        System.out.println(ba.toString()+ba.gain());

    }

}

//定义水果类
public class Fruit {
    public String fruitName;//水果名字
    public int input;//种植水果的投入
    public Fruit(){}
    public Fruit(String fruitName,int input){
        this.fruitName = fruitName;
        this.input = input;
    }
    public String toString(){
        return fruitName+"投入"+input+"元。";
    }
    public String gain(){
        return "收益为:"+input*2+"元。";
    }
}

//定义苹果类继承水果类
public class Apple extends Fruit {
    public Apple(){}
    public Apple(String n,int i){
        super(n,i);
    }
    public String gain(){
        return "收益为:"+(input*2+200)+"元。";
    }
}

//定义香蕉类继承水果类
public class Banana extends Fruit{
    public Banana(){}
    public Banana(String name,int input){
        super(name,input);
    }
    public String gain(){
        return "收益为:"+input*3+"元。";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值