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

UN
Uni24h
0 lượt tải
27
0

Nội dung tài liệu

Trang 3

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

Trang 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

Trang 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)

Trang 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() {...} }

Trang 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”;

Trang 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(); } }

Trang 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());

Trang 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?

Trang 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

Trang 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

Trang 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”); } }

Trang 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.

Trang 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)

Trang 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)

Trang 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

Trang 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!

Hiển thị 20 trang đầu. Tải tài liệu để xem đầy đủ.

Bình luận (0)

Đăng nhập để tham gia bình luận

Chưa có bình luận nào. Hãy là người đầu tiên!