package zuoye; //创建父类 public class Txd { public Txd() { } //属性 //颜色 public String Yanse; //品牌 public String Pinpai; //电容量 public int Dl; //当前电量 public int Xzdl; //构造方法 public Txd(String yanse,String pinpai,int dl) { Yanse = yanse; Pinpai = pinpai; Dl = dl; } //充电 public void chongdian (int cdl) { //实际充电量 int shijidl = Dl - Xzdl; Xzdl += cdl; if(Xzdl>=Dl) { System.out.println("电量已经充满,停止"); Xzdl=Dl; } else { shijidl = cdl; } System.out.println("充电量" + cdl+", 现在电量"+ Xzdl); } //使用一次耗电量 public int Haodianl = 10; //使用次数 public void shiyong (int cishu) { int Y = Haodianl * cishu; Xzdl -= Y; //没电了 if(Xzdl<=0) { System.out.println("电已经耗尽"); Xzdl = 0; cishu = Dl/10; } System.out.println("使用"+cishu+"次后,当前电量="+Xzdl); } } package zuoye; //创建子类 public class ttt extends Txd { public ttt(String yanse,String pinpai,int dl) { super(yanse, pinpai, dl); } public ttt() { } public void shiyong(int cishu) { System.out.println("使用"+cishu+"次后,当前电量="+Xzdl); } } package zuoye; public class txdtest { public static void main(String[] args) { Txd txd = new Txd("科迪","黑色",50 ); txd.Xzdl =30; txd.chongdian(20); txd.shiyong(3); //父类转子类 向上转型 Txd txd1 = new ttt(); txd1.shiyong(1); } }