By GokiSoft.com| 15:47 14/09/2022|
Java Basic

[Source Code] Lập trình OOP & Tính chất kế thừa - C2109I


Nội dung kiến thức: - Lập trình OOP: - T/c bao đóng: OK - public/private/protected/friendly (internal) - T/c kế thừa - override -> - overloading - T/c đa hình - T/c trừu tượng ============================================================ Mini Project: Quản lý công dân Hà Nội - Công dân: Thuộc tính: Tên Ngày sinh Địa chỉ cccd Phương thức input display - Sinh viên Thuộc tính: Tên Ngày sinh Địa chỉ cccd msv email Phương thức input display ... learning ...




#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.lesson04.oop;

import java.util.Scanner;

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

    public Citizen() {
        System.out.println("C");
    }

    public Citizen(String fullname, String address, String cccd, String birthday) {
        this.fullname = fullname;
        this.address = address;
        this.cccd = cccd;
        this.birthday = birthday;
    }

    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 getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
    
    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.println(toString());
    }

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


#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.lesson04.oop;

/**
 *
 * @author diepvan
 */
public class Main {
    public static void main(String[] args) {
//        Citizen citizen = new Citizen();
//        citizen.input();
////        citizen.fullname = "AAA";
////        citizen.address = "Ha Noi";
//        citizen.display();
//        
//        Student std = new Student();
//        std.input();
////        std.input2();
////        std.fullname = "BBB";
////        std.address = "Nam Dinh";
////        std.rollno = "R001";
//        std.display();
//        
//        std.showMessage();
//        std.showMessage(5);
        Student std = new Student();
    }
}


#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.lesson04.oop;

import java.util.Scanner;

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

    public Student() {
//        super();
        System.out.println("S");
    }

    public Student(String rollno, String email, String fullname, String address, String cccd, String birthday) {
        super(fullname, address, cccd, birthday);
        this.rollno = rollno;
        this.email = 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;
    }
    
//    public void input2() {
//        Scanner scan = new Scanner(System.in);
//        System.out.println("Nhap email: ");
//        email = scan.nextLine();
//        System.out.println("Nhap msv: ");
//        rollno = scan.nextLine();
//    }
    
    @Override
    public void input() {
        super.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 email: ");
        email = scan.nextLine();
        System.out.println("Nhap msv: ");
        rollno = scan.nextLine();
    }
    
//    public void display() {
//        System.out.println(toString());
//    }
//
    @Override
    public String toString() {
        return "fullname=" + fullname + ", address=" + address + ", cccd=" + cccd + ", birthday=" + birthday + ", rollno=" + rollno + ", email=" + email;
    }
    
    public void showMessage() {
        System.out.println("Hello 1");
    }
    
    public void showMessage(int x) {
        System.out.println("Hello > " + x);
    }
    
    public void showMessage(int x, int y) {
        System.out.println("Hello > " + x + ", " + y);
    }
    
}


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 đó