package com.ssm.cts.test; public class Car { public String code; private String name; public Car(String _name) { super(); this.name = _name; } public void start() { System.out.println(name + "启动"); } public void stop() { System.out.println(name + "停止"); } }
package com.ssm.cts.test; public class MiniCar extends Car { public MiniCar(String _name) { super(_name); } // 子类可以用自己的方式实现父类的方法 public void start() { // 子类拥有父类非private的属性,方法 System.out.println(super.code + "启动"); } // 子类可以拥有自己的属性和方法 public void restart() { System.out.println("重启"); super.stop(); super.start(); } }
package com.ssm.cts.test; public class ClassTest { // 测试 public static void main(String[] args) { MiniCar car = new MiniCar("001", "别克"); car.start(); car.stop(); car.restart(); } }