By GokiSoft.com| 15:20 25/01/2021|
Java Basic

[Share Code] Tìm hiểu Class Object - public private protected - hàm tạo - Lập trình Java


OOP:
	- Lập trình hướng đối tượng là gì: ???
	- Tính chất trong lập trình OOP
		- Tính chất bao đóng: ???
		- Tính chất kế thừa: ???
		- Tính đa hình 
		- Tính trừu tượng
================================================
Bài toán:
- Xây dựng dự án quản lý sở thú: Quản lý động vật trong sở thú
1) Tiger => Class (class object) => Tiger
	- Thuộc tính của tiger:
		- Tên
		- Age
		- foodType
	- Hành động của tiger
		- Ăn
		- Chạy
2) Monkey
	- Thuộc tính của Monkey
		- Tên
		- Age
		- foodType
	- Hành động của monkey
		- Leo
		- Ăn
Làm sao để có thể biểu diễn đc yêu cầu này trong lập trình.

- Dac tinh truy xuat
	- public
	- private
	- protected




#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 aptech.lesson03;

/**
 *
 * @author Diep.Tran
 * Goila: Class Object
 */
public class Tiger {
    //Dinh nghia cac thuoc tinh
    public String name;
    private int age;
    private String foodType;
    String area;//friendly => Ke thua.
    
    /**
     * Ham tao
     */
    public Tiger() {
        System.out.println("Ham tao khong doi so.");
        //Khoi tao gia tri cho thuoc tinh -> Tuy thuoc vao business logic.
    }
    
    /**
     * Overloading...
     * @param name1
     * @param age1 
     */
    public Tiger(String name1, int age1) {
        name = name1;
        age = age1;
    }
    
    public Tiger(String name, int age, String foodType) {
        this.name = name;
        this.age = age;
        this.foodType = foodType;
    }
    
    /**
     * Function, chuc nang, method, phuong thuc
     */
    public void running() {
        System.out.println("Tiger is running");
        System.out.println("Tiger is running");
        System.out.println("Tiger is running");
    }
    
    public void eating() {
        System.out.println("Tiger is eating");
        System.out.println("Tiger is eating");
        System.out.println("Tiger is eating");
    }

    //getter
    public int getAge() {
        return age;
    }

    //setter
    public void setAge(int age) {
        if(age > 0) {
            this.age = age;
        } else {
            System.err.println("Yeu cau age > 0");
        }
    }
    
    public String getName() {
        return name;
    }
    
    public void display() {
        System.out.format("\nName = %s, age = %d, foodType = %s\n", name, age, foodType);
    }
}


#Monkey.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 aptech.lesson03;

/**
 *
 * @author Diep.Tran
 */
public class Monkey {
    //Dinh nghia cac thuoc tinh
    public String name;
    public int age;
    public String foodType;
    
    /**
     * Function, chuc nang, method, phuong thuc
     */
    public void leo() {
        System.out.println("Monkey is running");
        System.out.println("Monkey is running");
        System.out.println("Monkey is running");
    }
    
    public void eating() {
        System.out.println("Monkey is eating");
        System.out.println("Monkey is eating");
        System.out.println("Monkey is eating");
    }
}


#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 aptech.lesson03;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        //Mo ta 1 tiger cu the: ABC, 2, Dog
        //new Tiger() -> Ham tao -> Khoi tao khong gian luu tru cho doi tuong tiger dc tao ra.
//        Tiger tiger = new Tiger();
//        tiger.name = "ABC";
//        tiger.age = 2;
//        tiger.foodType = "Dog";
//        
//        tiger.running();
//        tiger.display();
//        
//        Tiger tiger2 = new Tiger();
//        tiger2.name = "BBB";
//        tiger2.age = 3;
//        tiger2.foodType = "Dog12";
//        
//        tiger2.running();
//        tiger2.display();
//        
//        Tiger tiger3 = new Tiger("AAAA", 12);
//        tiger3.foodType = "OKOK";
//        tiger3.display();
//        tiger3.display();
//        tiger3.display();
//        tiger3.display();
//        tiger3.display();
//        
//        //So sanh tong cua 2 thang: 2 + 11 va 3 + 10
//        Calculator c1 = new Calculator(2, 11);
//        float t1 = c1.cong();
//        
//        Calculator c2 = new Calculator(3, 10);
//        float t2 = c2.cong();
//        
//        if(t1 > t2) {
//            System.out.println("Tong 2 + 11 > 3 + 10");
//        } else if(t1 < t2) {
//            System.out.println("Tong 2 + 11 < 3 + 10");
//        } else {
//            System.out.println("Tong 2 + 11 = 3 + 10");
//        }

        Tiger tiger = new Tiger();
        tiger.name = "ABC";
//        tiger.age = -2;
        tiger.setAge(-2);
//        tiger.foodType = "Dog";
        
        tiger.display();
    }
}


#Calculator.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 aptech.lesson03;

/**
 *
 * @author Diep.Tran
 */
public class Calculator {
    public float x, y;
    
    public Calculator(float x1, float y1) {
        x = x1;
        y = y1;
    }
    
    public float cong() {
        return x + y;
    }
}


#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 aptech.lesson03_1;

import aptech.lesson03.Tiger;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        Tiger tiger = new Tiger();
        tiger.name = "ABC";
//        tiger.age = 2;
//        tiger.foodType = "Dog";
        int age = tiger.getAge();
        System.out.println("Age: " + age);

        tiger.display();
    }
}


Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)