By GokiSoft.com|
15:26 19/06/2023|
Java Basic
[Share Code] Tìm hiểu tính chất đa hình và trừu tượng trong Java - C2209I
Nội dung kiến thức:
- OOP:
- T/c bao đóng -> DONE
- Thuộc tính truy xuất public/private/protected/default (friendly, internal)
- setter/getter
- T/c kế thừa -> DONE
- override
- overloading
- T/c đa hình: 1 object -> khởi tạo từ class object
- Đa hình là gì?
- 1 đối tượng cụ thể
- Liên tưởng:
object: các bạn sinh viên
- Bạn bè (bạn)
- Thầy <-> trò (bạn)
- Cha <-> con (bạn)
- Ông <-> chau (bạn)
- Con <-> cha (bạn)
- Ứng dụng của đa hình trong lập trình là gì?
- Xác định và hiệu nghiệp -> Khi nào thì nên dùng -> Khi nào thì ko???
- T/c trừu tượng
TH1: Khi nào thì nên đưa về abstract
Hiểu nên dùng khi nào?
TH2: Ý nghĩa của nó
Yêu cầu lớp con -> Thực hiện override toàn bộ phương thức abstract của lớp cha.
- static: Tìm hiểu về thành phần này
=============================================================================================
#Circle.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.bt985;
import java.util.Scanner;
/**
*
* @author teacher
*/
public class Circle extends Shape{
double PI = Math.PI;
double radius;
public Circle() {
}
public double getPI() {
return PI;
}
@Override
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ban kinh: ");
radius = Double.parseDouble(scan.nextLine());
}
@Override
public double getCV() {
return 2 * PI * radius;
}
@Override
public double getS() {
return PI * radius * radius;
}
public void showAbc() {
System.out.println("Xin chao ABC");
}
}
#HCN.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.bt985;
/**
*
* @author teacher
*/
public class HCN {
public static double width;
public double height;
public void showTest() {
System.out.println("Xin chao abc");
}
public static void showB() {
System.out.println("XIn chao B");
}
}
#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.bt985;
import java.util.Scanner;
/**
*
* @author teacher
*/
public class Main {
public static void main(String[] args) {
Circle circle = new Circle();
Rectangle hcn = new Rectangle();
int choose;
Scanner scan = new Scanner(System.in);
do {
showMenu();
choose = Integer.parseInt(scan.nextLine());
switch (choose) {
case 1:
System.out.println("Nhap dien tich hinh tron");
circle.input();
break;
case 2:
double cv = circle.getCV();
System.out.println("Chu vi hinh tron: " + cv);
break;
case 3:
double s = circle.getS();
System.out.println("Dien tich hinh tron: " + s);
break;
case 4:
System.out.println("Nhap thong tin HCN");
hcn.input();
break;
case 5:
double cvHcn = hcn.getCV();
System.out.println("Chu vi HCN: " + cvHcn);
break;
case 6:
double sHcn = hcn.getS();
System.out.println("Dien tich HCN: " + sHcn);
break;
case 7:
System.out.println("Thoat!!!");
break;
default:
System.out.println("Nhap sai!!!");
break;
}
} while(choose != 7);
}
static void showMenu() {
System.out.println("1. Nhap thong tin hinh tron");
System.out.println("2. Tinh CV Hinh Tron");
System.out.println("3. Tinh S Hinh Tron");
System.out.println("4. Nhap thong tin HCN");
System.out.println("5. Tinh CV HCN");
System.out.println("6. Tinh S HCN");
System.out.println("7. Thoat");
System.out.println("Chon: ");
}
}
#Rectangle.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.bt985;
import java.util.Scanner;
/**
*
* @author teacher
*/
public class Rectangle extends Shape{
double width;
double height;
public Rectangle() {
}
@Override
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap chieu dai: ");
width = Double.parseDouble(scan.nextLine());
System.out.println("Nhap chieu rong: ");
height = Double.parseDouble(scan.nextLine());
}
@Override
public double getCV() {
return 2 * (width + height);
}
@Override
public double getS() {
return width * height;
}
public void testing() {
System.out.println("XIn chao testing");
}
}
#Shape.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.bt985;
/**
*
* @author teacher
*/
public abstract class Shape {
public abstract void input();
public abstract double getCV();
public abstract double getS();
public void xinchao() {
System.out.println("Xin chao OKOK");
}
}
#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.bt985;
/**
*
* @author teacher
*/
public class Test {
public static void main(String[] args) {
// Shape shape = new Shape(); //Binh thuong
// shape.input();
// shape.xinchao();
Circle circle = new Circle();
circle.input();
System.out.println("Dien tich: " + circle.getS());
// circle.xinchao();
// circle.showAbc();
//T/c Đa hình
// Circle c = new Shape(); Sai -> Ko tao dc
Shape c1 = new Circle();
Shape c2 = new Rectangle();
//c1, c2: Kieu du lieu la Shape
//c1 -> khoi tao tu Circle
//c2 -> khoi tao Rectangle
//c1 -> doi tuong that la gi: Circle
//c2 -> doi tuong that la gi: Rectangle
//Che do debug -> Code
//c1 ->Hieu la doi tuong Shape
// c1.xinchao();
// c1.getCV();
//Che do runtime -> Running
//c1 -> Hieu la doi tuong Circle
// c1.showAbc();
//Lam sao de goi showAbc()
if(c1 instanceof Circle) {
((Circle) c1).showAbc();
}
}
}
#Test01.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.bt985;
import java.util.ArrayList;
/**
*
* @author teacher
*/
public class Test01 {
public static void main(String[] args) {
//Quản lý thong tin hình tròn và hcn trong 1 mảng -> dataList
ArrayList<Circle> circles = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Circle c = new Circle();
c.input();
circles.add(c);
}
ArrayList<Rectangle> rectangles = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Rectangle r = new Rectangle();
r.input();
rectangles.add(r);
}
//Duyet qua 2 mang circles va rectangles -> tinh dien tich & chu vi
ArrayList<Shape> dataList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
//Da hinh -> s: Kieu du lieu la Shape
//s ->> doi tuong that la Circle
//s -> Circle
Shape s = new Circle();
s.input();
dataList.add(s);
}
for (int i = 0; i < 10; i++) {
Shape s = new Rectangle();
s.input();
dataList.add(s);
}
//dataList -> Gom cac phan tu: Circle & Rectangle
for (Shape shape : dataList) {
System.out.println("Dien tich: " + shape.getS());
}
}
}
#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.bt985;
/**
*
* @author teacher
*/
public class Test2 {
public static void main(String[] args) {
HCN hcn1 = new HCN();
HCN.width = 10;
hcn1.height = 20;
hcn1.showTest();
HCN hcn2 = new HCN();
System.out.println("Chieu rong: " + HCN.width + ", chieu cao: " + hcn2.height);
HCN.width = 100;
hcn2.height = 200;
hcn2.showTest();
System.out.println("Chieu rong: " + HCN.width + ", chieu cao: " + hcn1.height);
System.out.println("Chieu rong: " + HCN.width + ", chieu cao: " + hcn2.height);
HCN.showB();
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)