By GokiSoft.com| 15:08 12/09/2022|
Java Basic

[Source Code] Tìm hiểu lập trình OOP - Phần 1 - C2109I



Nội dung kiến thức học:
	OOP -> Lập trình hướng đối tượng
		- Ý nghĩa của lập trình OOP
		- T/c trong lập trình OOP
			- Tính chất bao đóng
				- Ham tao
				- Thuoc tinh truy xuat: public/protected/private
					- public
						- Goi trong chinh class cua no -> OK
						- Goi trong class khac cung package -> OK
						- Goi trong class khac -> ko cung package -> OK
						-> goi dc thuoc tinh/method -> moi noi -> OK
					- protected/friendly (default/internal)
						- Goi trong chinh class cua no -> OK
						- Goi trong class khac cung package -> OK
					- private
						- Goi trong chinh class cua no -> OK
						-> Ko goi dc thuoc tinh/phuong thuc -> ngoai class do.
				- getter/setter
				- Static
			- Tính chất kế thừa
				- Overloading
				- Override
			- Tính chất đa hình
			- Tính chất trừu tượng
		- Interface
==================================================================
Quản lý công dân thành phố HN (VN)
	- Công dân noi chung -> Class Object: CongDan
		- Quản lý thuộc tính gì
			- Tên -> String
			- Ngày sinh -> String
			- Địa chị -> String
			- cccd -> String
			- giới tính -> String
			...
		- Những hành động gì -> function (hàm), methods (phương thức)
			- ăn
			- ngủ
			- chạy bộ
			...
			- nhập thông tin
			- Hiển thị thông tin
	- Sinh viên
		- Thuộc tính
			- Tên
			- Ngày sinh
			- Địa chỉ
			- cccd
			- giới tính
			- email
			- msv
			...
		- Hành động
			- ăn
			- ngủ
			- chạy bộ
			- học
			...
			- nhập thông tin
			- hiển thị thông tin
	- Công nhân
	- Giáo viên
	- ...




#Test04.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 com.gokisoft.c2109i.lesson03;

/**
 *
 * @author Diep.Tran
 */
public class Test04 {
    public static void main(String[] args) {
        System.out.println("Xin chao");
        
//        Test04 t = new Test04();
//        t.showMenu();
        showMenu();
        
        Calculator c1 = new Calculator();
        Calculator.x = 10;
        c1.y = 12;
        System.out.println(Calculator.x);
        
        Calculator c2 = new Calculator();
        Calculator.x = 16;
        c2.y = 20;
        
        System.out.println(Calculator.x + ", " + Calculator.x);
    }
    
    static void showMenu() {
        System.out.println("Vi du 1");
        System.out.println("Vi du 2");
        System.out.println("Vi du 3");
        System.out.println("Vi du 4");
        System.out.println("Vi du 5");
    }
}


#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 com.gokisoft.c2109i.lesson03;

/**
 *
 * @author Diep.Tran
 */
public class Test03 {
    public static void main(String[] args) {
        NewCongDan cd = new NewCongDan();
        cd.setFullname("Tran Van A");
        
        System.out.println(cd.getFullname());
        
        cd.gender = "ABC";
        
    }
}


#Test01.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 com.gokisoft.c2109i.lesson03;

/**
 *
 * @author Diep.Tran
 */
public class Test01 {
    public static void main(String[] args) {
        CongDan cdA = new CongDan();
        //new CongDan() -> Ham tao
        cdA.fullname = "TRAN VAN A";
        cdA.birthday = "08/08/1999";
        cdA.address = "Ha Noi";
//        cdA.cccd = "12345678";
        cdA.gender = "Nam";
        
        
    }
}


#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 com.gokisoft.c2109i.lesson03;

/**
 *
 * @author Diep.Tran
 */
public class Test {
    public static void main(String[] args) {
        //Bieu dien thong tin cu the cua 1 Cong dan
        //Vidu: TRAN VAN A, 08/08/1999, ha noi, 12345678, Nam
        CongDan cdA = new CongDan();
        //new CongDan() -> Ham tao
        cdA.fullname = "TRAN VAN A";
        cdA.birthday = "08/08/1999";
        cdA.address = "Ha Noi";
//        cdA.cccd = "12345678";
        cdA.gender = "Nam";
        
        cdA.eating();
        
        System.out.println("Ten: " + cdA.fullname + ", gioi tinh: " + cdA.gender);
        
        CongDan cdB = new CongDan("TRAN VAN B", "Nam Dinh");
        System.out.println("Ten: " + cdB.fullname + ", dia chi: " + cdB.address);
        
    }
}


#NewCongDan.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 com.gokisoft.c2109i.lesson03;

/**
 *
 * @author Diep.Tran
 */
public class NewCongDan {
    String fullname;
    String address;
    String cccd;
    //Gender chia nhan cac gia tri: Male, Female, Other
    String gender;
    String birthday;

    public NewCongDan() {
    }

    public NewCongDan(String fullname, String address, String cccd, String gender, String birthday) {
        this.fullname = fullname;
        this.address = address;
        this.cccd = cccd;
        this.gender = gender;
        this.birthday = birthday;
    }
    
    public void input() {
        
    }
    
    public void display() {
        
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCccd() {
        return cccd;
    }

    public void setCccd(String cccd) {
        this.cccd = cccd;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        if(gender.equalsIgnoreCase("Male") || 
                gender.equalsIgnoreCase("Female") ||
                gender.equalsIgnoreCase("Other")) {
            this.gender = gender;
        } else {
            System.err.println("Error > " + gender);
        }
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
    
    
}


#CongDan.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 com.gokisoft.c2109i.lesson03;

/**
 *
 * @author Diep.Tran
 */
public class CongDan {
    public String fullname;
    protected String birthday;
    String address; //default, friendly, internal
    private String cccd;
    public String gender;
    
    public CongDan() {
        //Khoi tao du lieu
        //Code j vao day cung dc
        System.out.println("Ham tao khong doi so...");
    }
    
    public CongDan(String fname, String add) {
        fullname = fname;
        address = add;
    }

    public CongDan(String fullname, String birthday, 
            String address, String cccd, String gender) {
        this.fullname = fullname;
        this.birthday = birthday;
        this.address = address;
        this.cccd = cccd;
        this.gender = gender;
    }
    
    public void eating() {
        System.out.println("Eat...");
    }
    
    public void sleeping() {
        System.out.println("Sleep...");
    }
    
    public void input() {
        
    }
    
    public void display() {
        
    }
}


#Calculator.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 com.gokisoft.c2109i.lesson03;

/**
 *
 * @author Diep.Tran
 */
public class Calculator {
    public static int x;
    public int y;
}


#BT1375.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 com.gokisoft.c2109i.lesson03;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class BT1375 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap so phan tu trong mang N = ");
        int N = scan.nextInt();
        
        int[] t = new int[N];
        
        for (int i = 0; i < N; i++) {
            System.out.println("\nNhap phan tu t["+i+"] = ");
            t[i] = scan.nextInt();
        }
        
        //Cau 1: In danh sach cac so nguyen to
        System.out.println("\nSo nguyen to trong mang: ");
        for (int value : t) {
            if(isSoNguyenTo(value)) {
                System.out.println(value);
            }
        }
        
        //Cau 2: Sap xep cac phan tu trong mang
        Arrays.sort(t);
        
        System.out.println("Phan tu trong mang: ");
        for (int v : t) {
            System.out.println("v = " + v);
        }
    }
    
    static boolean isSoNguyenTo(int num) {
        int max = num / 2;
        for (int i = 2; i <= max; i++) {
            if(num % i == 0) return false;
        }
        
        return true;
    }
}


#Test02.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 com.gokisoft.c2109i.lesson03.sub;

import com.gokisoft.c2109i.lesson03.CongDan;

/**
 *
 * @author Diep.Tran
 */
public class Test02 {
    public static void main(String[] args) {
        CongDan cd = new CongDan();
        cd.fullname = "ABC";
//        cd.birthday = "08/08/1999";
//        cd.cccd = "12345678";
    }
}


Tags:



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

5

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

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó