YOMEDIA
ADSENSE
Classes and Objects in Java_Object-oriented programming
123
lượt xem 8
download
lượt xem 8
download
Download
Vui lòng tải xuống để xem tài liệu đầy đủ
Outline: Classes, Working with objects, Attributes, methods, and access control, Constructors. Readings: Java how to program, chapter 3, 8. A Java program is a collection of objects. Each class is specified in one source file (file name is the same with class name), Every line of code you write in Java must be inside a class (not counting import directives), Increase modularity, Easier to modified code, shorter compile time.
AMBIENT/
Chủ đề:
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Classes and Objects in Java_Object-oriented programming
- Classes and Objects in Java Object-oriented programming
- Outline Classes Working with objects Attributes, methods, and access control Constructors Readings: Java how to program, chapter 3, 8 Đại học Công nghệ. ĐHQG Hà Nội Classes and objects in Java 2
- Java program A Java program is a collection of objects Each class is specified in one source file (file name is the same with class name) Every line of code you write in Java must be inside a class (not counting import directives) Increase modularity Easier to modified code, shorter compile time Đại học Công nghệ. ĐHQG Hà Nội Classes and objects in Java 3
- class declared as public must be // GradeBook.java stored in a file with the same name class declaration public class GradeBook begins / ends { // display a welcome message to the GradeBook user 02 classes public void displayMessage() in 02 files { System.out.println( "Welcome to the Grade Book!" ); } } // end class GradeBook main() is called automatically by // GradeBookTest.java the Java Virtual Machine when public class GradeBookTest the program is executed { // main method begins program execution public static void main( String args[] ) { // create a GradeBook object and assign it to myGradeBook GradeBook myGradeBook = new GradeBook(); // call myGradeBook's displayMessage method myGradeBook.displayMessage(); } creates an instant / object of the class } // end class GradeBookTest myGradeBook is the reference to it Welcome to the Grade Book! Đại học Công nghệ. ĐHQG Hà Nội Classes and objects in Java 4
- Objects Objects are manipulated via references Object references play the roles similar to pointers Objects must be explicitly created by new operator public class GradeBookTest { // main method begins program execution public static void main( String args[] ) { // create a GradeBook object and assign it to myGradeBook GradeBook myGradeBook = new GradeBook(); // call myGradeBook's displayMessage method myGradeBook.displayMessage(); } } // end class GradeBookTest Đại học Công nghệ. ĐHQG Hà Nội Classes and objects in Java 5
- Objects and Object references ... // create a GradeBook object and assign it to myGradeBook GradeBook myGradeBook = new GradeBook(); ... Heap memory myGradeBook The object GradeBook the object reference created by new GradeBook() Đại học Công nghệ. ĐHQG Hà Nội Classes and objects in Java 6
- Attributes, methods, and access control Access modifiers: Public Accessible anywhere by anyone Protected Accessible only to the class itself and to its subclasses or other classes in the same “package” Private Only accessible within this class Đại học Công nghệ. ĐHQG Hà Nội Classes and objects in Java 7
- GradeBook - courseName : String + setCourseName( name : String ) + getCourseName() : String private: // GradeBook.java + displayMessage() accessed by public class GradeBook the class’s { methods only private String courseName; // course name for this GradeBook attribute. // method to set the course name Each Gradebook object has its public void setCourseName( String name ) own instant variable named { courseName courseName = name; // store the course name access } modifiers // method to retrieve the course name public String getCourseName() { return courseName; methods } // display a welcome message to the GradeBook user public void displayMessage() { System.out.println( "Welcome to the Grade Book!" ); } } // end class GradeBook Đại học Công nghệ. ĐHQG Hà Nội Classes and objects in Java 8
- Overloading methods Methods can have the same name but different argument lists. class MyDate { … public boolean setMonth(int m) { …} public boolean setMonth(String s) { …} } … d.setMonth(9); d.setMonth(”September”); Đại học Công nghệ. ĐHQG Hà Nội Classes and objects in Java 9
- Constructors Every class has a default “method” called a Constructor Invoked when the object is to be “created” / “allocated” by using “new” Main purposes: Initialise object’s attributes / data members A class may have multiple constructors Distinguished at compile time by having different arguments The default constructor takes no arguments and is implicit when no other constructors are specified Đại học Công nghệ. ĐHQG Hà Nội Classes and objects in Java 10
- GradeBook - courseName : String // GradeBook.java «constructor» GradeBook( name: public class GradeBook String ) { + setCourseName( name: String ) private String courseName; // course name for this GradeBook + getCourseName() : String // constructor initializes courseName + displayMessage() public GradeBook( String name ) { courseName = name; // initializes courseName 12 } // method to set the course name public void setCourseName( String name ) { courseName = name; // store the course name } // method to retrieve the course name public String getCourseName() { return courseName; // GradeBookTest.java } … // display a welcome message to the GradeBook user // create a GradeBook object and assign it to myGradeBook public void displayMessage() GradeBook myGradeBook = { new GradeBook( "CS101 Introduction to Java Programming“ ); System.out.println( "Welcome to the Grade Book!" ); … } } // end class GradeBook Đại học Công nghệ. ĐHQG Hà Nội Classes and objects in Java 11
- Implementation vs. Interface GradeBookTest: a “client” of GradeBook Implementation Data structures and code that implement the features (variables and methods) Usually more involved and may have complex inner workings Clients don’t need to know Interface The controls exposed to the “client” by the implementation The knobs on the block box Đại học Công nghệ. ĐHQG Hà Nội Classes and objects in Java 12
- Encapsulation / information hiding “Don’t expose internal data structures!” Objects hold data and code Neither is exposed to the end user Objects expose an interface Anthropomorphic nature of objects Think of objects and people who have specialized roles! Lawyer, Mechanic, Doctor Complexity is hidden inside the object Make life easier for clients More modular approach Implementation changes in one component doesn’t affect others Less error-prone Đại học Công nghệ. ĐHQG Hà Nội Classes and objects in Java 13
ADSENSE
CÓ THỂ BẠN MUỐN DOWNLOAD
Thêm tài liệu vào bộ sưu tập có sẵn:
Báo xấu
LAVA
AANETWORK
TRỢ GIÚP
HỖ TRỢ KHÁCH HÀNG
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn