水果是超类,而对应的苹果、橙子等为子类,子类通常继承超类的特性,然后具有独有的属性对价格进行优惠。
1、Fruit类,超类
定义属性private私有—》构造方法初始化对象—》用get方法取得值
package com.em.fruits;
public class Fruits {
private String name;
private double gprice;
private double price;
private String mark;
public Fruits(String name, double gprice,double price, String mark){
this.name = name;
this.gprice = gprice;
this.price = price;
this.mark = mark;
}
public String getName() {
return name;
}
public double getGprice(){
return gprice;
}
public double getPrice() {
return price;
}
public String getMark() {
return mark;
}
}
2、子类 Apple.java
定义属性private私有—》构造方法初始化对象—》set方法赋值属性—》get方法获取超类