By GokiSoft.com| 15:20 11/03/2020|
Java Basic

Share Code- Chia sẻ code java overview - Class Object trong java - Ham tao la gi - Constructor la gi?


/*
 * 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 lession8;

/**
 *
 * @author Diep.Tran
 */
public class Book {
    String bookName, authorName;
    float price;

    public Book() {
    }

    public Book(String bookName, String authorName, float price) {
        this.bookName = bookName;
        this.authorName = authorName;
        this.price = price;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthorName() {
        return authorName;
    }

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

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" + "bookName=" + bookName + ", authorName=" + authorName + ", price=" + price + '}';
    }
}



/*
 * 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 lession8;

/**
 *
 * @author Diep.Tran
 */
public class Test2 {
    public static void main(String[] args) {
        //Class Object trong java de lam => tai sao chung ta phai su dung toi no
        //Bat viet chuong => mindset => code tat nhung cai gi cac ban co the code
        //vao trong 1 file => neu code => 100, 200 LOC (line of code) => no problem
        //1 file => 1000 LOC, 10.000 LOC => ko the nao doc dc => rat kho nhin va hieu
        //split (cat) nhieu file => to chuc code => tao ra that nhieu class (java)
        //Neu cac viet 1 chuong trinh quan ly 1 dau sach (ten sach, tac gia, gia ban,...)
        String bookName, authorName;
        float price;
        //OK => la co 1 quan
        //Neu nhieu quan sach => 10 quan => su dung mang
        String[] bookNameList = new String[10];
        String[] authorNameList = new String[10];
        float[] priceList = new float[10];
        //OK => tam thoi chap nhan
        //Bai toan => 1 mang 10 dau sach, 5 tac
        //sach (ten sach, tac gia, gia ban), tac gia(ten, tuoi, dia chi, email, sdt)
        //Hinh thanh lap trinh class object (Java, C++, PHP, C#, ...)
        //Structure (C programming language)
        //Tao 1 class object Book nhu the nao. khai bao thuoc tinh nhu the nao
        //su khac biet structure & class object la gi
        //class object => noi toi 1 doi tuong book, tac gia => moi 1 class object no se
        //luu toan bo thuoc tinh + actions (methods, function) trong class object
        //Thuc hien tao class object => Book gom cac thuoc tinh (ten sach, tac gia, gia ban)
        //Van de 1 >> ham tao la gi & tai sao chung ta su dung toi ham tao.
        Book book = new Book();
        book.bookName = "Lap Trinh C";
        book.authorName = "Tran Van Diep";
        book.price = 100000;
        
        System.out.println(book.toString());
        
        Book book2 = new Book("Lap trinh html/css/js", "Quach Tuan Ngoc", 1000000);
        System.out.println(book2.toString());
    }
}


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

5

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