Lecture note Java methods A & AB: Object-oriented programming and data structures: Chapter 9 - Maria Litvin, Gary Litvin

Chapter 9 - Implementing classes and using objects. This is a long and technical chapter: it deals with several important Java concepts and syntax details. These objectives are within the AP Java Subset for the A-level exam. Some of the details related to classes and methods are left out. Java is a big language. | Implementing Classes and Using Objects Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Chapter chapter9 = new Chapter(9); 9- This is a long and technical chapter: it deals with several important Java concepts and syntax details. Objectives: Review public and private fields and methods Learn the syntax for defining constructors and creating objects Learn the syntax for defining and calling methods Learn how parameters are passed to constructors and methods and how to return values from methods Learn about static and instance fields and methods 9- These objectives are within the AP Java Subset for the A-level exam. Some of the details related to classes and methods are left out. Java is a big language. Class’s Client Any class that uses class X is called a client of X Class X private fields private methods public methods public . | Implementing Classes and Using Objects Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Chapter chapter9 = new Chapter(9); 9- This is a long and technical chapter: it deals with several important Java concepts and syntax details. Objectives: Review public and private fields and methods Learn the syntax for defining constructors and creating objects Learn the syntax for defining and calling methods Learn how parameters are passed to constructors and methods and how to return values from methods Learn about static and instance fields and methods 9- These objectives are within the AP Java Subset for the A-level exam. Some of the details related to classes and methods are left out. Java is a big language. Class’s Client Any class that uses class X is called a client of X Class X private fields private methods public methods public constructor(s) Class Y A client of X Constructs objects of X and/or calls X’s methods 9- The same programmer can develop a class and its client(s). Public vs. Private Public constructors and methods of a class constitute its interface with classes that use it — its clients. All fields are usually declared private — they are hidden from clients. Static constants occasionally can be public. “Helper” methods that are needed only inside the class are declared private. 9- Static public constants (for example, or ) do not violate the spirit of encapsulation because they are specifically intended to be used by the class’s clients. Public vs. Private (cont’d) A private field is accessible anywhere within the class’s source code. Any object can access and modify a private field of another object of the same class. public class Fraction { private int num, denom; . public multiply (Fraction other) { int newNum = num * ; . 9- num is private to the class Fraction. It .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.