By GokiSoft.com|
21:10 06/06/2020|
Java Basic
[Share Code] Bài giảng tính chất đa hình trong Java + Tính chất trừu tượng trong Java + Interface trong Java
[Share Code] Bài giảng tính chất đa hình trong Java + Tính chất trừu tượng trong Java + Interface trong Java
- Học lập trình OOP
- Khái niệm về lập trình OOP => Lession 2 => DONE
- Tại sao chúng ta phải học OOP => => Lession 2 => DONE
- Thuộc tính truy xuất => Lession 2 => DONE
- public
- protected
- private
- Tính chất của lập trình OOP
=> Tính đóng gói => Lession 2 => DONE
=> Tính kế thừa => Lession 2 => DONE
=> Tính đa hình => Lession 3 => DONE
=> Tính trừu tượng => Lession 3 => DONE
=> Biến method không body => abstract method => tạo abstract class
=> Class child bắt buộc phải implement abstract method của lớp cha abstract class
=> Không thể tạo trực tiếp object từ abstract class.
- Interface
=> so sánh vs tính chất trong OOP => Lession 3
Bài toán:
Xậy dựng 1 phần mềm quản lý động vật (Dog, cat, tiger), xe cộ, con người (công dân, sinh viên)
- Yêu cầu 1 thiết kế
Động vật
- Thuộc tính : tên, loại thức ăn
- Hành động:
- Chạy
- Tiếng kêu
Xe cộ
- Thuộc tính: tên, hãng xe
- Hành động:
- Chạy
Con người
- Thuộc tính: tên, địa chỉ, ngày sinh, email (sinh viên), rollNo (sinh viên)
- Hành động:
- chạy
- ngủ
- Yêu cầu triển khải
- Khai báo 3 đối tượng : dog, cat, tiger => gọi tới tiếng kêu
- Quản lý tắt cả hành động running() của động vật, xe cộ, con người => Chỉ sử dụng 1 List duy nhất
#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 lession3;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author teacher
*/
public class Main {
public static void main(String[] args) {
//Khai bao object cach 1
Dog dog = new Dog();
Cat cat = new Cat();
Tiger tiger = new Tiger();
dog.showSound();//go .. go
cat.showSound();//meo .. meo
tiger.showSound();//hummm .. hummm
//Khoi tao object cach 2
// Animal animal1 = new Animal();//Chúng ta không thể khởi tạo được object trực tiếp từ abstract class.
// animal1.showSound();//Khong in gi => Ok
//Tính chất đa hình trong lập trình OOP
Animal animal2 = new Dog();
animal2.showSound();
if(animal2 instanceof Dog) {
((Dog) animal2).testing();
}
//Tìm hiểu interface
List<IRunning> object2s = new ArrayList<>();
IRunning d = new Dog();
object2s.add(d);
object2s.add(new Dog());
object2s.add(new Cat());
object2s.add(new Cat());
object2s.add(new Tiger());
object2s.add(new Vehicle());
object2s.add(new Student());
for (IRunning a : object2s) {
a.running();
}
}
}
#Animal.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 lession3;
/**
*
* @author teacher
*/
public abstract class Animal implements IRunning{
String name, foodType;
public Animal(String name, String foodType) {
this.name = name;
this.foodType = foodType;
}
public Animal() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFoodType() {
return foodType;
}
public void setFoodType(String foodType) {
this.foodType = foodType;
}
@Override
public void running() {
System.out.println("Animal is running");
}
public abstract void showSound();
@Override
public String toString() {
return "Animal{" + "name=" + name + ", foodType=" + foodType + '}';
}
public void display() {
System.out.println(this);
}
}
#Cat.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 lession3;
/**
*
* @author teacher
*/
public class Cat extends Animal{
@Override
public void showSound() {
System.out.println("Meo .. meo ..");
}
@Override
public void running() {
System.out.println("Cat is running");
}
}
#Dog.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 lession3;
/**
*
* @author teacher
*/
public class Dog extends Animal{
@Override
public void showSound() {
System.out.println("Go .. go ..");
}
public void testing() {
System.out.println("Testing...");
}
@Override
public void running() {
System.out.println("Dog is running");
}
}
#Tiger.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 lession3;
/**
*
* @author teacher
*/
public class Tiger extends Animal{
@Override
public void showSound() {
System.out.println("Hummm ... hummm");
}
@Override
public void running() {
System.out.println("Tiger is running");
}
}
#People.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 lession3;
/**
*
* @author teacher
*/
public class People implements IRunning{
String name, address, birthday;
public People() {
}
public People(String name, String address, String birthday) {
this.name = name;
this.address = address;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public void running() {
System.out.println("People is running");
}
public void sleeping() {
System.out.println("People is sleeping");
}
}
#Student.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 lession3;
/**
*
* @author teacher
*/
public class Student extends People{
String email, rollNo;
public Student() {
}
public Student(String email, String rollNo, String name, String address, String birthday) {
super(name, address, birthday);
this.email = email;
this.rollNo = rollNo;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getRollNo() {
return rollNo;
}
public void setRollNo(String rollNo) {
this.rollNo = rollNo;
}
}
#Vehicle.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 lession3;
/**
*
* @author teacher
*/
public class Vehicle implements IRunning{
String name, manufacturer;
public Vehicle() {
}
public Vehicle(String name, String manufacturer) {
this.name = name;
this.manufacturer = manufacturer;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public void running() {
System.out.println("Vihicle is running");
}
}
#IRunning.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 lession3;
/**
*
* @author teacher
*/
public interface IRunning {
void running();
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)