📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
12
返回列表 发新帖
楼主: hurricane_e
跳转到指定楼层
上一主题 下一主题
收起左侧

[CS61B_Spring 2015] Discussion4 - Inheritance

🔗
HNAKXR 2016-2-6 20:14:09 | 只看该作者
全局:
本帖最后由 HNAKXR 于 2016-2-6 20:43 编辑

1 Creating Cats
Animal Kitty says: MEOW!
Animal Garfield says: Meow!

2 Raining Cats and Dogs
  1. public class TestAnimals {
  2.         public static void main(String[] args) {
  3.                 Animal a = new Animal("Pluto", 10);
  4.                 Cat c = new Cat("Garfield", 6);
  5.                 Dog d = new Dog("Fido", 4);
  6.                 a.greet(); // Animal Pluto says: Huh?
  7.                 c.greet(); // Cat Garfield says: Meow!
  8.                 d.greet(); // Dog Fido says: WOOF!
  9.                 a = c;
  10.                 a.greet(); // Cat Garfield says: Meow!
  11.                 ((Cat) a).greet(); // Cat Garfield says: Meow!

  12.                 /* Compiler error!
  13.                  * because a Dog is an Animal, but an Animal is not necessarily a Dog. */
  14.                 // a = new Dog("Hiernoymus", 10);
  15.                 // d = a;

  16.                 /* We have to make an awkward cast. */
  17.                 a = new Dog("Hiernoymus", 10);
  18.                 d = (Dog) a;
  19.         }
  20. }
复制代码
3 An Exercise in Inheritance Misery
  1. class C extends B {
  2.         public int y = x + 1;
  3.         public void m2() {System.out.println("Cm2-> " + super.x);}
  4.         // public void m3() {System.out.println("Cm3-> " + super.super.x);} // super.super.x is not allowed!
  5.         public void m4() {System.out.println("Cm4-> " + y);}
  6.         // public void m5() {System.out.println("Cm5-> " + super.y);} // there is no variable y in superclass B.
  7. }
复制代码
  1. class D {
  2.         public static void main (String[] args) {
  3.                 // B a0 = new A(); // A can't be converted to B. Must cast if you want.
  4.                 // a0.m1(); // see the line above.
  5.                 A b0 = new B();
  6.                 System.out.println(b0.x);
  7.                 b0.m1(); // class B hides a field in class A.
  8.                 b0.m2(); // you should never hide fields.
  9.                 // b0.m3(); // as you’ll see, it’s confusing! see C.java: super.super.x not allowed!
  10.                 B b1 = new B();
  11.                 b1.m3();
  12.                 b1.m4();
  13.                 A c0 = new C();
  14.                 c0.m1();
  15.                 // C c1 = (A) new C(); // B can't be converted to C. Must cast if you want.
  16.                 A a1 = (A) c0;
  17.                 C c2 = (C) a1;
  18.                 c2.m4();
  19.                 // c2.m5(); // see C.java: there is no variable y in superclass B.
  20.                 // ((C) c0).m3(); // very tricky! see C.java: super.super.x not allowed!
  21.                 // (C) c0.m3(); // same as the line above.
  22.                 b0.update();
  23.                 b0.m1();
  24.                 b0.m2();
  25.         }
  26. }
复制代码
5
Am1-> 5
Bm2-> 10
Bm3-> 5
Bm4-> Am2-> 5
Am1-> 5
Cm4-> 11
Am1-> 99
Bm2-> 10

评分

参与人数 1大米 +20 收起 理由
zzwcsong + 20 坚持的不错,再接再厉!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表