By GokiSoft.com| 09:19 31/10/2020|
Java Basic

[Share Code] TÌm hiểu OOP + Interface - Lập trình Java

#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 lession2;

import java.util.Scanner;

/**
 *
 * @author student
 */
public abstract class Animal implements IRunning{
    String name, foodType;
    int age;
    float weight;

    public Animal() {
    }

    public Animal(String name, String foodType, int age, float weight) {
        this.name = name;
        this.foodType = foodType;
        this.age = age;
        this.weight = weight;
    }

    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public float getWeight() {
        return weight;
    }

    public void setWeight(float weight) {
        this.weight = weight;
    }
    
    public void running() {
        System.out.println("Animal "+name+" is running");
    }
    
    public abstract void showSound();
    
    public void input() {
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap ten: ");
        name = input.nextLine();
        
        System.out.println("Nhap loai thuc an: ");
        foodType = input.nextLine();
        
        System.out.println("Nhap tuoi: ");
        age = Integer.parseInt(input.nextLine());
        
        System.out.println("Nhap trong luong: ");
        weight = Float.parseFloat(input.nextLine());
    }
    
    public void display() {
//        System.out.println("Ten: " + name + ", tuoi: " + age + ", loai thuc an: " + foodType + ", trong luong: " + weight);
        System.out.println(this);
//        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "Animal{" + "name=" + name + ", foodType=" + foodType + ", age=" + age + ", weight=" + weight + '}';
    }
}


#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 lession2;

/**
 *
 * @author student
 */
public class Cat extends Animal{

    @Override
    public void showSound() {
        System.out.println("Meo..meo..");
    }
}


#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 lession2;

/**
 *
 * @author student
 */
public class Dog extends Animal{

    @Override
    public void showSound() {
        System.out.println("Go..go..");
    }
    
    public void testing() {
        System.out.println("Testing...");
    }
}


#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 lession2;

/**
 *
 * @author student
 */
public interface IRunning {
    void running();//public
}


#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 lession2;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author student
 */
public class Main {
    public static void main(String[] args) {
//        Dog dog = new Dog();
//        dog.input();
//        Cat cat = new Cat();
//        cat.input();
//        
//        dog.display();
//        cat.display();
        Animal obj = new Dog();
        //obj => Hieu la class Animal
        //runtime => obj => Hieu la class object Dog
        obj.showSound();
        if(obj instanceof Dog) {
            ((Dog) obj).testing();
        }
//        obj.input();
//        obj.display();

        //Tao mang quan ly Dog
        ArrayList<Dog> dogList = new ArrayList<>();
        Dog dog1 = new Dog();
        dogList.add(dog1);
        Dog dog2 = new Dog();
        dogList.add(dog2);
        Dog dog3 = new Dog();
        dogList.add(dog3);
        
        ArrayList<Cat> catList = new ArrayList<>();
        catList.add(new Cat());
        catList.add(new Cat());
        
        ArrayList<Tiger> tigerList = new ArrayList<>();
        tigerList.add(new Tiger());
        tigerList.add(new Tiger());
        tigerList.add(new Tiger());
        
        for (Tiger tiger : tigerList) {
            tiger.showSound();
        }
        
        for (Dog dog : dogList) {
            dog.showSound();
        }
        
        for (Cat cat : catList) {
            cat.showSound();
        }
        
        //tinh chat da hinh
        ArrayList<Animal> animalList = new ArrayList<>();
        animalList.add(new Dog());
        animalList.add(new Cat());
        animalList.add(new Dog());
        animalList.add(new Dog());
        animalList.add(new Tiger());
        
        for (Animal animal : animalList) {
            animal.showSound();
        }
    }
}


#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 lession2;

/**
 *
 * @author student
 */
public class Student implements IRunning{
    String name;

    public Student() {
    }

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    @Override
    public void running() {
        System.out.println("Student is running");
    }
}


#Test.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 lession2;

/**
 *
 * @author student
 */
public class Test {
    public static void main(String[] args) {
        Cat cat = new Cat();
        cat.showSound();
    }
}


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

import java.util.ArrayList;

/**
 *
 * @author student
 */
public class Test2 {
    public static void main(String[] args) {
        //Quan ly hanh dong => running cua Dog, Cat, Vehicle, Student
        ArrayList<IRunning> list = new ArrayList<>();
        
        Dog dog = new Dog();
        list.add(dog);
        
        Cat cat = new Cat();
        list.add(cat);
        
        Vehicle v = new Vehicle();
        list.add(v);
        
        Student std = new Student();
        list.add(std);
        
        for (IRunning base : list) {
            base.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 lession2;

/**
 *
 * @author student
 */
public class Tiger extends Animal{

    @Override
    public void showSound() {
        System.out.println("Humnmmm...hummmm...");
    }
    
}


#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 lession2;

/**
 *
 * @author student
 */
public class Vehicle implements IRunning {
    String name;

    public Vehicle() {
    }

    public Vehicle(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    @Override
    public void running() {
        System.out.println("Vehicle is running");
    }
}


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 đó