By GokiSoft.com| 10:24 14/07/2021|
Java Basic

[Share Code] Tìm hiểu tính chất đa hình lập trình OOP - Java căn bản - Khóa học lập trình Java

#Test01.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 lesson04;

import java.util.ArrayList;

/**
 *
 * @author Diep.Tran
 */
public class Test01 {
    public static void main(String[] args) {
        //Xay dung 1 chuong -> su dung 1 array -> quan ly tat ca cac doi tuong
        //Animal (Dog, Cat, Tiger), Student, Citizen, Car
        //Animal (Dog, Cat, Tiger)
        //Student & Citizen
        //Car
        //Quan ly dc hanh dong running() cua tat ca cac doi tuong tren
        ArrayList<IRunning> list = new ArrayList<>();
        
        IRunning r = new Dog();
        list.add(r);
        
        r = new Cat();
        list.add(r);
        
        r = new Citizen();
        list.add(r);
        
        r = new Student();
        list.add(r);
        
        r = new Car();
        list.add(r);
        
        for (IRunning rr : list) {
            rr.running();
        }
    }
}


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

import java.util.Scanner;

/**
 * Student: Child class -> lop con.
 * @author Diep.Tran
 */
public class Student extends Citizen{
    String rollno, email;

    public Student() {
    }

    public Student(String rollno, String email, String fullname, String address, String cmtnd, int age) {
        super(fullname, address, cmtnd, age);
        this.rollno = rollno;
        this.email = email;
    }

    public String getRollno() {
        return rollno;
    }

    public void setRollno(String rollno) {
        this.rollno = rollno;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
    
    @Override
    public void input() {
        super.input();
        
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap rollno: ");
        rollno = scan.nextLine();
        
        System.out.println("Nhap email: ");
        email = scan.nextLine();
    }
    
    @Override
    public void display() {
        super.display();
        System.out.printf(", rollno: %s, email: %s\n", rollno, email);
    }
    
    public void learning() {
        System.out.println(fullname + " dang hoc bai!!!");
    }
    
    public void learning(String msg) {
        System.out.println(fullname + " dang hoc bai!!! -> " + msg);
    }
    
    public void learning(String msg, String msg2) {
        System.out.println(fullname + " dang hoc bai!!! -> " + msg + " -> " + msg2);
    }
}


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

/**
 *
 * @author Diep.Tran
 */
public interface IRunning {
    void 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 lesson04;

/**
 *
 * @author Diep.Tran
 */
public class Dog extends Animal{

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


#Citizen.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 lesson04;

import java.util.Scanner;

/**
 * Citizen -> parent class & lop cha.
 * @author Diep.Tran
 */
public class Citizen implements IRunning{
    String fullname, address, cmtnd;
    int age;

    public Citizen() {
    }

    public Citizen(String fullname, String address, String cmtnd, int age) {
        this.fullname = fullname;
        this.address = address;
        this.cmtnd = cmtnd;
        this.age = age;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCmtnd() {
        return cmtnd;
    }

    public void setCmtnd(String cmtnd) {
        this.cmtnd = cmtnd;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if(age >= 0) {
            this.age = age;
        } else {
            System.out.println("Yeu cau age >= 0");
        }
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Nhap ten: ");
        fullname = scan.nextLine();
        
        System.out.println("Nhap dia chi: ");
        address = scan.nextLine();
        
        System.out.println("Nhap CMTND: ");
        cmtnd = scan.nextLine();
        
        System.out.println("Nhap tuoi: ");
        setAge(Integer.parseInt(scan.nextLine()));
    }
    
    public void display() {
        System.out.printf("\nTen: %s, age: %d, dia chi: %s, cmtnd: %s", fullname, age, address, cmtnd);
    }
    
    @Override
    public void running() {
        System.out.println("Citizen is running");
    }
}


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

/**
 *
 * @author Diep.Tran
 */
public class Cat extends Animal{

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


#Car.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 lesson04;

/**
 *
 * @author Diep.Tran
 */
public class Car implements IRunning{
    @Override
    public void running() {
        System.out.println("Car is 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 lesson04;

/**
 *
 * @author Diep.Tran
 */
public abstract class Animal implements IRunning{
    String name, color;

    public Animal() {
    }

    public Animal(String name, String color) {
        this.name = name;
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
    
    public void input() {
        System.out.println("Nhap thong tin Animal");
    }
    
    public void display() {
        System.out.println("Hien thi thong tin Animal");
    }
    
    public abstract void showSound();
    
    @Override
    public void running() {
        System.out.println("Animal 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 đó