抽象类与抽象方法
//抽象类 意义:单纯被继承 [权限修饰符]abstract class 类名{ // 抽象方法 继承抽象类的所有子类 都要重写抽象类中的抽象方法 [权限修饰符] abstract 方法返回值类型 方法名(参数列表); //无方法体 }
----------------------------------------------------------------------------------- public class GoShopping { public static void main(String[] args) { Shopping tb = new TBShopping(); tb.market = "淘宝"; tb.goods = "衣服"; System.out.println("在"+tb.market+tb.shop()+tb.goods); Shopping xx = new XXShopping(); xx.market = "实体店"; xx.goods = "鞋子"; System.out.println("在"+xx.market+xx.shop()+xx.goods); } } // 抽象类 买东西 abstract class Shopping { public String market; //购买地 public String goods; //商品 public abstract String shop(); //抽象方法 购买行为 } class TBShopping extends Shopping{ //网购类 public String shop(){ return "网购"; } } class XXShopping extends Shopping{ //线下购类 public String shop(){ return "线下买"; } } /*在淘宝网购衣服 在实体店线下买鞋子*/
-----------------------------------------------------------------------------------
public class GoShopping { public static void main(String[] args) { Shopping tb = new TBShopping(); tb.market = "淘宝"; tb.goods = "衣服"; System.out.println("在"+tb.market+tb.shop()+tb.goods); Shopping xx = new XXShopping(); xx.market = "实体店"; xx.goods = "鞋子"; System.out.println("在"+xx.market+xx.shop()+xx.goods); } } // 抽象类 买东西 abstract class Shopping { public String market; //购买地 public String goods; //商品 public abstract String shop(); //抽象方法 购买行为 } class TBShopping extends Shopping{ //网购类 public String shop(){ return "网购"; } } class XXShopping extends Shopping{ //线下购类 public String shop(){ return "线下买"; } } /*在淘宝网购衣服 在实体店线下买鞋子*/
接口
原创文章,作者:254126420,如若转载,请注明出处:https://blog.ytso.com/275673.html