By GokiSoft.com| 15:17 29/01/2021|
Java Basic

[Share Code] Tìm hiểu lập trình OOP - Tính kế thừa - Lập trình Java Basic

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

/**
 *
 * @author Diep.Tran
 */
public class Student extends People{
    String rollno;

    public Student() {
        System.out.println("Student init");
    }

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

    @Override
    public void running() {
        super.running();
        System.out.println("Xin chao, toi la sinh vien!!!");
    }
    
    public void learning() {
        System.out.println("Learning ...");
    }
    
    public void learning(String s1) {
        System.out.println("Dang hoc ... " + s1);
    }
    
    public void learning(String s1, String s2) {
        System.out.println("Dang hoc ... " + s1 + ", " + s2);
    }
}


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

/**
 *
 * @author Diep.Tran
 */
public class People {
    String name, address;
    int age;

    public People() {
        System.out.println("People init");
    }

    public People(String name, String address, int age) {
        this.name = name;
        this.address = address;
        this.age = age;
    }
    
    public void running() {
        System.out.println("People is running");
    }
}


#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.lesson04_1;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        //Viet chuong trinh quan ly 2 object: People + Student
//        People people = new People();
//        people.running();
        
        Student student = new Student();
        student.running();
//        student.learning();
        student.learning("123");
        student.learning("1", "2");
    }
}


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