By GokiSoft.com|
18:28 17/04/2024|
Java Basic
[Share Code] Tìm hiểu về Class Object - C2307L
Lập trình hướng đối tượng: OOP
- Tính chất bao đóng -> Today
- Tìm hiểu tạo thuộc tính, methods
- Tìm hiểu về hàm tạo
- Tim hieu ve access properties (public, private, protected, default)
- Tính chất kế thừa -> Today
- Tính chất đa hình
- Tính chất trừu tường
Viết chương trình quản lý thông tin học sinh / sinh viên
-> Làm như thế nào???
-> B1. Xác định được đối tượng cần quản lý với học sinh / sinh viên
- Thông tin cá nhân -> Class Object -> Student
Cần quản lý thuộc tính gì???
Tên -> fullname -> String
Ngày sinh -> birthday -> String
địa chỉ -> address -> String
email ...
sđt ...
giới tính ..
Hạnh động (phương thức - method, hàm (function))
học
chạy
...
- Thiết bị học tập
Cần quản lý thuộc tính gì ???
tên thiết bị
ngày mua
giá tiền
màu sắc
Hành động là gì?
???
...
#Test.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.c2307l.lesson03;
/**
*
* @author diepvan
*/
public class Test {
public static void main(String[] args) {
//Bo comment -> Xem error xuat hien -> Hieu van de.
//Tao ra 1 doi tuong -> Viec dau tien -> cap bo nho cho doi tuong nay
Student std = new Student(); //new Student() -> Goi la ham tao (constructor)
// Student std = new Student("1231232");
//Gan du lieu
std.fullname = "TRAN VAN A";
// std.address = "HN";
std.birthday = "1990";
std.email = "a@gmail.com";
std.gender = "Male";
std.phoneNumber = "123456789";
std.running();
std.learning();
// Student std2, abc, okok;
// System.out.println("Thong tin ten: " + std.fullname + ", dia chi: " + std.address);
Student std2 = new Student("TRAN VAN A", "1990");
System.out.println("Thong tin ten: " + std2.fullname + ", ngay sinh: " + std2.birthday);
Student std3 = new Student("TRAN VAN AAAAA", "1990", "HN", "a@gmail.com", "123456789", "Male");
System.out.println("Thong tin ten: " + std3.fullname + ", ngay sinh: " + std3.birthday);
}
}
#Student.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.c2307l.lesson03;
/**
* Student -> Goi la lop doi tuong (Class Object) Cu the hoa ra: Student
* (fullname: TRAN VAN A, birthday: 1990, address: HN, ...) -> Doi tuong ->
* Object Cach su dung: Coi Student -> kieu du lieu.
*
* @author diepvan
*/
public class Student {
//Thuộc tính truy xuất dữ liệu
//Co 4 loai
// default (internal)
// public -> goi trong chinh no (Student), goi o class object khac (cung package hoac khac package) -> Dc goi o moi noi
// private -> Chi goi dc trong chinh no
// protected -> Goi trong chinh no, cung package -> Khong goi dc o package khac.
String fullname;
public String birthday;
private String address;
protected String email;
public String phoneNumber;
public String gender;
/**
* Goi la ham tao -> Trung ten voi Class Object, File name, Ko co kieu du
* lieu Goi la ham tao mac dinh -> Default constructor
*/
public Student() {
//Ko co code o day
//Sua lai ham tao mac dinh cua he thong tu tao ra
System.out.println("Testing ... constructor");
}
//Co bao nhieu ham tao
//Java -> Vo so ham tao -> Tao bao nhieu cung dc
public Student(String x) {
System.out.println("x = " + x);
}
//Co nen tao nhieu ham tao khong
// => Tuy theo yeu cau nghiep vu du an -> Y do cua lap trinh vien
//Tai sao phai tao nhieu ham tao
// => Dua theo yeu cau nghiep vu cua bai toan -> Dev tu phan tich
//Nguyen tac khi tao ham tao
// => Co -> Nen tao ham tao theo nguyen tac -> Y do
//Muc dich de lam gi???
// => Co nhieu cach khac nhau de thiet lap du lieu ban dau cho thuoc tinh
public Student(String fname, String ns) {
fullname = fname;
birthday = ns;
}
//Tuan thu nguyen tac dat ten bien -> Tham so trung ten bien (thuoc tinh)
public Student(String fullname, String birthday, String address) {
this.fullname = fullname;
this.birthday = birthday;
this.address = address;
}
//Tao ham tao tu dong
public Student(String fullname, String birthday,
String address, String email, String phoneNumber, String gender) {
this.fullname = fullname;
this.birthday = birthday;
this.address = address;
this.email = email;
this.phoneNumber = phoneNumber;
this.gender = gender;
}
public Student(String fullname, String address, String email, String phoneNumber) {
this.fullname = fullname;
this.address = address;
this.email = email;
this.phoneNumber = phoneNumber;
}
public void running() {
System.out.println("Running ...");
}
public void learning() {
System.out.println("Learning ...");
}
}
#Main.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.c2307l.lesson03;
import java.util.Scanner;
/**
*
* @author diepvan
*/
public class Main {
public static void main(String[] args) {
//Bai tap > https://gokisoft.com/test-tim-kiem-chuoi-trong-chuoi.html?his=C2307L
//B1. Khai bao bo nhap du lieu
Scanner scan = new Scanner(System.in);
//B2. Nhap du lieu dau vao
String sourceStr, searchingStr;
System.out.println("Nhap chuoi goc: ");
sourceStr = scan.nextLine();
System.out.println("Nhap chuoi tim kiem: ");
searchingStr = scan.nextLine();
//B3. Giai thuat tim kiem
//Vi du: sourceStr = a(0)a(1)a(2)a(3)b(4)b(5)a(6)a(7)a(8), searchingStr = aa
//Yeu cau 1:
//Ket qua: aa | aabb | aaa -> Tim ra dc 3 lan xuat hien -> Easy
int index = 0;
int count = 0;
while(true) {
int findingIndex = sourceStr.indexOf(searchingStr, index);
//Lan 1: findingIndex = 0
//Lan 2: findingIndex = 2
//Lan 3: findingIndex = 6
//Lan 4: findingIndex = -1 => Ko tim thay nua
// System.out.println("findingIndex = " + findingIndex);
if(findingIndex >= 0) {
count++;
System.out.println("Vi tri tim thay: " + findingIndex);
//index (1): 0 + 2 = 2
//index (2): 2 + 2 = 4
//index (3): 6 + 2 = 8
index = findingIndex + searchingStr.length();
} else {
break;
}
}
System.out.println("So lan tim thay: " + count);
//Yeu cau 2:
//Ket qua: aa | aa | aabb | aa | aa -> Tim dc 5 lan xuat hien -> Hard
index = 0;
count = 0;
while(true) {
int findingIndex = sourceStr.indexOf(searchingStr, index);
if(findingIndex >= 0) {
count++;
System.out.println("Vi tri tim thay (2): " + findingIndex);
index = findingIndex + 1;
} else {
break;
}
}
System.out.println("So lan tim thay (2): " + count);
}
}
#Test2.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.c2307l.lesson03.sub;
import com.gokisoft.c2307l.lesson03.Student;
/**
*
* @author diepvan
*/
public class Test2 {
public static void main(String[] args) {
Student std = new Student(); //new Student() -> Goi la ham tao (constructor)
//Gan du lieu
// std.fullname = "TRAN VAN A";
// std.address = "HN";
std.birthday = "1990";
// std.email = "a@gmail.com";
std.gender = "Male";
std.phoneNumber = "123456789";
std.running();
std.learning();
// System.out.println("Thong tin ten: " + std.fullname + ", dia chi: " + std.address);
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)