By GokiSoft.com|
16:14 19/09/2022|
Java Basic
[Source Code] Tìm hiểu Interface & Abstract Class trong Java - C2109I
#Animal.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.lesson05;
import java.util.Scanner;
/**
*
* @author diepvan
*/
public abstract class Animal implements ISound{
String name, foodType;
public Animal() {
}
public Animal(String name, String foodType) {
this.name = name;
this.foodType = foodType;
}
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;
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ten: ");
name = scan.nextLine();
System.out.println("Nhap thuc an: ");
foodType = scan.nextLine();
}
@Override
public String toString() {
return "Animal{" + "name=" + name + ", foodType=" + foodType + '}';
}
public void display() {
System.out.println(this);
}
public abstract void showSound();
}
#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.lesson05;
/**
*
* @author diepvan
*/
public class Car implements ISound{
@Override
public void showSound() {
System.out.println("... xin chao ...");
}
}
#Cat.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.lesson05;
/**
*
* @author diepvan
*/
public class Cat extends Animal{
@Override
public void showSound() {
System.out.println("Meo .. meo ...");
}
}
#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.lesson05;
/**
*
* @author diepvan
*/
public class Dog extends Animal{
@Override
public void showSound() {
System.out.println("Go .. go ..");
}
}
#ISound.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.lesson05;
/**
*
* @author diepvan
*/
public interface ISound {
void showSound();
}
#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.lesson05;
/**
*
* @author diepvan
*/
public class Main {
public static void main(String[] args) {
//T/c truu tuong
Dog dog = new Dog();
dog.showSound();
Cat cat = new Cat();
cat.showSound();
Tiger t = new Tiger();
t.showSound();
}
}
#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.lesson05;
/**
*
* @author diepvan
*/
public class Student implements ISound{
@Override
public void showSound() {
System.out.println("... abc ...");
}
}
#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.lesson05;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author diepvan
*/
public class Test {
public static void main(String[] args) {
List<Animal> animals = new ArrayList<>();
animals.add(new Cat());
animals.add(new Dog());
animals.add(new Tiger());
for (Animal animal : animals) {
animal.showSound();
}
//Cach nao su dung 1 mang -> Quan ly dc tat ca cac doi tuong Animal, Cat, Dog, Tiger, Student, Car
//Interface:
List<ISound> list = new ArrayList<>();
list.add(new Cat());
list.add(new Tiger());
list.add(new Student());
for (ISound iSound : list) {
iSound.showSound();
}
}
}
#Tiger.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.lesson05;
/**
*
* @author diepvan
*/
public class Tiger extends Animal{
@Override
public void showSound() {
System.out.println("Tiger .. tiger ...");
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)