Classes and Objects in Java_Object-oriented programming
Số trang: 13
Loại file: ppt
Dung lượng: 77.00 KB
Lượt xem: 21
Lượt tải: 0
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 ...
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ìm kiếm theo từ khóa liên quan:
corejava ngôn ngữ lập trình java lập trình hướng đối tượng Object oriented programming Classes in java Objects in JavaTài liệu có liên quan:
-
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 316 0 0 -
101 trang 211 1 0
-
14 trang 141 0 0
-
Giáo trình lập trình hướng đối tượng - Lê Thị Mỹ Hạnh ĐH Đà Nẵng
165 trang 129 0 0 -
Giáo trình Lập trình Windows 1 - Trường CĐN Đà Lạt
117 trang 104 0 0 -
Giáo trình Phân tích, thiết kế hướng đối tượng với UML: Phần 1 - Trường ĐH Công nghiệp Quảng Ninh
111 trang 103 0 0 -
265 trang 93 0 0
-
Giáo trình Lập trình hướng đối tượng với Java: Phần 2 - Trần Thị Minh Châu, Nguyễn Việt Hà
141 trang 86 0 0 -
Bài giảng Ngôn ngữ lập trình Java: Applet - TS. Nguyễn Thị Hiền
34 trang 77 0 0 -
33 trang 75 0 0