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+" 。";
    }
}