By GokiSoft.com|
10:01 12/07/2021|
Java Basic
[Share Code] Tìm hiểu tính chất kế thừa và tính chất đa hình trong lập trình Java - Khoá học Java
- Tính chất trong lập trình hướng đối tượng (OOP)
- Tính chất đóng gói => OK
- Tính kế thừa => OK
- override
- overloading
- Tính đa hình
- Tính trừu tượng
- Interface
======================================================
Mini Project:
Viết chương trình quản lý công dan + sinh viên + xe cộ + động vật trong thành phố.
Yêu cầu:
1) Xây dựng lớp đối tượng công dân + sinh viên
- Citizen:
- Thuộc tính: Tên, tuổi, địa chỉ, cmtnd
- Hành động:
- input & display -> Nhập & hiển thị thông tin
- running
- Student
- Thuộc tính: Tên, tuổi, địa chỉ, cmtnd, rollno, email
- Hành động:
- input & display -> Nhập & hiển thị thông tin
- running
- learning
2) Triển khai dự án
#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 lesson04;
/**
*
* @author Diep.Tran
*/
public class Test {
public static void main(String[] args) {
Dog dog = new Dog();
dog.showSound();
Cat cat = new Cat();
cat.showSound();
//Xay dung chuong trinh -> Su dung 1 list quan ly toan bo doi tuong Car, Citizen, Student, Dog, Cat...
//Quan ly hanh dong running() cho class object nay.
//Giai phap la gi???
}
}
#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);
}
}
#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 lesson04;
import java.util.ArrayList;
/**
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) {
// Student std = new Student();
// std.input();
//
// std.display();
// std.learning();
//
// std.learning("12345");
//Tinh chat da hinh
// Citizen citizen = new Citizen();
// citizen.input();
// citizen.display();
// Citizen c = new Student();//Goi la tinh chat da hinh.
// c.input();
// c.display();
//
// if(c instanceof Student) {
// ((Student) c).learning();
// }
//Ung dung trong lap trinh su dung tinh chat da hinh
ArrayList<Citizen> list = new ArrayList<>();
// ArrayList<Student> list2 = new ArrayList<>();
//Quan ly nhap & hien thong tin du lieu cua Citizen va Student
//Cach thong thuong -> su dung -> 2 Array de quan ly du lieu cua Citizen va Student
//Co cach khac -> su dung chi 1 Array de quan ly ca Citizen & Student hay ko
Citizen ci = new Citizen();
list.add(ci);
ci = new Student();
list.add(ci);
for (Citizen citizen : list) {
citizen.input();
}
for (Citizen citizen : list) {
citizen.display();
}
}
}
#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 {
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);
}
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 {
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 {
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();
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)