By GokiSoft.com| 20:21 06/07/2021|
Java Basic

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



Noi dung kien thuc: OOP -> Lap trinh huong doi tuong
- Class & Object + Khai niem lien quan
- Getter/Setter + Constructor -> function/method
- Public/Protected/Private/Default (Friendly) -> Thuoc tinh truy xuat
	- Public: goi dc moi class
	- Default/Protected: goi dc trong chinh class + class khac cung package
	- Private: chi goi dc trong chinh class.
- Tinh chat trong lap trinh OOP
	- Tinh dong goi -> OK
	- Tinh ke thua -> OK
		- override
	- Tinh da hinh
	- Tinh chat truu tuong
======================================================================
Mini Project: Quan ly bai do xe
- Xe Dap -> Bike
	- Thuoc tinh:
		- name -> String
		- color -> String
		- manufacturer -> String
	- Hanh dong nao kem theo (function/method)
		- input
		- display
		- running
		- stop
- Xe May -> MotoBike
- Xe Oto -> Car
	- Thuoc tinh:
		- name -> String
		- color -> String
		- manufacturer -> String
		- bien so xe (carNo) -> String
		- ...
	- Hanh dong nao kem theo (function/method)
		- input
		- display
		- running
		- stop
-> Chuyen phan tich tren -> Chuong trinh phan mem
-> Bieu dien -> doi tuong cu the:
	Car: Name -> Santafe, Color: White, manufacturerName -> Huyndai, carNo: 30K-888888 -> Object

======================================================
Mini Project: Quan ly cong dan & sinh vien




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

/**
 *
 * @author Diep.Tran
 */
public class Test02 {
    public static void main(String[] args) {
        Student std = new Student();
//        std.fullname = "A";
//        std.rollNo = "123";
        std.input();
        
        System.out.println("Name: " + std.getFullname());
    }
}


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

import java.util.ArrayList;

/**
 *
 * @author Diep.Tran
 */
public class Test {
    public static void main(String[] args) {
        //Khai bao mang doi Bike
        //C1: Khai bao mang co dinh
        Bike[] bikeList = new Bike[5];//Chua dc cap phat bo nho
        //Cap phat bo nho va nhap du lieu
        for (int i = 0; i < bikeList.length; i++) {
            bikeList[i] = new Bike();
            bikeList[i].input();
        }
        //Tuong tu -> duyet qua phan tu.
        
        //C2: Khai bao mang dong
        ArrayList<Bike> list = new ArrayList<>();
        Bike b1 = new Bike("A1");
        list.add(b1);
        Bike b2 = new Bike("A2");
        list.add(b2);
        //Nang cao quan ly doi
        b2 = new Bike("A3");
        list.add(b2);
        
//        for (Bike bike : list) {
//            System.out.println("Name: " + bike.getName());
//        }
        for (int i = 0; i < list.size(); i++) {
            Bike bike = list.get(i);
            System.out.println("Name: " + bike.getName());
        }
        
        //Code van de j ko?
        //In ra ket qua j?
    }
}


#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.Car;

/**
 *
 * @author Diep.Tran
 */
public class Test {
    public static void main(String[] args) {
        Car c1;//Chua cap phat bo nho cho bien c1
        c1 = new Car();//Bat dau cap phat bo nho cho c1 (thuoc tinh du lieu)
        //new Car() -> Java -> Ham tao (constructor) -> O dau & duoc sinh ra nhu nao
        c1.name = "Santafe";
//        c1.color = "White";
//        c1.manufacturerName = "Huyndai";
//        c1.carNo = "30K-888888";
        
        System.out.println("Name: " + c1.name);
        c1.running();
    }
}


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

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Student extends Citizen{
    String rollNo;

    public Student() {
    }

    public Student(String rollNo, String fullname, String birthday, String address) {
        super(fullname, birthday, address);
        this.rollNo = rollNo;
    }
    
    @Override
    public void input() {
        super.input();
        
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap MSV: ");
        rollNo = scan.nextLine();
    }

    public String getRollNo() {
        return rollNo;
    }

    public void setRollNo(String rollNo) {
        this.rollNo = rollNo;
    }
}


#MotoBike.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 MotoBike {
    
}


#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) {
        int t = 5, k;
        
        Car c1;//Chua cap phat bo nho cho bien c1
        c1 = new Car();//Bat dau cap phat bo nho cho c1 (thuoc tinh du lieu)
        //new Car() -> Java -> Ham tao (constructor) -> O dau & duoc sinh ra nhu nao
        c1.name = "Santafe";
        c1.color = "White";
        c1.manufacturerName = "Huyndai";
//        c1.carNo = "30K-888888";
        
        System.out.println("Name: " + c1.name);
        c1.running();
        
        Car c2 = new Car("Kia Morning");
        System.out.println("Name: " + c2.name);
        c2.running();
        
        Car c3 = new Car("Kia Morning", "Red");
        System.out.println("Name: " + c3.name);
        c3.running();
        
        //Tim hieu getter/setter -> Khuyen khich (canh bao) -> Ko dc phep thiet lap du lieu truc tiep cho bien (thuoc tinh) cua object tu ben ngoai
        Car c4 = new Car();
        c4.setName("Morning");
//        c4.setPrice(-250000000);
        /**
        int price = -250000000;
        c4.price = price;//Sai mat nghiep vu -> price > 0
        //Lam sao fix (sua) -> dc van de nay -> Thong bao nhap sai gia tien -> fix
        //Cach 1:
        if(price > 0) {
            c4.price = price;
        } else {
            System.out.println("Gia tien am -> nhap lai");
        }
        */
        //Cach 2: setter
        c4.setPrice(-250000000);
        System.out.println("Price: " + c4.getStringPrice());
    }
}


#Citizen.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.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Citizen {
    String fullname;
    String birthday;
    String address;

    public Citizen() {
    }

    /**
     * @param fullname -> Ten sinh vien
     * @param birthday -> ngay sinh
     * @param address -> Dia chi
     * Chuc nang: Ham tao -> Khoi tao du lieu.
     */
    public Citizen(String fullname, String birthday, String address) {
        this.fullname = fullname;
        this.birthday = birthday;
        this.address = address;
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ten: ");
        fullname = scan.nextLine();
        
        System.out.println("Nhap ngay sinh: ");
        birthday = scan.nextLine();
        
        System.out.println("Nhap dia chi: ");
        address = scan.nextLine();
    }

    public String getFullname() {
        return fullname;
    }

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

    public String getBirthday() {
        return birthday;
    }

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

    public String getAddress() {
        return address;
    }

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


#Car.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 Car {
    public String name;
    String color;
    protected String manufacturerName;
    private String carNo;
    public int price;
    
    //Ham tao & constructor -> ko bieu dien trong class nay -> Bien dich -> Tu sinh ra 1 ham tao default
    public Car() {
        //Ham tao -> Khoi tao khong gian du lieu + Gan du lieu cho cac thuoc tinh.
    }
    
    //Co bao nhieu ham tao -> vo so ham tao -> Tuy thuoc vao coder muon tao bao nhieu thi tao.
    //Redundant -> du thua code -> Ko nen tao ra no
    public Car(int x) {
        
    }
    //Redundant -> du thua code -> Ko nen tao ra no
    public Car(int x, int y) {
        
    }
    //Chu y: Ham tao -> tao ra -> Phuc vu 1 muc dich nao do.
    public Car(String carName) {
        name = carName;
    }
    
    public Car(String carName, String carColor) {
        name = carName;
        color = carColor;
    }
    
    public Car(String name, String color, String carNo) {
        this.name = name;
        this.color = color;
        this.carNo = carNo;
    }

    public Car(String name, String color, String manufacturerName, String carNo) {
        this.name = name;
        this.color = color;
        this.manufacturerName = manufacturerName;
        this.carNo = carNo;
    }
    
    public void input() {
        System.out.println("Nhap thong tin du lieu ...");
    }
    
    public void display() {
        System.out.println("Hien thi thong tin du lieu ...");
    }
    
    public void running() {
        System.out.println("Xe dang chay ...");
    }
    
    public void stop() {
        System.out.println("Xe dang dung ...");
    }

    public String getName() {
        return name;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getManufacturerName() {
        return manufacturerName;
    }

    public void setManufacturerName(String manufacturerName) {
        this.manufacturerName = manufacturerName;
    }

    public String getCarNo() {
        return carNo;
    }

    public void setCarNo(String carNo) {
        this.carNo = carNo;
    }

    public int getPrice() {
        return price;
    }
    
    public String getStringPrice() {
        if(price <= 0) {
            return "Not avaiable";
        }
        return String.valueOf(price);
    }

    //setter
    public void setPrice(int price) {
        if(price > 0) {
            this.price = price;
        } else {
            System.out.println("Thiet lap gia tien sai > yeu cau price > 0");
        }
    }
}


#Bike.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 Bike {
    String name;
    String color;
    String manufacturerName;

    public Bike() {
    }

    public Bike(String name) {
        this.name = name;
    }

    public Bike(String name, String color, String manufacturerName) {
        this.name = name;
        this.color = color;
        this.manufacturerName = manufacturerName;
    }
    
    public void input() {
        System.out.println("Nhap thong tin du lieu ...");
    }
    
    public void display() {
        System.out.println("Hien thi thong tin du lieu ...");
    }
    
    public void running() {
        System.out.println("Xe dang chay ...");
    }
    
    public void stop() {
        System.out.println("Xe dang dung ...");
    }

    public String getName() {
        return name;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getManufacturerName() {
        return manufacturerName;
    }

    public void setManufacturerName(String manufacturerName) {
        this.manufacturerName = manufacturerName;
    }
}


Tags:

Liên kết rút gọn:

https://gokisoft.com/2323

Bình luận