By GokiSoft.com| 09:36 02/07/2021|
Java Basic

[Share Code] Tìm hiểu Class Object - public private protected - hàm tạo - Lập trình Java - C2010G



Nội dung kiến thức:
- Class & Object
- public/private/protected/default (friendly)
- getter/setter
- Tính chất trong lập trình hướng đối tượng (OOP)
	- Tính chất đóng gói
	- Tính kế thừa 
	- Tính đa hình
	- Tính trừu tượng
=========================================================
Mini Project: Xây dựng ứng dụng quản lý thư viện
- Yêu cầu:
	- Quản lý được sách trong thư viện
	- Quản lý được danh mục sách
- Phân tích: Đối tượng (Class & Object)
	- Sách -> Book -> Class
		Thuộc tính:
			name -> String
			authorName -> String
			price -> float
			nxb -> String
			yearPublish -> String
		Phương thức (function/method)
			- display & input
	- Danh mục sách: Category
		Thuộc tính: name, danh sách các quấn sách
		Phương thức (function/method)
			- display & input
==========================================================
- Mo ta 1 quan sach cu the:
	Lap Trinh C, TRAN VAN DIEP, 200000, Aptech, 2020
- Thuoc tinh truy xuat:
	public/private/protected/default (friendly)
	public -> thuoc tinh/phuong thuc -> dc goi o moi noi (trong class/class khac cung package/class khac khac package)
	protected -> thuoc tinh/phuong thuc -> trong class/class khac cung package
	private -> thuoc tinh/phuong thuc -> trong class
	public > default > protected > private (default ~ protected)
	




#Test.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson03;

/**
 *
 * @author Diep.Tran
 */
public class Test {
    public static void main(String[] args) {
        BookNew b = new BookNew();
        
//        b.name = "Lap Trinh C";
        b.setName("Lap Trinh C");
//        b.price = -200000;//price >= 0.
        b.setPrice(-200000);
        
        System.out.println("Name: " + b.name + ", price: " + b.price);
    }
}


#sub/Test.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson03.sub;

import lesson03.Book;

/**
 *
 * @author Diep.Tran
 */
public class Test {
    public static void main(String[] args) {
        Book b1;//b1 -> Chua dc khoi tao -> null (Chua dc cap phat bo nho)
        //Book -> Class Object (Class) -> Lop doi tuong
        //b1 -> Object -> Doi tuong
        b1 = new Book();//b1 -> cap phat bo nho -> Tao ra khong gian bo nho -> luu du lieu thuoc tinh
        //new Book() -> Ham tao (constructor)
        b1.name = "Lap Trinh C";
//        b1.authorName = "TRAN VAN DIEP";
//        b1.nxb = "Aptech";
//        b1.price = 200000;
        b1.yearPublish = "2020";
        
        System.out.println("Name: " + b1.name);
    }
}


#Main.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson03;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        //Phan 1: Basic
        //Coi class Book -> data type (custom data type) -> su dung nhu int, float, String, double, ...
        Book b1;//b1 -> Chua dc khoi tao -> null (Chua dc cap phat bo nho)
        //Book -> Class Object (Class) -> Lop doi tuong
        //b1 -> Object -> Doi tuong
        b1 = new Book();//b1 -> cap phat bo nho -> Tao ra khong gian bo nho -> luu du lieu thuoc tinh
        //new Book() -> Ham tao (constructor)
        b1.name = "Lap Trinh C";
        b1.authorName = "TRAN VAN DIEP";
        b1.nxb = "Aptech";
//        b1.price = 200000;
        b1.yearPublish = "2020";
        
        System.out.println("Name: " + b1.name);
        //Phan 2: Y nghia co constructor
        Book b2 = new Book("Lap Trinh HTML/CSS/JS", 300000);
        
        System.out.println("Name: " + b2.name);
        
        Book b3 = new Book("SQL Server", 200000, "TRAN VAN ABC");
        
        System.out.println("Name: " + b3.name);
    }
}


#Category.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson03;

import java.util.ArrayList;

/**
 *
 * @author Diep.Tran
 */
public class Category {
    public String name;
    public ArrayList<Book> bookList;
    
    public void input() {
        System.out.println("Nhap du lieu cho thuoc tinh ...");
    }
    
    public void display() {
        System.out.println("Hien thi thong tin sach");
    }
}


#BookNew.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson03;

/**
 *
 * @author Diep.Tran
 */
public class BookNew {
    String name;
    String authorName;
    float price;
    String nxb;
    String yearPublish;

    public BookNew() {
    }

    public BookNew(String name, String authorName, float price, String nxb, String yearPublish) {
        this.name = name;
        this.authorName = authorName;
        this.price = price;
        this.nxb = nxb;
        this.yearPublish = yearPublish;
    }
    
    public void input() {
        System.out.println("Nhap du lieu cho thuoc tinh ...");
    }
    
    public void display() {
        System.out.println("Hien thi thong tin sach");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthorName() {
        return authorName;
    }

    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        if(price < 0) {
            System.err.println("Nhap gia sai > Yeu cau price > 0");
        } else {
            this.price = price;
        }
    }

    public String getNxb() {
        return nxb;
    }

    public void setNxb(String nxb) {
        this.nxb = nxb;
    }

    public String getYearPublish() {
        return yearPublish;
    }

    public void setYearPublish(String yearPublish) {
        this.yearPublish = yearPublish;
    }
}


#Book.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson03;

/**
 *
 * @author Diep.Tran
 */
public class Book {
    public String name;
    protected String authorName;
    private float price;
    String nxb;
    public String yearPublish;
    
    //Ham tao default
    public Book() {
    }
    
    //Ham tao rac -> tao ra ko de lam j
    public Book(String x, String y) {
    }
    
    //Ham tao rac -> tao ra ko de lam j
    public Book(float a) {
    }
    
    //Muc dich cua construct -> Khoi tao du lieu -> thiet gia tri cho thuoc tinh
    //Du lieu dc truyen vao -> cai dat cho thuoc tinh
    //Vi du: Tao ra 1 constructor -> truyen gia tri cho name, price
    public Book(String bookName, float bookPrice) {
        name = bookName;
        price = bookPrice;
    }
    
    //Vi du: Tao ra 1 constructor -> truyen name, price, authorName
    public Book(String name, float price, String authorName) {
        this.name = name;
        this.price = price;
        this.authorName = authorName;
    }

    public Book(String name, String authorName, float price, String nxb, String yearPublish) {
        this.name = name;
        this.authorName = authorName;
        this.price = price;
        this.nxb = nxb;
        this.yearPublish = yearPublish;
    }

    public Book(String name, String authorName, float price, String nxb) {
        this.name = name;
        this.authorName = authorName;
        this.price = price;
        this.nxb = nxb;
    }
    
    public void input() {
        System.out.println("Nhap du lieu cho thuoc tinh ...");
    }
    
    public void display() {
        System.out.println("Hien thi thong tin sach");
    }
}


Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)