0

OOP (Oriented Object Programming - OOP) Part 1

Bài viết mục đích chính là để viết ra những gì mình hiểu hoặc tổng hợp lại các nguồn và để có thể đọc lại note này bất cứ lúc nào.

Disclaimer: Bài viết có thể có tiếng anh để giữ nguyên ý nghĩa các thuật ngữ chuyên ngành

Intro

  • Different from Python, Java needs to be compiled before running
  • Acquiring type awareness is a must when writing Java programs
  • Java focuses on object-oriented modeling where everything is within some classes
  • Java Memory Model: Stack for function executions, Heap for object storage and garbage collection, Non-heap(Metaspace) for static fields etc.

1. OOP là gì?

Object Oriented Programming (OOP) is a programming paradigm that focuses on the use of objects to represent and manipulate data. In OOP, data is encapsulated within objects, and objects are defined by their properties (attributes) and behaviors (methods).

OOP là mô hình lập trình mà tập trung vào sử dụng Objects để thể hiện và thao tác với data. Data được đóng gói trong Objects và Objects được xác định bởi các properties and methods. image.png

2. Abstraction and Encapsulation (Sự trừu tượng và sự đóng gói)

2.1 Abstraction (Tính trừu tượng)

  • Data abstraction: Trừ tượng hóa data-> Change primitive to POJO
boolean contains(double pointX, double pointY, double centreX, double centreY, double radius){
    // by distance between two points on a graph formulae
    double dx = pointX - centreX;
    double dy = pointY - centreY;
    double distance = math.sqrt(dx*dx + dy*dy); 
    return distance < radius;
}
class Point {
    double x;
    double y;
}
class Circle {
    Point centre;
    double radius;
}
boolean contains(Point p, Circle c){
    double dx = p.x- c.x;
    double dy = p.y- c.y;
    double distance = math.sqrt(dx*dx + dy*dy); 
    return distance < c.radius;
}
  • Functional abstraction: Chia thành các hàm nhỏ
boolean contains(Point p, Circle c){
    double d = distance(p,c.centre);
    return d<c.radius;
}
double distance(Point a, Point b){
    double dx = a.x- b.x;
    double dy = a.y- b.y;
    return math.sqrt(dx*dx + dy*dy); 
}

2.2 Encapsulation (Tính đóng gói)

image.png

3, Inheritance and Polymorphism (Thừa kế và Trừu tượng hóa)

https://dev.to/tlylt/inheritance-and-polymorphism-oop-java-2-d8g

3.1. Inheritance

class Paper{
    private int height;
    private int width;
    private String color;

    // original
    Paper(int height, int width, String color){
        this.height= height;
        this.width = width;
        this.color = color;
    }
    // added, will be called if no parameters are given
    Paper(){
        this.height= 297;
        this.width = 210;
        this.color = "white"
    }
}
class PrinterPaper extends Paper{
    PrinterPaper(){
        super(297,210,"white");
    }
}

3.2. Polymorphism (many forms)

Polymorphism trong lập trình hướng đối tượng OOP cho phép một Object có thể có nhiều hình dạng và hành vi khác nhau Tính đa hình trong OOP được chia làm 2 loại:

  • Static Polymorphism (đa hình tĩnh): là cơ chế định nghĩa lại các methods cùng tên, nhưng có thể khác số lượng hoặc kiểu của tham số. Static Polymorphism còn được gọi là Method Overloading.
  • Dynamic Polymorphism (đa hình động): là cơ chế định nghĩa lại các methods cùng tên, cùng tham số và kiểu trả về từ parent class. Dynamic Polymorphism còn được gọi là Method Overriding. image.png

Sự khác biệt lớn nhất giữa Static và Dynamic Polymorphism là: Static Polymorphism được xử lý tại thời điểm biên dịch (compile-time). Dynamic Polymorphism được xử lý tại thời điểm chạy chương trình (runtime).

Reference


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí