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+"元。";
}
}
java上机。。。类与对象
最新推荐文章于 2021-03-16 05:39:21 发布