Danh mục

Classes and Objects in Java_Object-oriented programming

Số trang: 13      Loại file: ppt      Dung lượng: 77.00 KB      Lượt xem: 10      Lượt tải: 0    
Thư viện của tui

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

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 beinside a class (not counting import directives),Increase modularity,Easier to modified code, shorter compile time.
Nội dung trích xuất từ tài liệu:
Classes and Objects in Java_Object-oriented programmingClasses andObjects in JavaObject-oriented programmingOutline 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 2Java 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 nameclass declaration publicclassGradeBookbegins / ends { //displayawelcomemessagetotheGradeBookuser02 classes publicvoiddisplayMessage()in 02 files { System.out.println(WelcometotheGradeBook!); } }//endclassGradeBook main() is called automatically by //GradeBookTest.java the Java Virtual Machine when publicclassGradeBookTest the program is executed { //mainmethodbeginsprogramexecution publicstaticvoidmain(Stringargs[]) { //createaGradeBookobjectandassignittomyGradeBook GradeBookmyGradeBook=newGradeBook(); //callmyGradeBooksdisplayMessagemethod myGradeBook.displayMessage(); } creates an instant / object of the class }//endclassGradeBookTest myGradeBook is the reference to it WelcometotheGradeBook! Đại học Công nghệ. ĐHQG Hà Nội Classes and objects in Java 4Objects Objects are manipulated via references  Object references play the roles similar to pointers Objects must be explicitly created by newoperator publicclassGradeBookTest { //mainmethodbeginsprogramexecution publicstaticvoidmain(Stringargs[]) { //createaGradeBookobjectandassignitto myGradeBook GradeBookmyGradeBook=newGradeBook(); //callmyGradeBooksdisplayMessagemethod myGradeBook.displayMessage(); } }//endclassGradeBookTestĐại học Công nghệ. ĐHQG Hà Nội Classes and objects in Java 5Objects and Object references ... //createaGradeBookobjectandassignitto myGradeBook GradeBookmyGradeBook=newGradeBook(); ... Heap memory myGradeBookThe object GradeBook the objectreference created by new GradeBook()Đại học Công nghệ. ĐHQG Hà Nội Classes and objects in Java 6Attributes, 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() : Stringprivate: //GradeBook.java + displayMessage()accessed by publicclassGradeBookthe class’s {methods only privateStringcourseName;//coursenameforthisGradeBook attribute. //methodtosetthecoursename Each Gradebook object has ...

Tài liệu được xem nhiều:

Tài liệu cùng danh mục:

Tài liệu mới: