By GokiSoft.com| 20:02 13/07/2021|
Java Basic

[Share Code] Tìm hiểu final - static + String (StringBuilder & StringBuffer) + List - Kiểu truy xuất - Lập trình Java



Nội dung kiến thức:
	- final & static
		- static
			- thuoc tinh
			- phuong thuc
	- String (String + StringBuilder + StringBuffer)
	- List (ArrayList)
	- public/private/protected/friendly (default) -> OOP (Tính chất trong lập trình OOP)
		public -> protected -> friendly (default) -> private




#Test1.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 lesson05;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author Diep.Tran
 */
public class Test1 {
    public static void main(String[] args) {
        ArrayList<Book> list = new ArrayList<>();
//        list = new Vector<>();
        
        List<Book> list2 = new ArrayList<>();// interface.
        list2 = new Vector<>();
        
        String str = "abc";
        System.out.println(str.toUpperCase());
        int t = 5;//du lieu nguyen thuy.
        float f = 5.6f;
        
//        String str2 = new String("OKOK");
//        System.out.println(str2);
        str += "12321";
        str += "213";
        System.out.println(str);
        
        StringBuilder builder = new StringBuilder();
        builder.append("asdasd");
        builder.append("2ssds");
        builder.append("24dsfsdfsdf");
        
        System.out.println(builder.toString());
        
        StringBuffer buffer = new StringBuffer();
        buffer.append("asdasd");
        buffer.append("2ssds");
        buffer.append("24dsfsdfsdf");
        
        System.out.println(buffer.toString());
        
        
    }
}


#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 lesson05;

/**
 *
 * @author Diep.Tran
 */
public class Test {
    public static void main(String[] args) {
//        Circle c = new Circle(3);
//        
//        float cv = c.chuvi();
//        float dt = c.dientich();
//        
//        System.out.println("cv: " + cv + ", dt: " + dt);
        Book b1 = new Book("A", 1, "A");
        Book b2 = new Book("B", 2, "B");
        
        b1.display();
        b2.display();
        //Dat van -> sau khi sua float price -> static float price
        //Dieu j se xay ra
        //Ket qua in ra la gi
        //Co phai la: A, 1, A va B, 2, B ???
        System.out.println("Price: " + Book.price);
        System.out.println("Price: " + Book.price);
        
        //main -> phuong thuc static -> goi toi cac phuong thuc khac trong class -> thi cac phuong do do cung phai static
//        showMenu();
        Test t = new Test();
        t.showMenu();//Ko gap error trong TH nay.
    }
    
    void testing() {
        showMenu();
    }
    
    void showMenu() {
        System.out.println("1. Nhap du lieu");
        System.out.println("2. Hien thi thong tin");
        System.out.println("3. Thoat");
        System.out.println("Chon: ");
    }
}


#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 lesson05;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        Test test = new Test();
        test.testing();//testing(), showMenu() -> test: ko gap loi
//        test.showMenu();
        Test.main(null);//showMenu() -> ko xac dinh dc doi tuong cua no.
    }
}


#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 lesson05;

/**
 *
 * @author Diep.Tran
 */
public class Book {
    String name;//friendly
    static float price;
    protected String authorName;

    public Book() {
    }

    public Book(String name, float price, String authorName) {
        this.name = name;
        Book.price = price;
        this.authorName = authorName;
    }
    
    public void input() {
        System.out.println("Nhap thong tin du lieu");
    }
    
    public void display() {
        System.out.printf("\nname = %s, price = %f, authorName = %s\n", name, price, authorName);
    }
}


#Circle.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 lesson05;

/**
 *
 * @author Diep.Tran
 */
public class Circle {
    float r;
    static final float PI = 3.14f;//const

    public Circle(float r) {
        this.r = r;
    }

    public Circle() {
    }
    
    public float chuvi() {
        return 2 * PI * r;
    }
    
    public float dientich() {
        return PI * r * r;
    }
}


#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 lesson05.sub;

import lesson05.Book;

/**
 *
 * @author Diep.Tran
 */
public class Test {
    public static void main(String[] args) {
        Book b = new Book("A", 1, "A");
//        System.out.println(b.name);
//        System.out.println(b.authorName);
    }
}


#AptechBook.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 lesson05.sub;

import lesson05.Book;

/**
 *
 * @author Diep.Tran
 */
public class AptechBook extends Book{
    String course;
    
    public void showMsg() {
//        System.out.println("Ten sach: " + name);
        System.out.println("Ten tac gia: " + authorName);//Book & AptechBook -> khac package -> van goi dc authorName -> protected
    }
}


Tags:

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

5

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