By GokiSoft.com| 10:15 16/07/2021|
Java Basic

[Share Code] Tìm hiểu Static - Final + So sanh protected & friendly (Default) + StringBuilder & StringBuffer lập trình Java




Nội dung kiến thức:
	- Chữa bài tập
	- Collections (List, ArrayList, Vector, ...)
	- Public/Protected/Private/Friendly (Default)
		Public -> Protected -> Friendly (Default) -> Private
	- Final + Static + ...
		Final
			- Bien -> Day du
			- Ham -> ???
	- String + StringBuilder + StringBuffer




#TestStatic.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 lesson06;

/**
 *
 * @author Diep.Tran
 */
public class TestStatic {
    public static void main(String[] args) {
        People p1 = new People();
        People p2 = new People();
        
        People.name = "A";
        People.name = "B";
        
        System.out.println("p1.name = " + People.name);
        System.out.println("p2.name = " + People.name);
        
        showMenu();
//        testing();

        TestStatic tt = new TestStatic();
        tt.testing();//code chay dc -> ko gap error.
    }
    
    void abc() {
        testing();
        showMenu();
    }
    
    void testing() {
        System.out.println("testing ...");
    }
    
    static void showMenu() {
        System.out.println("Menu ...");
    }
}


#Test03.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 lesson06;

/**
 *
 * @author Diep.Tran
 */
public class Test03 {
    public static void main(String[] args) {
        int x, y;
        float k;
        char c;
        String s1 = new String("asdads");//Khong nen khai bao
        String s2 = new String("sdasds");//Khong nen khai bao
        String s = "AAAA";
        
        s = s + "aa" + "bbb";
        s = s.concat("12312321");
        System.out.println("s = " + s);
        
        //Xu ly chuoi large -> noi chuoi
        StringBuilder builder = new StringBuilder();
        builder.append("asdasd");
        builder.append("234234");
        builder.append("fsdfsdf");
        builder.append("234dsfsdf");
        
        System.out.println(builder.toString());
        
        
        StringBuffer buffer = new StringBuffer();
        buffer.append("asdasd");
        buffer.append("234234");
        buffer.append("fsdfsdf");
        buffer.append("234dsfsdf");
        
        System.out.println(buffer.toString());
    }
}


#Test2.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 lesson06;

/**
 *
 * @author Diep.Tran
 */
public class Test2 {
    public static void main(String[] args) {
        TestStatic.main(null);
        
        TestStatic t = new TestStatic();
        t.abc();
    }
}


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

import lesson06.People;

/**
 *
 * @author Diep.Tran
 */
public class Test {
    public static void main(String[] args) {
        People p = new People();
//        p.name = "A";
//        p.age = 20;
//        p.address = "Ha Noi";
        
//        System.out.println("Name: " + p.name);
    }
}


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

import lesson06.People;

/**
 *
 * @author Diep.Tran
 */
public class Student extends People{
    String rollno;

    public Student() {
    }
    
    public void hienthi() {
        System.out.println("name: " + name);
//        System.out.println("address: " + address);
//        System.out.println("age: " + age);
        System.out.println("rollno: " + rollno);
    }
}


#People.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 lesson06;

/**
 *
 * @author Diep.Tran
 */
public class People {
    static final String CLASS_NAME = "People";//Hang so -> khong thay doi dc du lieu
    
    static protected String name = "123";
    String address;
    private int age;

    public People() {
    }
    
    public void nhap() {
        System.out.println("Nhap du lieu ...");
//        CLASS_NAME = "23132";
    }
}


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

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

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        ArrayList<String> list1 = new ArrayList<>();
        
        List<String> list2 = new ArrayList<>();
        list2 = new Vector<>();
        list2 = new LinkedList<>();
        
        People p = new People();
        p.name = "A";
//        p.age = 20;
        p.address = "Ha Noi";
        
        System.out.println("Name: " + p.name);
    }
}


Tags:

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

5

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