Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 50 trang
THÔNG TIN TÀI LIỆU
Cấu trúc
Chapter 3. OOP Basic
Object - Oriented
Object Oriented
Class and Object
Class and Objects
Classes
class definition
Definition of member variable
Method definition
Variable length Argument
Example
Object Instantiate
Access to method and member variable
Exercise
Solution: Account class
Create class
Initialization of object
Overload
Slide 19
Solution
Solution: Overload.java
Constructor
Slide 23
Default constructor
Overloading constructor method
Using constructor
Slide 27
Slide 28
Slide 29
Assignment of variable
The static Modifier
Static Variables
Static member
Static variable
Static method
Slide 36
Account class
StaticVM class
Access modifier
Public access
Slide 41
Protected and Default and Private accessibility
Slide 43
Slide 44
Test2 class
Slide 46
Encapsulation
Slide 48
Slide 49
Slide 50
Nội dung
1 Chapter 3. OOPBasic ITSS Java Programming CAO Tuan-Dung, HUT 2 Object - Oriented Real world concepts can be modeled in a computer program The program is made of objects with certain properties and operations that the objects can perform It is not important how an object is implemented, but what it does Every object is responsible for carrying out a set of related tasks 3 Object Oriented Real world Attribute Account name: Duc Binh Balance: 2.000.000 VND Deposit Debit Balance inquiry Operation request Balance inquiry I want to inquiry the balance Computer world Attribute name: Duc Binh Balance: 2.000.000 VND Deposit() Withdraw() checkBalance() operation operation Account object User object Message transmission checkBalance() 4 Class and Object A group of objects with similar properties and behaviors is described by a class. Class is a blueprint for an object. A class contains fields and methods that define the behavior of the object. A class is a description of a kind of object. 5 Class and Objects Attribute name: Duc Binh Balance: 2.000.000 VND Deposit() Withdraw() checkBalance() operation Account object of Mr Duc Binh Attribute name: Thu Lan Balance: 1.000.000 VND Deposit() Withdraw() checkBalance() operation Account object of Mrs Thu Lan Account Class (model of objects) Attribute Definition of member variable String name; long balance; void deposit(int money); void withdraw() int checkBalance() Method definition operation INSTANTIATE 6 Classes A class can contain data declarations and method declarations int size, weight; char category; Data declarations Method declarations 7 class definition Syntax: <modifier> class <name> { <Definition of member variable> <Definition of method> } Example public class StudentRecord { // Definition of member variable // method definition } public class Account { // Definition of member variable // method definition } 8 Definition of member variable Format: [Modifier] Data type Member variable name,…; Example: public class Account { String name; // Account name long balance; // Balance : : void deposint (long money){ balance +=money; } } member variable can be used from anywhere if in the class 9 Method definition Syntax: [Modifier] Return Value type method- name (Arguments list) { Body of method } Example: public class Account { String name; // Account name long balance; // Balance : : void deposint (long money){ balance +=money; } } member variable can be used from anywhere if in the class 10 Variable length Argument In the method, a variable-length argument that can pass many values can be declared. You may not pass the value to a variable-length argument. In the method that uses a variable-length argument, we treat the passed argument as an array. Syntax: [Modifier] Type of return value Method name (Data type name… Variable name) { Processing; } [...]... { name = pName; balance = pMoney; System.out.println(“It has 2 arguments "); System.out.print("Account name : " + name); System.out.println(“/tBalance : " + balance + "Yen"); } } 20 Solution: Overload .java class Overload { public static void main(String args[]) { Account obj = new Account(); obj.setData("Lan Anh", 100000); // Call display() method without argument obj.display(); // Call display() method... Account(); obj.display(); System.out.println(); // Generation of account object(Constructor with 2 arguments) Account obj2 = new Account(“Nam Hai", 100000); obj2.display(); } } 29 Assignment of variable Basic data type int x = 20; int y = x; Reference type x 20 y 20 Account obj = new Account(); Account act = obj Account Object obj act obj and act refer the same object 30 The static Modifier We declare . 1 Chapter 3. OOP Basic ITSS Java Programming CAO Tuan-Dung, HUT 2 Object - Oriented Real world concepts can be modeled