By GokiSoft.com|
20:11 29/06/2022|
Java Basic
[Source Code] Tìm hiểu static & tính chất trong OOP - C2108L
Nội dung kiến thức học:
- static -> Nghĩa là gì
- T/c trong lập trình OOP
- T/c bao đóng -> OK
- T/c kế thừa
- Override
- Overloading
- T/c đa hình
- T/c trừu tượng
=====================================================
Viết 1 chương trình quản lý nhân viên của công cty Aprotrain Aptech:
- SRO
- TuVan
- GiaoVien
- Thuộc tính:
- Mã NV
- Ten
- Ngay sinh
- Địa chỉ
- Danh sach lop hoc day
- Phương thức:
- Nhập dữ liệu
- Hiển thị
- Công việc
Nói 1 khai niệm chung chung
- NhanVien
- Thuộc tính:
- Mã NV
- Ten
- Ngay sinh
- Địa chỉ
- Phương thức:
- Nhập dữ liệu
- Hiển thị
- Công việc
- Triển khai:
#ClassA.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 lesson05;
/**
*
* @author std
*/
public class ClassA {
public static int x;
public int y;
public void display() {
System.out.println("x = " + x + ", y = " + y);
}
}
#GiaoVien.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 lesson05;
import java.util.ArrayList;
import java.util.Scanner;
/**
* GiaoVien -> child class -> lop con
* @author std
*/
public class GiaoVien extends NhanVien{
ArrayList<String> classList;
public GiaoVien() {
classList = new ArrayList<>();
System.out.println("Ham tao giao vien");
}
@Override
public void input() {
//Lam sao de su dung dc input() -> parent class
Scanner scan = new Scanner(System.in);
System.out.println("Nhap so lop day: ");
int N = Integer.parseInt(scan.nextLine());
for (int i = 0; i < N; i++) {
System.out.println("Nhap lop: ");
String className = scan.nextLine();
classList.add(className);
}
super.input();
}
void input(String msg) {
System.out.println("OKOK");
}
protected void input(String msg, String kk) {
System.out.println("OKOK");
}
void testing01(String msg) {
System.out.println("OKOK");
}
protected void testing02(String msg, String kk) {
System.out.println("OKOK");
}
public void testing03(String msg, String kk) {
System.out.println("OKOK");
}
@Override
public void display() {
super.display();
System.out.println("Hien thi danh sach lop hoc");
for (String className : classList) {
System.out.println(className);
}
}
/***
* Trường hơp cụ thể -> xác định đc công việc của giáo viên sẽ làm
*/
@Override
public void working() {
System.out.println("Dạy hoc");
}
}
#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 lesson05;
/**
*
* @author std
*/
public class Main {
public static void main(String[] args) {
ClassA a = new ClassA();
a.x = 10;
a.y = 12;
ClassA b = new ClassA();
b.x = 2;
b.y = 6;
a.display();
b.display();
}
}
#NhanVien.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 lesson05;
import java.util.Scanner;
/**
* NhanVien -> parent class -> lop cha
* @author std
*/
public abstract class NhanVien {
String manv;
String fullname;
String birthday;
public NhanVien() {
System.out.println("Ham tao nhan vien");
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap MaNV: ");
manv = scan.nextLine();
System.out.println("Nhap ten: ");
fullname = scan.nextLine();
System.out.println("Nhap ngay sinh: ");
birthday = scan.nextLine();
}
public void display() {
System.out.format("\nMaNV: %s, ten: %s, ngay sinh: %s", manv, fullname, birthday);
}
public void testing() {
System.out.println("Testing ...");
}
/***
* Trường hợp này không biết nhân viên sẽ làm công việc j.
* Nhân viên sẽ có công việc để làm -> ??? Tìm hiểu sau
*/
// public void working() {
// System.out.println("Khong biet nhan vien lam cong viec gi???");
// }
public abstract void working();
}
#SRO.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 lesson05;
/**
*
* @author std
*/
public class SRO extends NhanVien{
@Override
public void working() {
System.out.println("Quan ly hoc vien ...");
}
}
#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 lesson05;
/**
*
* @author std
*/
public class Test {
public static void main(String[] args) {
// NhanVien nv = new NhanVien();
// nv.input();
// nv.working();
GiaoVien gv = new GiaoVien();
gv.input();
gv.testing();
System.out.println("Ten: " + gv.fullname);
gv.working();
//public -> protected -> default -> private
}
}
#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 lesson05;
import java.util.ArrayList;
/**
*
* @author std
*/
public class Test01 {
public static void main(String[] args) {
// NhanVien nv = new NhanVien();
// nv.working();
GiaoVien gv = new GiaoVien();
gv.working();
//Tim hieu khac biet
//TH -> nhanvien -> Duoc hieu la doi tuong: NhanVien
//TH -> chuong trinh chay runtime -> GiaoVien
NhanVien nhanvien = new GiaoVien();
nhanvien.working();
nhanvien.testing();
//Ung dung:
//TH tao 1 mang -> Quan ly nhan vien, giao vien, SRO, Tu van -> lam nhu the nao
ArrayList<NhanVien> staffList = new ArrayList<>();
// NhanVien nv01 = new NhanVien();
// staffList.add(nv01);
NhanVien nv01 = new GiaoVien();
staffList.add(nv01);
//For -> duyet -> kiem soat data.
// NhanVien nv02 = new NhanVien();
// GiaoVien gv02 = new GiaoVien();
NhanVien nv02 = new GiaoVien();
nv02.testing();
nv02.working();
if(nv02 instanceof GiaoVien) {
((GiaoVien) nv02).testing03("asdasd", "234234");
}
}
}
#Sub/ClassB.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 lesson05.sub;
import lesson05.GiaoVien;
/**
*
* @author std
*/
public class ClassB extends GiaoVien{
public void show() {
testing02("123123", "sfsdf");
// testing01("234234");
}
}
#Sub/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 lesson05.sub;
import lesson05.GiaoVien;
/**
*
* @author std
*/
public class Test {
public static void main(String[] args) {
GiaoVien gv = new GiaoVien();
// gv.testing01("123123");
// gv.testing02("123123", "okok");
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)