首页 > 其他分享 >CS61B srping 2018 disc04 https://sp18.datastructur.es/

CS61B srping 2018 disc04 https://sp18.datastructur.es/

时间:2025-01-11 20:32:22浏览次数:1  
标签:srping sp18 void CS61B System Cat Animal println public

extends(扩展) 和 override(重写)

extends 关系导致的类型,子类一定是父类,父类一定不是子类。
就赋值而言, 父类 a= 子类 b 是 ok的 ;反过来 子类 x = 父类 y;是不ok的, 也就是说赋值时,类型层级上,右边一定是小于(低于)左边的。
给定 Animal 类,填写 Cat 类的定义,以便在greet()被调用时,“Cat”(而不是“Animal”)被打印到屏幕上。如果猫是 5 岁或以上,“Meow”,5岁以下,则会发出噪音并发出“MEOW”。

1. Animal
public class Animal {
protected String name, noise;
protected int age;

public Animal(String name, int age) {
this.name = name;
this.age = age;
this.noise = "Huh?";
}

public String makeNoise() {
if (age < 5) {
return noise.toUpperCase();
} else {
return noise;
}
}

public void greet() {
System.out.println("Animal " + name + " says: " + makeNoise());
}
}
2.Cat
public class Cat extends Animal {

    public Cat(String name, int age) {
        super(name, age);
        this.noise = "Meow!";
    }
    @Override
    public void greet() {
        System.out.println("Cat " + name + " says: " + makeNoise());
    }

    public static void main(String[] args) {
        Cat babyCat = new Cat("babyCat",1);
        Cat bigCat = new Cat("bigCat",5);

//        MEOW!
        babyCat.greet();
//        Meow!
        bigCat.greet();
    }
}

extends 和override 2

对上面的 Animal和Cat ,给出下列程序运行结果:
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
7 a.greet(); // (A)___Pluto says Huh?___________________
8 c.greet(); // (B)___Garfield says Meow!___________________
9 d.greet(); // (C)___Fido says WOOF!___________________
10 a = c;
11 ((Cat) a).greet(); // (D) ______Garfield says Meow!________________
12 a.greet(); // (E) _______Garfield says Meow!_______________
13 }
14 }
15
16 public class Dog extends Animal {
17 public Dog(String name, int age) {
18 super(name, age);
19 noise = "Woof!";
20 }
21
22 @Override
23 public void greet() {
24 System.out.println("Dog " + name + " says: " + makeNoise());
25 }
26
27 public void playFetch() {
28 System.out.println("Fetch, " + name + "!");
29 }
30 }
如果在上述程序第12行下面加下面语句,会出现什么情况?如果出现了编译错误,为什么?怎么改?
1 a = new Dog("Spot", 10);
2 d = a;

答:

1 a = new Dog("Spot", 10); //没问题, Dog 可以看做是Animal,
2 d = a;                  //有问题, Animal 不是一个Dog, 上位不是下位。

答:

Dog is a subclass of Animal, so this assignment will
fail at compile time because not all Animals are Dogs. Use casting to address the
problem. 因为不是所有的Animal 都是 Dog ,上位词包含更多不一定是更具体的下位。
也就是右边一定要小于左边。
1 d = (Dog) a;
This represents a promise to the compiler that at runtime, a will be bound to an
object that is compatible with the Dog type.

3 找出下列程序的潜在错误并记录,消除这些错误后,D的运行结果是什么?

1 class A {
2 public int x = 5;
3 public void m1() {System.out.println("Am1-> " + x);}
4 public void m2() {System.out.println("Am2-> " + this.x);}
5 public void update() {x = 99;}
6 }
7 class B extends A {
8 public void m2() {System.out.println("Bm2-> " + x);}
9 public void m2(int y) {System.out.println("Bm2y-> " + y);}
10 public void m3() {System.out.println("Bm3-> " + "called");}
11 }
12 class C extends B {
13 public int y = x + 1;
14 public void m2() {System.out.println("Cm2-> " + super.x);}
15 \\ public void m4() {System.out.println("Cm4-> " + super.super.x); }} can't do super.super
16 public void m5() {System.out.println("Cm5-> " + y);}
17 }
18 class D {
19 public static void main (String[] args) {
20 \\ B a0 = new A(); Dynamic type must be B or subclass of B
21 \\ a0.m1(); cascading: prev line failed, so a0 can't be initialized
22 \\ a0.m2(16); cascading: prev line failed, so a0 can't be initialized
23 A b0 = new B();
24 System.out.println(b0.x); [prints "5"]
25 b0.m1(); [prints "Am1-> 5"]
26 b0.m2(); [prints "Bm2-> 5"]
27 \\ b0.m2(61); m2 (int y) not defined in static type of b0
28 B b1 = new B();
29 b1.m2(61); [prints "Bm2y-> 61"]
30 b1.m3(); [prints "Bm3-> called"]
31 A c0 = new C();
32 c0.m2(); [prints "cm2-> 5"]
33 \\ C c1 = (A) new C(); Can't assign c1 to an A
34 A a1 = (A) c0;
35 C c2 = (C) a1;
36 c2.m3(); [print Bm3-> called]
37 \\ c2.m4(); C.m4() is invalid
38 c2.m5(); [print Cm5-> 6]
39 ((C) c0).m3(); [print Bm3-> called]
40 \\ (C) c0.m3(); NOT RUNTIME ERROR This would case the result of what the method returns and
it returns void therefore compile-time error
41 b0.update();
42 b0.m1(); [print Am1-> 99]
43 }
44 }

第四题不会做。

标签:srping,sp18,void,CS61B,System,Cat,Animal,println,public
From: https://www.cnblogs.com/nulixuexipython/p/18664853

相关文章

  • CS61B srping 2018 proj1A https://sp18.datastructur.es/
    proj1A数据结构skeleton地址开始这个proj之前,至少应该对SLList,DLList,AList有所了解介绍在proj1A里要用list和Array来创建“DoubleEndedQueue”,在接下来的proj1B里要对应编写测试。LinkedListDeque.javaandArrayDeque.java是这个proj里边要操作的文件,推荐使用intel......
  • CS61B srping 2018 lab03 https://sp18.datastructur.es/
    UnitTestingwithJUnit,Debugging准备装好CS61B插件(emmmmm,不装也没事)把lab2的IntList.java复制到lab3/IntList文件夹.看看关于测试的课程视频介绍啊?JUnit是java测试框架,现在要用JUnit进行单元测试,单元Unit就是把程序分成小块的单元,一个单元的功能尽量少,单独测试,......
  • CS61B 0A Exercises
    虽然以前有Java基础,但是工作四年主要使用的还是python和shell,另外对于数据结构和算法的基础也不牢固。所以开一个坑从头开始学习CS61B,对于Dicussion/Lab/Homework做一个记录,学完之后回头看能有一个收获。整体流程参考CS自学社区,学习课程为B站CS61B,本篇为学习完Lectrue01后的Homew......
  • CS61B srping 2018 examprep03 https://sp18.datastructur.es/
    flatten方法接受二维数组,返回一个一维数组比如,flatten({{1,2,3},{},{7,8}})shouldreturn{1,2,3,7,8}补全下面的程序片段publicstaticint[]flatten(int[][]x){inttotalLength=0;for(____________________________________){_______________________......
  • CS61B srping 2018 disc03 https://sp18.datastructur.es/
    为下面方法添加SLList.insert方法,要求签名如publicvoidinsert(intitem,intposition){},如果position大于最后一个元素,那就是添加到最后。(注意这个作业里的SLList和课程中介绍的SLList相比少点东西,故意的,可能是为了让学生开拓思路?)publicclassSLList{privateclassIn......
  • CS61B srping 2018 project00 https://sp18.datastructur.es/
    GettingtheSkeletonFiles,网站上应该有仓库地址,这个也行,https://gitee.com/heqilaoge/skeleton-sp18。拉下来找到proj0,就能开始作业。可以不使用IDE。2.ThePlanetClassandItsConstructor创建Planet类publicclassPlanet{publicdoublexxPos;publicdo......
  • CS61B srping 2018 examprep01(?02) https://sp18.datastructur.es/
    1.写出第21、24行的运行结果。(画出box-pointer指示图会对答题很有帮助)1publicclassShock{2publicstaticintbang;3publicstaticShockbaby;4publicShock(){5this.bang=100;6}7publicShock(intnum){8this.bang=num;9baby=starter();10this......
  • CS61B srping 2018 disc02 sol https://sp18.datastructur.es/
    第19行的变量level是静态方法change方法内部的本地变量,他对main方法里的level或者是实例变量level没什么影响。publicclassPokemon{//一个名为Pokemon的类publicStringname;//实例变量namepublicintlevel;//实例变量levelpublicPokemon(String......
  • CS61B srping 2018 disc02 https://sp18.datastructur.es/
    passbywhat?a.下列程序每一行怎么运行?b.画出box-and-pointer指示图c.在第19行,level被赋值为50,level是什么?是Pokemon的实例变量?(instancevariable)还是change方法的本地变量?(localvariable?)还是main方法里的变量?还是其他什么东西?1publicclassPokemon{2public......
  • CS61B srping 2018 disc01 answer
    1intsize=27;//声明一个int类型的变量size,并把27赋值给他2Stringname="Fido";//声明一个String类型的变量,并把“Fido”赋值给他3DogmyDog=newDog(name,size);//声明一个Dog类型的变量myDog,并调用Dog构造函数,分别把name和size传入其中,生成一个Dog类型的对......