继承 3—A B E

20.编写一个Java应用程序,该程序包括3个类: A类、B类和主类E。其中

B是类A的子类,在子类B中新增了成员变量和成员方法,并且隐藏了父类

A的成员变量和重写了父类A的成员方法。在主类Emain方法中,创建类B

的对象并赋给父类A的对象a,使用上转型对象a来测试上转型对象的一些特性。

 1 package mianxiangduixiang;
 2 
 3 public class jichengA {
 4     private String Food;
 5     public String getFood() {
 6         return Food;
 7     }
 8     public void setFood(String food) {
 9         Food = food;
10     }
11      public void speakFood()
12      {
13          System.out.println(" go out to dinners");
14      }
15      public void AmericanFood()
16      {
17          System.out.println("牛排,披萨,汉堡");
18      }
19     public static void main(String[] args) {
20         // TODO 自动生成的方法存根
21     
22     }
23 
24 }
 1 package mianxiangduixiang;
 2 
 3 public class jichengB extends jichengA {
 4 
 5     public void speakFood()
 6     {
 7         System.out.println("传膳");
 8     }
 9     public void ChinaFood()
10     {
11         System.out.println("鲁菜、 川菜、 粤菜、 闽菜、 苏菜、 浙菜、湘菜、 徽菜");
12     }
13     public static void main(String[] args) {
14         // TODO 自动生成的方法存根
15         jichengB a=new jichengB();
16          a.setFood("八大菜系");
17          a.speakFood();
18          a.AmericanFood();
19          a.ChinaFood();
20     }
21 
22 }

原文地址:https://www.cnblogs.com/yg6405816/p/5518333.html