Inheritance II and Midterm Review (Discussion 5) (Kế thừa trong lập trình hướng đối tượng) - Christine Zhou
Đang tạo bản xem trước...
Tài liệu thảo luận về kế thừa trong lập trình hướng đối tượng, bao gồm quy tắc gọi phương thức, field hiding, và ôn tập giữa kỳ với bài tập reduce.
Mô tả
Discussion 5: Inheritance and Midterm Review Christine Zhou Agenda Announcements Inheritance Practice 4. Midterm Review on Worksheet 2. Reduce on Worksheet Announcements Field hiding is in scope! Same as static lookup Sorry for the mix-up. We’ll go over this today Midterm is tomorrow 8-10PM. Room assignments on Piazza Go to right rooms! Best of luck! HW3 is due Friday 11:59PM Get started on the project! Discussion survey: tinyurl.com/disc5cz Inheritance Rules For non-static methods: During compilation: If static type has the method, it compiles. Otherwise check parent, parent… Compile time error if no more parent and still haven’t found the method. During runtime: Run the method in the dynamic type. Otherwise check parent, parent... For static method: During compilation: Same as non-static During runtime: Run the method found during compilation (don’t look at the dynamic type!) Always look at the dynamic type when running methods, unless the method is static! For fields: Use same procedures as static method lookup Look in the static type for the field and use the field that you eventually find Field Hiding If you have a variable of the same name in the static and dynamic type, then this is called “field hiding/shadowing”. It’s possible for the field in the static type to “hide/shadow” the field in the dynamic type. Field hiding is not good practice! Example public class Animal { public String x = “Animal”; public static void staticM() { System.out.println(“A static”); } public class Dog extends Animal { public String x = “Dog”; public static void staticM() { System.out.println(“D static”); } public void nonStaticM() { System.out.println(“A public void nonStaticM() { nonStatic”); System.out.println(“D } nonStatic”); public static void m1(Animal a) { } System.out.println(“A m1”); a.nonStaticM(); } } } public class Runner { public static void main(String[] args) { Animal a = new Dog(); System.out.println(a.x); a.nonS
Tóm tắt AI
- Tên tài liệu
- Inheritance II and Midterm Review (Discussion 5) (Kế thừa trong lập trình hướng đối tượng) - Christine Zhou
- Trường / Môn
- University of California, Berkeley · Lập trình Java
- Tác giả (trong tài liệu)
- Christine Zhou
- Nội dung
- Tài liệu về kế thừa và giảm thiểu trong lập trình hướng đối tượng
- Mục lục
- Agenda
- Announcements
- Inheritance Rules
- Field Hiding
- Example
- Midterm Review
- Reduce
- Số trang
- 9 trang
- Người đăng
- Uni24h
Câu hỏi thường gặp
Tài liệu này có miễn phí không?
Có. “Inheritance II and Midterm Review (Discussion 5) (Kế thừa trong lập trình hướng đối tượng) - Christine Zhou” miễn phí — bạn chỉ cần đăng nhập rồi bấm Tải xuống để lấy file gốc.
Tài liệu dài bao nhiêu trang?
Tài liệu gồm 9 trang, thuộc môn Lập trình Java. Bạn có thể xem trước online trước khi tải.
Tôi có thể xem trước trước khi tải không?
Có. Bạn xem trước tài liệu ngay trên trang này bằng trình đọc online, rồi quyết định tải về.
Inheritance II and Midterm Review (Discussion 5) (Kế thừa trong lập trình hướng đối tượng) - Christine Zhou
Đang tạo bản xem trước...
Discussion 5: Inheritance and Midterm Review Christine Zhou Agenda Announcements Inheritance Practice 4. Midterm Review on Worksheet 2. Reduce on Worksheet Announcements Field hiding is in scope! Same as static lookup Sorry for the mix-up. We’ll go over this today Midterm is tomorrow 8-10PM. Room assignments on Piazza Go to right rooms! Best of luck! HW3 is due Friday 11:59PM Get started on the project! Discussion survey: tinyurl.com/disc5cz Inheritance Rules For non-static methods: During compilation: If static type has the method, it compiles. Otherwise check parent, parent… Compile time error if no more parent and still haven’t found the method. During runtime: Run the method in the dynamic type. Otherwise check parent, parent... For static method: During compilation: Same as non-static During runtime: Run the method found during compilation (don’t look at the dynamic type!) Always look at the dynamic type when running methods, unless the method is static! For fields: Use same procedures as static method lookup Look in the static type for the field and use the field that you eventually find Field Hiding If you have a variable of the same name in the static and dynamic type, then this is called “field hiding/shadowing”. It’s possible for the field in the static type to “hide/shadow” the field in the dynamic type. Field hiding is not good practice! Example public class Animal { public String x = “Animal”; public static void staticM() { System.out.println(“A static”); } public class Dog extends Animal { public String x = “Dog”; public static void staticM() { System.out.println(“D static”); } public void nonStaticM() { System.out.println(“A public void nonStaticM() { nonStatic”); System.out.println(“D } nonStatic”); public static void m1(Animal a) { } System.out.println(“A m1”); a.nonStaticM(); } } } public class Runner { public static void main(String[] args) { Animal a = new Dog(); System.out.println(a.x); a.nonS
Đọc toàn bộ tài liệu
- Tên tài liệu
- Inheritance II and Midterm Review (Discussion 5) (Kế thừa trong lập trình hướng đối tượng) - Christine Zhou
- Trường / Môn
- University of California, Berkeley · Lập trình Java
- Tác giả (trong tài liệu)
- Christine Zhou
- Nội dung
- Tài liệu về kế thừa và giảm thiểu trong lập trình hướng đối tượng
- Mục lục
- Agenda
- Announcements
- Inheritance Rules
- Field Hiding
- Example
- Midterm Review
- Reduce
- Số trang
- 9 trang
- Người đăng
- Uni24h
Bình luận (0)
Chưa có bình luận nào. Hãy là người đầu tiên!
Misc.onclusion (Discussion 14-END) (Ôn tập cấu trúc dữ liệu) - Christine Zhou
Final Review Solutions (Cấu trúc dữ liệu heap, hàng đợi ưu tiên và duyệt đồ thị) - Ching and Christines
Giáo trình Lập trình Java
Inheritance (Discussion 4) (Kế thừa trong Java) - Christine Zhou
Asymptotic Analysis (Discussion 7) (Phân tích tiệm cận) - Christine Zhou
Chương 7.Cơ học lượng tử - Vật lý đại cương 3 - TS.Nguyễn Thị Trang
Chương 6.Quang học lượng tử - Vật lý đại cương 3 - TS.Nguyễn Thị Trang
Chương 5.Thuyết tương đối - Vật lý đại cương 3 - TS.Nguyễn Thị Trang
Chương 4. Tán xạ ánh sáng - Vật lý đại cương 3 - TS.Nguyễn Thị Trang
Chương 3.Phân cực ánh sáng - Vật lý đại cương 3 - TS.Nguyễn Thị Trang
Bình luận (0)
Chưa có bình luận nào. Hãy là người đầu tiên!