By GokiSoft.com|
15:34 21/06/2023|
Java Basic
[Share Code] Tìm hiểu Interface - C2209I
Nội dung kiến thức:
- Chữa bài tập
- Overview OOP
- T/c đóng gói
- T/c kế thừa
- override
- overloading
- T/c đa hình
- T/c trừu tượng
- Tìm hiểu interface
- Test nhanh:
- Kiểm tra lại toàn bộ OOP (basic)
- Logic cở bản
=====================================================================
Bài toán:
- Citizen:
Thuộc tính:
name
Phương thức:
running()
- Student
Phương thức:
running()
- Dog:
Phương thúc:
running()
- Car:
Phương thức:
running()
Yêu cầu:
Tạo 1 mảng -> quản ly đc tất cả các đối tượng: Citizen, Student, Dog, Car trong 1 mang
Mục đích -> Xử lý gọi function running() của tất cả đối tương cho dễ.
Code như thế nào???
Giải pháp:
???
#Car.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.lesson04;
/**
*
* @author teacher
*/
public class Car implements IRunning{
@Override
public void running() {
System.out.println("Car is running");
}
}
#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.lesson04;
/**
*
* @author teacher
*/
public class Citizen implements IRunning{
String name;
public Citizen() {
}
public Citizen(String name) {
this.name = name;
}
@Override
public void running() {
System.out.println("Citizen is running");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
#Dog.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.lesson04;
/**
*
* @author teacher
*/
public class Dog implements IRunning{
@Override
public void running() {
System.out.println("Dog is running");
}
}
#IRunning.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
*/
package com.gokisoft.c2209i.lesson04;
/**
*
* @author teacher
*/
public interface IRunning {
//public void running()
void running();
}
#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.lesson04;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author teacher
*/
public class Main {
public static void main(String[] args) {
List<IRunning> dataList = new ArrayList<>();
Citizen c = new Citizen();
dataList.add(c);
Student std = new Student();
dataList.add(std);
Dog d = new Dog();
dataList.add(d);
Car car = new Car();
dataList.add(car);
for (IRunning run : dataList) {
run.running();
}
}
}
#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.lesson04;
/**
*
* @author teacher
*/
public class Student extends Citizen{
@Override
public void running() {
System.out.println("Student is running");
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)