By GokiSoft.com| 15:36 16/06/2023|
Java Basic

[Share Code] Tìm hiểu tính chất lập trình hướng đối tượng - C2209I

#Book.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.c2209i.lesson03;

import java.util.Scanner;

/**
 *
 * @author teacher
 */
public class Book {
    public String bookName;
    protected String authorName;
    private int pageNum;
    float price;

    public Book() {
    }

    public Book(String bookName, String authorName, int pageNum, float price) {
        this.bookName = bookName;
        this.authorName = authorName;
        this.pageNum = pageNum;
        this.price = price;
    }

    public void input() {
        Scanner scan = new Scanner(System.in);

        System.out.println("Nhap ten sach: ");
        bookName = scan.nextLine();
        System.out.println("Nhap tac gia: ");
        authorName = scan.nextLine();
        System.out.println("Nhap so trang: ");
        pageNum = Integer.parseInt(scan.nextLine());
        System.out.println("Nhap gia: ");
        price = Float.parseFloat(scan.nextLine());
    }

    public void display() {
        System.out.format("\nTen sach: %s, ten tac gia: %s, so trang: %d, gia: %f",
                bookName, authorName, pageNum, price);
    }
}

#BookNew.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.c2209i.lesson03;

/**
 *
 * @author teacher
 */
public class BookNew {
    String bookName;
    String authorName;
    int pageNum;
    float price;

    public BookNew() {
    }

    public BookNew(String bookName, String authorName, int pageNum, float price) {
        this.bookName = bookName;
        this.authorName = authorName;
        this.pageNum = pageNum;
        this.price = price;
    }

    public void display() {
        System.out.format("\nTen sach: %s, ten tac gia: %s, so trang: %d, gia: %f\n",
                bookName, authorName, pageNum, price);
    }
    
    public void setPageNum(int pageNum) {
        if(pageNum > 0) {
            this.pageNum = pageNum;
        } else {
            System.out.println("Du lieu gan sai > yeu cau pageNum > 0");
        }
    }
    
    public int getPageNum() {
        return pageNum;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthorName() {
        return authorName;
    }

    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        if(price < 0) {
            System.out.println("Nhap gia sai > yeu cau > price >= 0");
            return;
        }
        this.price = price;
    }
    
    
}

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

import java.util.Scanner;

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

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

    public Citizen(String fullname, String birthday, String cccd) {
        System.out.println("Tao citizen 2");
        this.fullname = fullname;
        this.birthday = birthday;
        this.cccd = cccd;
    }
    
    //overloading
    public void input(String msg) {
        System.out.println("ABC");
    }
    
    //overloading
    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 CCCD: ");
        cccd = 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 getCccd() {
        return cccd;
    }

    public void setCccd(String cccd) {
        this.cccd = cccd;
    }
    
    
}

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

/**
 *
 * @author teacher
 */
public class Main {
    public static void main(String[] args) {
        Book b = new Book();
        b.bookName = "LAP TRINH C";
        b.authorName = "ABC";
//        b.pageNum = 20; => Error => private
        b.price = 100;
        
        b.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.c2209i.lesson03;

import java.util.Scanner;

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

    public Student() {
//        super();
//        super("TRAN VAN A", "NGAY SINH", "1234");
        System.out.println("Tao student");
    }
    
    @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 CCCD: ");
//        cccd = scan.nextLine();
        System.out.println("Nhap MSV: ");
        rollNo = scan.nextLine();
    }

    public String getRollNo() {
        return rollNo;
    }

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

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

/**
 *
 * @author teacher
 */
public class Test {
    public static void main(String[] args) {
        BookNew b1 = new BookNew();
        b1.pageNum = 10;
        b1.price = 200;
        
//        System.out.println("gia: " + b1.price);
        System.out.println("Gia: " + b1.getPrice());
//        b1.display();
//        
//        BookNew b2 = new BookNew();
////        b2.pageNum = -10;
//        b2.setPageNum(-20);
////        b2.price = -200;
//        b2.setPrice(-200);
//        b2.display();
//        
//        BookNew b3 = new BookNew();
////        b3.pageNum = -20;
//        b3.setPageNum(-20);
////        b3.price = 2000;
//        b3.setPrice(2000);
//        b3.display();
    }
}

#Test2.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.c2209i.lesson03;

/**
 *
 * @author teacher
 */
public class Test2 {
    public static void main(String[] args) {
//        Citizen c = new Citizen();
//        c.input();
//        System.out.println("Ten: " + c.getFullname());
////        
        Student std = new Student();
        std.input();
        std.input("aasdasd");
//        System.out.println("Ten: " + std.getFullname() + ", MSV: " + std.getRollNo());
    }
}

#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.c2209i.lesson03.sub;

import com.gokisoft.c2209i.lesson03.Book;

/**
 *
 * @author teacher
 */
public class Test {
    public static void main(String[] args) {
        Book b = new Book();
        b.bookName = "LAP TRINH C";
//        b.authorName = "ABC"; => Error -> protected
//        b.pageNum = 20; => Error => private
//        b.price = 100; => Error => default
        
        b.display();
    }
}
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 đó