By GokiSoft.com| 19:29 17/04/2024|
Java Basic

[Share Code] Tìm hiểu về kế thừa trong lập trình hướng đối tượng - C2307L

Lập trình hướng đối tượng: OOP
	- Tính chất bao đóng -> OK
		- Tìm hiểu tạo thuộc tính, methods
		- Tìm hiểu về hàm tạo
		- Tim hieu ve access properties (public, private, protected, default)
	- Tính chất kế thừa -> Today
		- Override
		- Overloading
	- Tính chất đa hình
	- Tính chất trừu tường
===============================================================
Bài toán:
- Xây dựng 1 chương trình quản lý công dân ở Hà Nội. Phân loại được công dân theo nghề nghiệp.
	- Quản lý danh sách công dân
	- Nhập thông tin
	- Hiển thị

- Phân tích (Trao đổi với bên thuê chúng ta triển khai)
	- Công dân
		- Thuộc tính gì?
			- fullname
			- birthday
			- address
			- cccd
		- Hành động gì?
			- input
			- display
	- Sinh viên
		- Thuộc tính gì?
			- fullname
			- birthday
			- address
			- cccd
			- rollno
			- email
		- Hành động gì?
			- input
			- display
	- Công nhân
		- Thuộc tính gì?
			- fullname
			- birthday
			- address
			- cccd
			- level
		- Hành động gì?
			- input
			- display
	- Giáo viên
		- Thuộc tính gì?
			- fullname
			- birthday
			- address
			- cccd
			- email
			- teacherNo
		- Hành động gì?
			- input
			- display
	...

#Student.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.c2307l.lesson04;

import java.util.Scanner;

/**
 *
 * @author diepvan
 */
public class Student extends Citizen{
    String rollNo;
    String email;

    public String getRollNo() {
        return rollNo;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
    
    /**
     * Overloading -> Nap chong phuong thuc
     */
    public void sendMsg() {
        System.out.println("Hello default");
    }
    
    public void sendMsg(String msg) {
        System.out.println("Hello > " + msg);
    }
    
    public void sendMsg(String msg, String error) {
        System.out.println("Hello > " + msg + " > " + error);
    }
    
    @Override
    public void input() {
        super.input();
        
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap MSV: ");
        rollNo = scan.nextLine();
        System.out.println("Nhap email: ");
        email = scan.nextLine();
    }
    
//    @Override
//    public void display() {
////        System.out.format("\nTen: %s, ngay sinh: %s, dia chi: %s, cccd: %s, msv: %s, email: %s\n", 
////                fullname, birthday, address, cccd, rollNo, email);
////        Cach 1
////        System.out.println(toString());
////        Cach 2
////        String line = toString();
////        System.out.println(line);
//        //Cach 3
//        System.out.println(this);
//    }
    
    //Cach viet code nhanh

    @Override
    public String toString() {
        return super.toString() + ", rollNo=" + rollNo + ", email=" + email;
    }
}

#Main.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.c2307l.lesson04;

/**
 *
 * @author diepvan
 */
public class Main {
    public static void main(String[] args) {
//        Citizen citizen = new Citizen();
//        citizen.input();
//        citizen.display();
        
//        citizen.fullname = "ABC";
        
//        System.out.println("Ten: " + citizen.fullname);

        Student std = new Student();
//        std.input();
//        std.display();
        std.sendMsg();
        std.sendMsg("ABC");
    }
}

#Citizen.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.c2307l.lesson04;

import java.util.Scanner;

/**
 *
 * @author diepvan
 */
public class Citizen {
    String fullname;
    String birthday;
    String address;
    String cccd;

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

    public String getCccd() {
        return cccd;
    }

    public void setCccd(String cccd) {
        //Dat dieu kien: cccd -> 12 ky tu
        if(cccd.length() != 12) {
            System.out.println("Error!!!");
            return;
        }
        
        this.cccd = cccd;
    }
    
    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();
        System.out.println("Nhap CCCD: ");
        cccd = scan.nextLine();
    }
    
    public void display() {
//        System.out.format("\nTen: %s, ngay sinh: %s, dia chi: %s, cccd: %s\n", 
//                fullname, birthday, address, cccd);
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "fullname=" + fullname + ", birthday=" + birthday + ", address=" + address + ", cccd=" + cccd;
    }
}

#Worker.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.c2307l.lesson04;

import java.util.Scanner;

/**
 *
 * @author diepvan
 */
public class Worker extends Citizen{
    String level;

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }
    
    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();
        System.out.println("Nhap CCCD: ");
        cccd = scan.nextLine();
        System.out.println("Nhap trinh do: ");
        level = scan.nextLine();
    }
    
    public void display() {
        System.out.format("\nTen: %s, ngay sinh: %s, dia chi: %s, cccd: %s, trinh do: %s\n", 
                fullname, birthday, address, cccd, level);
    }
}

#Test.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.c2307l.lesson04.sub;

import com.gokisoft.c2307l.lesson04.Citizen;

/**
 *
 * @author diepvan
 */
public class Test {
    public static void main(String[] args) {
        Citizen citizen = new Citizen();
        citizen.input();
        
//        citizen.fullname = "ABC";
        citizen.setFullname("ABC");
        citizen.setCccd("sdfsdfsdf");
        
//        System.out.println("Ten: " + citizen.fullname);
        System.out.println("Ten: " + citizen.getFullname());
    }
}
Tags:

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

5

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