By GokiSoft.com| 21:07 16/03/2020|
Java Basic

Share Code- Chia sẻ code java lession 3

- Lập trình hướng đối tượng là gì => Today
Quản lý sinh viên:
- Xác định => có những đối tượng nào
- Sinh viên
- Quản lý thông tin gì trong đối tượng sinh viên
tên, tuổi, địa chỉ, email, sđt, rollno => var, propertes, thuộc tính
- Hành động của sinh viên => function, hàm, phương thức, methods
=> Học
=> Chạy bộ
.v.v.
=> khái bao tất cả thuộc tính + hành động => lớp đôi tương => OOP.
=> Triển khải code như thế
- Điểm
- Thuoc
- Actions
- nhap thoong tin diem => function => actions
- hien thi thong itn diem => function => actions
- Môn học
- Khoá học - Kỳ học (SEM1, SEM2, SEM3, SEM4)
- Hieu dc cach
- Hieu dc constructor
- Thuộc tính truy xuất
=> public => cong khai => goi dc o moi noi
=> private => chi goi dc trong class object chinh no
=> protected => chi goi dc trong class object chinh no + class khac nhung cung package
- Tính chất trong lập trình hướng đối tượng
=> Tính bao đóng => Today => OKOK
=> Tính kế thừa => Today
=> hay & can tim hieu sau
Quan ly thong tin cong dan o thanh pho Ha Noi
=> Cong dan Citizen (People) => Class Parent (Bo)
Thuoc tinh: ten , tuoi , dia
Chay bo
=> Sinh vien => Class Child (Chung)
Thuoc tinh: ten, tuoi, dia chi, email, rollNo
Actions
Chay bo
Learning
=> Engineer
=> ???

=> Tai su dung lai code co san => ngan gon

=> Ke thua
=> Hieu y nghia tai sao dung ke thua
=> Hieu ban chat constructor
=> Methods
=> Override (ghi de phuong thuc)
=> OOP
=> Overloading: qua tai => 
=> Tính đa hình => Next
=> Tính trừu tượng => Next


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

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        //new Student() => ham tao => khoi tao khong gian luu tru cho doi tuong
        //can dc khoi tao
        //co bao nhieu ham tao cho 1 class object => vo so
        //chi can tao nhung ham tao can thiet.
        Student leecuongStd = new Student();
        
        leecuongStd.fullname = "Lee Cuong";
        leecuongStd.age = 18;
//        leecuongStd.address = "Ha Noi";
        leecuongStd.email = "leecuong@gmail.com";
        leecuongStd.phoneNumber = "1234567890";
        leecuongStd.rollNo = "R001";
        
        leecuongStd.running();
        
        Student khiemStd = new Student();
        
        khiemStd.fullname = "Khiem Nhat";
        khiemStd.age = 18;
        //private => khong goi dc o class object khac
//        khiemStd.address = "Ha Noi";
        khiemStd.email = "khiemnhat@gmail.com";
        khiemStd.phoneNumber = "1234567890";
        khiemStd.rollNo = "R001";
        
        khiemStd.running();
        
        Student std = new Student("A", "Ha Noi");
        std.running();
        //ham tao dung de lam.
    }
}



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

/**
 *
 * @author Diep.Tran
 */
public class People {
    protected String fullname;
    private String address;
    public String phoneNumber;
    public int age;
    
    public People() {
        System.out.println("People init");
    }

    public People(String fullname, String address, String phoneNumber, int age) {
        this.fullname = fullname;
        this.address = address;
        this.phoneNumber = phoneNumber;
        this.age = age;
    }
    
    public void running() {
        System.out.println("People is running");
    }
}



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

/**
 *
 * @author Diep.Tran
 */
public class Student {
    protected String fullname;
    private String address;
    public String email;
    public String phoneNumber;
    public String rollNo;
    public int age;
    
    //ham tao mac dinh, default, ham tao khong doi, ham tao ko param
    public Student() {
        System.out.println("Init class object");
        //code them bat ky cai gi vao day.
    }
    
    public Student(String ten, String diachi) {
        fullname = ten;
        address = diachi;
    }
    
    public Student(String fullname) {
        this.fullname = fullname;
    }
    
//    //khong tao ra dc nhieu ham tao co cung tham so.
//    public Student(String fullname) {
//        this.fullname = fullname;
//    }
//    
//    //khong tao ra dc nhieu ham tao co cung tham so.
//    public Student(String address) {
//        this.address = address;
//    }

    public Student(String fullname, String address, String email, String phoneNumber, String rollNo, int age) {
        this.fullname = fullname;
        this.address = address;
        this.email = email;
        this.phoneNumber = phoneNumber;
        this.rollNo = rollNo;
        this.age = age;
    }
    
    public void running() {
        //source code
        System.out.println(this.fullname+ " is running");
    }
    
    public void learning() {
        System.out.println(fullname + "Student is learning");
    }
}



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

/**
 *
 * @author Diep.Tran
 */
public class Student2 extends People{
    //default => protected
    String email;
    String rollNo;

    public Student2() {
        //ham tao parent => neu ko co => compile no se tu dong them vao
        super();
        System.out.println("Student init");
    }

    public Student2(String email, String rollNo, String fullname, String address, String phoneNumber, int age) {
        super(fullname, address, phoneNumber, age);
//ham tao parent => neu ko co => compile no se tu dong them vao
//        super();
        this.email = email;
        this.rollNo = rollNo;
    }

    @Override
    public void running() {
        super.running();
        System.out.println("Student is running");
        super.running();
        super.running();
        super.running();
        super.running();
        super.running();
        super.running();
        super.running();
    }
    
    public void learning() {
        System.out.println("Student is learning");
    }
    
    public void learning(String str) {
        
    }
    
    //Overloading.
    public void learning(String str, int x) {
        
    }

    //getter && setter
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getRollNo() {
        return rollNo;
    }

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



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

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class TamGiac {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap N = ");
        int N = input.nextInt();
        
        for (int i = 1; i <= N; i++) {
            for (int j = 0; j < i; j++) {
                System.out.print("*");
            }
            System.out.println("");
        }
    }
}



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

/**
 *
 * @author Diep.Tran
 */
public class Test {
    public static void main(String[] args) {
//        Student std = new Student();
//        std.fullname = "ABC";
//        
//        std.running();
        
//        People people = new People();
//        people.running();
        
        Student2 std = new Student2();
        std.running();
        std.learning();
    }
}



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

import lession3.Student;

/**
 *
 * @author Diep.Tran
 */
public class Test {
    public static void main(String[] args) {
        Student std = new Student();
        //khong su dung dc protected o khac package
//        std.fullname = "Tran Van Diep";
//        std.address = "Ha Noi";
        std.running();
    }
}


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

5

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