Inheritance (Discussion 4) (Kế thừa trong Java) - Christine Zhou

UN
Uni24h
0 次下载
27
0

资料内容

第 3 页

Agenda - Problem 1 - Mini lecture and Problem 2 - Mini lecture and Problem 3 - Problem 4 at your own pace

第 4 页

Administrivia - Project 1B and 1Gold released, due Saturday 2/16 - Lab this week is peer-code review; attendance is required! - Midterm 2/20, 8-10 PM, see midterm post for more details - Room assignments out later on - Guerrilla section this Sunday 2/17 from 2-4pm in 271, 273, 275 Soda - One-on-one tutoring sign ups on Piazza weekly - Resources: tinyurl.com/cz-disc4-sp19 - Resources: tinyurl.com/cs61b-christine-zhou

第 6 页

JUnit Testing What are the advantages and disadvantages of writing JUnit tests? Advantages: - 1. Organized code - each test corresponds to different part of program - 2. Can debug code locally - 3. Documentation that your program works - 4. Can reduce the amount of test code you need to write because you can reuse code Disadvantages: - 1. Hard to use for higher-level testing (integration testing)

第 7 页

Review: Inheritance public class Fish { public class Goldfish { String name; String name; public Fish(String name) public Goldfish(String name) {...} {...} Public void eat() {...} public void eat() { public void blink() {...} print(“bloop”); ... } } public void blink() {...} }

第 8 页

Inheritance: extends - extends (can extend only one class) - Inherits all fields, methods, and nested classes - Constructor is not inherited, but there will be an implicit call to super( ) in the beginning of the subclass’s constructor (need to make the Superclass before making the Subclass) - If the Superclass’s constructor requires arguments, you must write an explicit call to super! Fish() { Goldfish() { this.name = “Bob”; super(); this.age = 12; this.name = “Bobfish”; } } ----------------------------------------------------------------------------- - Fish(String name) { Goldfish(String name) { this.name = name; super(name); this.age = 12; this.name = “Bobette- fish”;

第 9 页

Review: Inheritance public class Fish { public class Goldfish extends Fish { String name; public Goldfish(String name) { public Fish(String name) super(name); // need to {...} have this! ... Public void eat() {...} } public void blink() {...} public void eat() { } print(“bloop”); super.eat(); } }

第 11 页

Creating Cats public class Cat extends Animal { public Cat(String name, int age) { super(name, age); // Call superclass' constructor. this.noise = "Meow!";// Change the value of the field. } @Override public void greet() { System.out.println("Cat " + name + " says: " + makeNoise());

第 12 页

Review: More Inheritance - Anything that lives in water must have these two methods: float(), swim() - If we have a Fish or SeaOtter class, it must have these! - How do we guarantee this?

第 13 页

Inheritance: implements - Let’s define an interface, all classes that implement this interface must have all the methods - Can implement multiple interfaces public interface WaterBeing { public class Fish implements WaterBeing { public void swim(); public Fish() {...} public void float(); @Override } public void swim() {...} @Override

第 14 页

Review: Static vs. Dynamic Types - Fish f = new Goldfish() - Fish is the static type, this is what Java looks at during compilation - Goldfish is the dynamic type, this is what Java looks at during runtime - The dynamic type can be any subclass of the static type - Does Fish b = new Goldfish() work? - How about Goldfish c = new Fish()? - Casting changes the static type of an object temporarily - Fish d = new Goldfish(); - ((Goldfish) d).bloop(); // static type of d is Goldfish for this line - d.bloop(); // static type of d is Fish

第 15 页

Overriding/Overloading public class Fish { public class Salmon extends Fish { String home; int weight; public Salmon(int w, String h) { public Fish(int w) { super(w); Overriding: exactweight = w; home = h; }same method } signature public void swim() { public void swim() { System.out.println(“splash System.out.println(“splish ”); splash”); } } } Overloading: same public void swim(int speed) { name, different System.out.println(“swimming at “ parameters + speed + “ mph”); } }

第 16 页

Review: Static vs. Dynamic Types - Method lookup in non-static contexts: - During compilation: - Check if the method exists in the static type - If doesn’t exist, go to the superclass and repeat - If no superclass, compile time error - During runtime: - Run the method in the dynamic type (dynamic method selection), if not in the current class, go to the parent, and then the parent… - The method has to have the exact same signature as the one found during compilation. - Is this overloading or overriding? - Overriding! Only overriding has to do with inheritance. Overloading does not.

第 17 页

Dynamic Method Selection Compile time: static method Fish fish = new Fish(); lookup, check if Salmon salmon = new Salmon(); the static type Fish bob = new Salmon(); has that method Run time: fish.swim(); dynamic method salmon.swim(); lookup (for bob.swim(); overridden methods only!), fish.swim(5); use the method of salmon.swim(5); the dynamic type bob.swim(5)

第 18 页

Dynamic Method Selection Compile time: static method Fish fish = new Fish(); lookup Salmon salmon = new Salmon(); Run time: dynamic method Fish bob = new Salmon(); lookup splash fish.swim(); splish splash salmon.swim(); splish splash bob.swim(); compile-time error fish.swim(5); swimming at 5 mph salmon.swim(5); compile-time error bob.swim(5)

第 19 页

Raining Cats and Dogs ● Remember dynamic method lookup for overridden methods ● Casting forces the compile-time type of an object johnsu.deviantart.com/art/Raining-Cats-and-Dogs- 30291521

第 20 页

Raining Cats and Dogs Animal a = new Animal("Pluto", 10); Cat c = new Cat("Garfield", 6); Dog d = new Dog("Fido", 4); a.greet(); // (A) Animal Pluto says: Huh? c.greet(); // (B) Cat Garfield says: Meow! d.greet(); // (C) Dog Fido says: WOOF! a = c; ((Cat) a).greet(); // (D) Cat Garfield says: Meow! a.greet(); // (E) Cat Garfield says: Meow!

显示前 20 页。下载资料查看完整内容。

评论 (0)

登录 以加入讨论

暂无评论。快来抢沙发吧!