-
07-15-2013, 07:00 AM #1Junior Member
- Ngày tham gia
- Apr 2014
- Bài viết
- 0
Bài 16: Tính kế thừa (inheritance) trong Java
/* bài viết thuộc loạt bài hướng dẫn trong "khóa học lập trình java miễn phí"1, khái niệm:
trên diễn đàn android.vn, bạn có thể vào đây đọc
để hiểu hơn về khóa học và tham gia ngay cùng mọi người */
android.vn/attachments/20111012_java-ltrinhhuongdoituong-p3_11-gif.17727/" border="0" alt="" />
tính kế thừa trong java cũng tương tự như mô hình trên!
một lớp con (subclass) có thể kế thừa tất cả những vùng dữ liệu và phương thức của một lớp khác (siêu lớp - superclass).
như vậy việc tạo một lớp mới từ một lớp đã biết sao cho các thành phần (fields và methods) của lớp cũ cũng sẽ thành các thành phần (fields và methods) của lớp mới. khi đó ta gọi lớpmới là lớp dẫn xuất (derived class) từ lớp cũ (superclass).
có thể lớp cũ cũng là lớp được dẫn xuất từ một lớp nào đấy, nhưng đối với lớp mới vừa tạo thì lớp cũ đó là một lớp siêu lớp trực tiếp (immediate supperclass).
dùng từ khóa extends để chỉ lớp dẫn xuất.
Mã nguồn PHP:[color=#000000]
[/color][color=#007700]class [/color][color=#0000bb]a [/color][color=#007700]{ [/color][color=#ff8000]//...[/color][color=#007700]}class [/color][color=#0000bb]b [/color][color=#007700]extends [/color][color=#0000bb]a[/color][color=#007700]{ [/color][color=#ff8000]//...[/color][color=#007700]}[/color]
** chú ý: java cung cấp 3 tiền tố/từ khóa để hỗ trợ tính kế thừa của lớp:
- public: lớp có thể truy cập từ các gói, chương trình khác.
- final: lớp hằng, lớp không thể tạo dẫn xuất (không thể có con), hay đôi khi người ta gọi là lớp “vô sinh”.
- abstract: lớp trừu tượng (không có khai báo các thành
phần và các phương thức trong lớp trừu tượng). lớp dẫn xuất sẽ khai báo, cài đặt cụ thể các thuộc tính, phương thức của lớp trừu tượng.
(những điều này ta sẽ bàn đến ở những bài sau)
2, ví dụ:
tạo 3 class gồm các thuộc tính tương ứng:
class "nhân sự" gồm: họ tên, năm sinh, quê quán.
class "học sinh" gồm: họ tên, năm sinh, quê quán, điểm trung bình.
class "giáo viên" gồm: họ tên, năm sinh, quê quán, lương hàng tháng.
như vậy, nếu như chúng ta không sử dụng tính kế thừa thì sẽ xảy ra tình trạng lặp code, nghĩa là khi khai báo 2 lớp "học sinh" và "giáo viên" sẽ khai báo lại các thuộc tính họ tên, năm sinh, quê quán
để sử dụng tính kế thừa, ta chỉ cần khai báo lớp "nhân sự", sau đó 2 lớp còn lại ta kế thừa từ lớp "nhân sự" , thiếu thuộc tính, phương thức nào thì sẽ bổ sung vào lớp đó.
xem bài làm dưới đây:
a, tạo các thuộc tính để public, và kế thừa: (để thuộc tính public như này cho ngắn gọn, các bạn dễ hiểu trước)
Mã nguồn PHP:[color=#000000]
package javademoandroidvn[/color][color=#007700]; class [/color][color=#0000bb]nhansu [/color][color=#007700]{ public [/color][color=#0000bb]string hoten[/color][color=#007700]; public [/color][color=#0000bb]int namsinh[/color][color=#007700]; public [/color][color=#0000bb]string quequan[/color][color=#007700];}class [/color][color=#0000bb]hocsinh [/color][color=#007700]extends [/color][color=#0000bb]nhansu[/color][color=#007700]{ public [/color][color=#0000bb]float diemtb[/color][color=#007700];}class [/color][color=#0000bb]giaovien [/color][color=#007700]extends [/color][color=#0000bb]nhansu[/color][color=#007700]{ public [/color][color=#0000bb]int luong[/color][color=#007700];} public class [/color][color=#0000bb]javademoandroidvn [/color][color=#007700]{ public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string[/color][color=#007700][] [/color][color=#0000bb]args[/color][color=#007700]) { [/color][color=#0000bb]nhansu ns [/color][color=#007700]= new [/color][color=#0000bb]nhansu[/color][color=#007700](); [/color][color=#0000bb]ns[/color][color=#007700].[/color][color=#0000bb]hoten [/color][color=#007700]= [/color][color=#dd0000]"nhân sự a"[/color][color=#007700]; [/color][color=#0000bb]ns[/color][color=#007700].[/color][color=#0000bb]namsinh [/color][color=#007700]= [/color][color=#0000bb]1990[/color][color=#007700]; [/color][color=#0000bb]ns[/color][color=#007700].[/color][color=#0000bb]quequan [/color][color=#007700]= [/color][color=#dd0000]"hải dương"[/color][color=#007700]; [/color][color=#0000bb]hocsinh hs [/color][color=#007700]= new [/color][color=#0000bb]hocsinh[/color][color=#007700](); [/color][color=#0000bb]hs[/color][color=#007700].[/color][color=#0000bb]hoten [/color][color=#007700]= [/color][color=#dd0000]"vũ văn a"[/color][color=#007700]; [/color][color=#ff8000]// đối tượng tạo ra từ lớp "học sinh" kế thừa, sử dụng các thuộc tính của lớp "nhân sự" [/color][color=#0000bb]hs[/color][color=#007700].[/color][color=#0000bb]namsinh [/color][color=#007700]= [/color][color=#0000bb]1992[/color][color=#007700]; [/color][color=#0000bb]hs[/color][color=#007700].[/color][color=#0000bb]quequan [/color][color=#007700]= [/color][color=#dd0000]"hải dương"[/color][color=#007700]; [/color][color=#0000bb]hs[/color][color=#007700].[/color][color=#0000bb]diemtb [/color][color=#007700]= [/color][color=#0000bb]9.5f[/color][color=#007700]; [/color][color=#0000bb]giaovien gv [/color][color=#007700]= new [/color][color=#0000bb]giaovien[/color][color=#007700](); [/color][color=#0000bb]gv[/color][color=#007700].[/color][color=#0000bb]hoten [/color][color=#007700]= [/color][color=#dd0000]"nguyễn văn b"[/color][color=#007700]; [/color][color=#ff8000]// đối tượng tạo ra từ lớp "giáo viên" kế thừa, sử dụng các thuộc tính của lớp "nhân sự" [/color][color=#0000bb]gv[/color][color=#007700].[/color][color=#0000bb]namsinh [/color][color=#007700]= [/color][color=#0000bb]1980[/color][color=#007700]; [/color][color=#0000bb]gv[/color][color=#007700].[/color][color=#0000bb]quequan [/color][color=#007700]= [/color][color=#dd0000]"hà nội"[/color][color=#007700]; [/color][color=#0000bb]gv[/color][color=#007700].[/color][color=#0000bb]luong [/color][color=#007700]= [/color][color=#0000bb]300000000[/color][color=#007700]; }}[/color]
(cụ thể là các phương thức getter và setter ở ví dụ này)
Mã nguồn PHP:[color=#000000]
package javademoandroidvn[/color][color=#007700]; class [/color][color=#0000bb]nhansu [/color][color=#007700]{ private [/color][color=#0000bb]int namsinh[/color][color=#007700]; private [/color][color=#0000bb]string quequan[/color][color=#007700]; private [/color][color=#0000bb]string hoten[/color][color=#007700]; public [/color][color=#0000bb]string gethoten[/color][color=#007700]() { return [/color][color=#0000bb]hoten[/color][color=#007700]; } public [/color][color=#0000bb]void sethoten[/color][color=#007700]([/color][color=#0000bb]string hoten[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]hoten [/color][color=#007700]= [/color][color=#0000bb]hoten[/color][color=#007700]; } public [/color][color=#0000bb]int getnamsinh[/color][color=#007700]() { return [/color][color=#0000bb]namsinh[/color][color=#007700]; } public [/color][color=#0000bb]void setnamsinh[/color][color=#007700]([/color][color=#0000bb]int namsinh[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]namsinh [/color][color=#007700]= [/color][color=#0000bb]namsinh[/color][color=#007700]; } public [/color][color=#0000bb]string getquequan[/color][color=#007700]() { return [/color][color=#0000bb]quequan[/color][color=#007700]; } public [/color][color=#0000bb]void setquequan[/color][color=#007700]([/color][color=#0000bb]string quequan[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]quequan [/color][color=#007700]= [/color][color=#0000bb]quequan[/color][color=#007700]; }} class [/color][color=#0000bb]hocsinh [/color][color=#007700]extends [/color][color=#0000bb]nhansu [/color][color=#007700]{ private [/color][color=#0000bb]float diemtb[/color][color=#007700]; public [/color][color=#0000bb]float getdiemtb[/color][color=#007700]() { return [/color][color=#0000bb]diemtb[/color][color=#007700]; } public [/color][color=#0000bb]void setdiemtb[/color][color=#007700]([/color][color=#0000bb]float diemtb[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]diemtb [/color][color=#007700]= [/color][color=#0000bb]diemtb[/color][color=#007700]; }} public class [/color][color=#0000bb]javademoandroidvn [/color][color=#007700]{ public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string[/color][color=#007700][] [/color][color=#0000bb]args[/color][color=#007700]) { [/color][color=#0000bb]nhansu ns [/color][color=#007700]= new [/color][color=#0000bb]nhansu[/color][color=#007700](); [/color][color=#0000bb]ns[/color][color=#007700].[/color][color=#0000bb]sethoten[/color][color=#007700]([/color][color=#dd0000]"nhân sự a"[/color][color=#007700]); [/color][color=#0000bb]ns[/color][color=#007700].[/color][color=#0000bb]setnamsinh[/color][color=#007700]([/color][color=#0000bb]1990[/color][color=#007700]); [/color][color=#0000bb]ns[/color][color=#007700].[/color][color=#0000bb]setquequan[/color][color=#007700]([/color][color=#dd0000]"hải dương"[/color][color=#007700]); [/color][color=#0000bb]hocsinh hs [/color][color=#007700]= new [/color][color=#0000bb]hocsinh[/color][color=#007700](); [/color][color=#0000bb]hs[/color][color=#007700].[/color][color=#0000bb]sethoten[/color][color=#007700]([/color][color=#dd0000]"vũ văn a"[/color][color=#007700]); [/color][color=#ff8000]// đối tượng tạo ra từ lớp "học sinh" kế thừa, sử dụng các thuộc tính, phương thức của lớp "nhân sự" [/color][color=#0000bb]hs[/color][color=#007700].[/color][color=#0000bb]setnamsinh[/color][color=#007700]([/color][color=#0000bb]1992[/color][color=#007700]); [/color][color=#0000bb]hs[/color][color=#007700].[/color][color=#0000bb]setquequan[/color][color=#007700]([/color][color=#dd0000]"hải dương"[/color][color=#007700]); [/color][color=#0000bb]hs[/color][color=#007700].[/color][color=#0000bb]setdiemtb[/color][color=#007700]([/color][color=#0000bb]9.5f[/color][color=#007700]); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"thông tin học học sinh: "[/color][color=#007700]); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"họ tên: " [/color][color=#007700]+ [/color][color=#0000bb]hs[/color][color=#007700].[/color][color=#0000bb]gethoten[/color][color=#007700]()); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"năm sinh: " [/color][color=#007700]+ [/color][color=#0000bb]hs[/color][color=#007700].[/color][color=#0000bb]getnamsinh[/color][color=#007700]()); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"quê quán: " [/color][color=#007700]+ [/color][color=#0000bb]hs[/color][color=#007700].[/color][color=#0000bb]getquequan[/color][color=#007700]()); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"điểm trung bình: " [/color][color=#007700]+ [/color][color=#0000bb]hs[/color][color=#007700].[/color][color=#0000bb]getdiemtb[/color][color=#007700]()); }}[/color]
tham khảo thêm video bên blog studyandshare, có thể có 1 vài khái niệm bạn sẽ chưa hiểu, sau vài bài nữa, bạn nên xem lại video này!bài tập về nhà::d
bài 1: dùng tính kế thừa khai báo 3 ;sinh viên" gồm các thuộc tính: họ tên, năm sinh, mã thẻ, tiền học phí còn nợ.
class "giảng viên" gồm các thuộc tính: họ tên, năm sinh, mã thẻ, tiền lương hàng tháng.
class "giám đốc" gồm các thuộc tính: họ tên, năm sinh, mã thẻ, tiền tiêu hàng tháng.
các thuộc tính để private, truy cập các thuộc tính sử dụng phương thức getter và setter
tạo 1 đối tượng giảng viên, giá trị nhập vào từ bàn phím, in thông tin ra ngoài
màn hình console
đọc thêm: tính kế thừa và tính trừu tượng trong java
(chú ý: các bạn nên tự giác làm bài tập, hoàn thiện rồi đăng lên, các bài bạn đã làm, nên để code trong thẻ code - chọn thẻ php cho rõ ràng và để ẩn code trong thẻ ẩn spoiler, để khi click vào mới hiện code lên , trước khi thử sức tự làm, bạn không nên nhìn code của các bạn khác đã đăng nhé)
my facebook: https://www.facebook.com/tiasangmoi
bài viết đăng lên diễn đàn khác vui lòng ghi rõ nguồn: android.vn
-
07-16-2013, 07:00 AM #2Junior Member
- Ngày tham gia
- Mar 2015
- Bài viết
- 0
<div class="bbcodeblock bbcodequote bbcmspoilerblock">
<div class="attribution type"><noscript>spoiler (move your mouse to the spoiler area to reveal the content)</noscript>
show spoiler
hide spoiler
</div>
<div class="quotecontent">
<div class="bbcm_spoiler_noscript"><blockquote>
Mã nguồn PHP:[color=#000000]
package demojava[/color][color=#007700].[/color][color=#0000bb]android[/color][color=#007700].[/color][color=#0000bb]vn[/color][color=#007700]; [/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]scanner[/color][color=#007700]; class [/color][color=#0000bb]thongtin [/color][color=#007700]{ private [/color][color=#0000bb]string hoten[/color][color=#007700]; private [/color][color=#0000bb]int namsinh[/color][color=#007700]; private [/color][color=#0000bb]string mathe[/color][color=#007700]; public [/color][color=#0000bb]void sethoten[/color][color=#007700]([/color][color=#0000bb]string hoten[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]hoten [/color][color=#007700]= [/color][color=#0000bb]hoten[/color][color=#007700]; } public [/color][color=#0000bb]string gethoten[/color][color=#007700]() { return [/color][color=#0000bb]hoten[/color][color=#007700]; } public [/color][color=#0000bb]int getnamsinh[/color][color=#007700]() { return [/color][color=#0000bb]namsinh[/color][color=#007700]; } public [/color][color=#0000bb]void setnamsinh[/color][color=#007700]([/color][color=#0000bb]int namsinh[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]namsinh [/color][color=#007700]= [/color][color=#0000bb]namsinh[/color][color=#007700]; } public [/color][color=#0000bb]string getmathe[/color][color=#007700]() { return [/color][color=#0000bb]mathe[/color][color=#007700]; } public [/color][color=#0000bb]void setmathe[/color][color=#007700]([/color][color=#0000bb]string mathe[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]mathe [/color][color=#007700]= [/color][color=#0000bb]mathe[/color][color=#007700]; } public [/color][color=#0000bb]void input[/color][color=#007700](){ [/color][color=#0000bb]scanner inp [/color][color=#007700]= new [/color][color=#0000bb]scanner[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/color][color=#007700]); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"nhập họ tên: "[/color][color=#007700]); [/color][color=#0000bb]sethoten[/color][color=#007700]([/color][color=#0000bb]inp[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700]()); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"nhập năm sinh: "[/color][color=#007700]); [/color][color=#0000bb]setnamsinh[/color][color=#007700]([/color][color=#0000bb]inp[/color][color=#007700].[/color][color=#0000bb]nextint[/color][color=#007700]()); [/color][color=#0000bb]inp[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"nhập mã thẻ: "[/color][color=#007700]); [/color][color=#0000bb]setmathe[/color][color=#007700]([/color][color=#0000bb]inp[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700]()); } public [/color][color=#0000bb]void output[/color][color=#007700]() { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"họ tên: " [/color][color=#007700]+ [/color][color=#0000bb]gethoten[/color][color=#007700]()); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"năm sinh: " [/color][color=#007700]+ [/color][color=#0000bb]getnamsinh[/color][color=#007700]()); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"mã thẻ: " [/color][color=#007700]+ [/color][color=#0000bb]getmathe[/color][color=#007700]()); }} class [/color][color=#0000bb]sinhvien [/color][color=#007700]extends [/color][color=#0000bb]thongtin [/color][color=#007700]{ private [/color][color=#0000bb]long hocphi[/color][color=#007700]; public [/color][color=#0000bb]long gethocphi[/color][color=#007700]() { return [/color][color=#0000bb]hocphi[/color][color=#007700]; } public [/color][color=#0000bb]void sethocphi[/color][color=#007700]([/color][color=#0000bb]long hocphi[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]hocphi [/color][color=#007700]= [/color][color=#0000bb]hocphi[/color][color=#007700]; }} class [/color][color=#0000bb]giangvien [/color][color=#007700]extends [/color][color=#0000bb]thongtin [/color][color=#007700]{ private [/color][color=#0000bb]long tienluong[/color][color=#007700]; public [/color][color=#0000bb]long gettienluong[/color][color=#007700]() { return [/color][color=#0000bb]tienluong[/color][color=#007700]; } public [/color][color=#0000bb]void settienluong[/color][color=#007700]([/color][color=#0000bb]long tienluong[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]tienluong [/color][color=#007700]= [/color][color=#0000bb]tienluong[/color][color=#007700]; } public [/color][color=#0000bb]void inputgiangvien[/color][color=#007700]() { [/color][color=#0000bb]scanner inp [/color][color=#007700]= new [/color][color=#0000bb]scanner[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/color][color=#007700]); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"tiền lương hàng tháng: "[/color][color=#007700]); [/color][color=#0000bb]settienluong[/color][color=#007700]([/color][color=#0000bb]inp[/color][color=#007700].[/color][color=#0000bb]nextlong[/color][color=#007700]()); } public [/color][color=#0000bb]void outputgiangvien[/color][color=#007700]() { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"tiền lương hàng tháng: "[/color][color=#007700]+[/color][color=#0000bb]gettienluong[/color][color=#007700]()); }} class [/color][color=#0000bb]giamdoc [/color][color=#007700]extends [/color][color=#0000bb]thongtin [/color][color=#007700]{ private [/color][color=#0000bb]long tientieu[/color][color=#007700]; public [/color][color=#0000bb]long gettientieu[/color][color=#007700]() { return [/color][color=#0000bb]tientieu[/color][color=#007700]; } public [/color][color=#0000bb]void settientieu[/color][color=#007700]([/color][color=#0000bb]long tientieu[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]tientieu [/color][color=#007700]= [/color][color=#0000bb]tientieu[/color][color=#007700]; }} public class [/color][color=#0000bb]unit16 [/color][color=#007700]{ public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string[/color][color=#007700][] [/color][color=#0000bb]args[/color][color=#007700]) { [/color][color=#0000bb]thongtin ttin [/color][color=#007700]= new [/color][color=#0000bb]thongtin[/color][color=#007700](); [/color][color=#0000bb]giangvien gvien [/color][color=#007700]= new [/color][color=#0000bb]giangvien[/color][color=#007700](); [/color][color=#ff8000]//input [/color][color=#0000bb]ttin[/color][color=#007700].[/color][color=#0000bb]input[/color][color=#007700](); [/color][color=#0000bb]gvien[/color][color=#007700].[/color][color=#0000bb]inputgiangvien[/color][color=#007700](); [/color][color=#ff8000]//output [/color][color=#0000bb]ttin[/color][color=#007700].[/color][color=#0000bb]output[/color][color=#007700](); [/color][color=#0000bb]gvien[/color][color=#007700].[/color][color=#0000bb]inputgiangvien[/color][color=#007700](); }}[/color]
</div>
</div>
-
07-17-2013, 07:00 AM #3Junior Member
- Ngày tham gia
- Mar 2015
- Bài viết
- 0
mình bổ sung thêm chút nhé, đã update lại ở trên, trước viết thiếu chút
** java cung cấp 3 tiền tố/từ khóa để hỗ trợ tính kế thừa của lớp:
- public: lớp có thể truy cập từ các gói, chương trình khác.
- final: lớp hằng, lớp không thể tạo dẫn xuất (không thể có con), hay đôi khi người ta gọi là lớp “vô sinh”.
- abstract: lớp trừu tượng (không có khai báo các thành
phần và các phương thức trong lớp trừu tượng). lớp dẫn xuất sẽ khai báo, cài đặt cụ thể các thuộc tính, phương
thức của lớp trừu tượng.
-
07-20-2013, 07:00 AM #4Junior Member
- Ngày tham gia
- Feb 2015
- Bài viết
- 0
có tham khảo bài anh vjrus.ht
<div class="bbcodeblock bbcodequote bbcmspoilerblock">
<div class="attribution type"><noscript>spoiler (move your mouse to the spoiler area to reveal the content)</noscript>
show spoiler
hide spoiler
</div>
<div class="quotecontent">
<div class="bbcm_spoiler_noscript"><blockquote>
Mã nguồn PHP:[color=#000000]
package unit16_kethua[/color][color=#007700]; [/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]scanner[/color][color=#007700]; class [/color][color=#0000bb]chuan [/color][color=#007700]{ private [/color][color=#0000bb]string hoten[/color][color=#007700], [/color][color=#0000bb]mathe[/color][color=#007700]; private [/color][color=#0000bb]long namsinh[/color][color=#007700]; public [/color][color=#0000bb]string gethoten[/color][color=#007700]() { return [/color][color=#0000bb]hoten[/color][color=#007700]; } public [/color][color=#0000bb]void sethoten[/color][color=#007700]([/color][color=#0000bb]string hoten[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]hoten [/color][color=#007700]= [/color][color=#0000bb]hoten[/color][color=#007700]; } public [/color][color=#0000bb]string getmathe[/color][color=#007700]() { return [/color][color=#0000bb]mathe[/color][color=#007700]; } public [/color][color=#0000bb]void setmathe[/color][color=#007700]([/color][color=#0000bb]string mathe[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]mathe [/color][color=#007700]= [/color][color=#0000bb]mathe[/color][color=#007700]; } public [/color][color=#0000bb]long getnamsinh[/color][color=#007700]() { return [/color][color=#0000bb]namsinh[/color][color=#007700]; } public [/color][color=#0000bb]void setnamsinh[/color][color=#007700]([/color][color=#0000bb]long namsinh[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]namsinh [/color][color=#007700]= [/color][color=#0000bb]namsinh[/color][color=#007700]; } public [/color][color=#0000bb]void input[/color][color=#007700]() { [/color][color=#0000bb]scanner nhapdulieu [/color][color=#007700]= new [/color][color=#0000bb]scanner[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/color][color=#007700]); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"nhập họ tên: "[/color][color=#007700]); [/color][color=#0000bb]sethoten[/color][color=#007700]([/color][color=#0000bb]nhapdulieu[/color][color=#007700].[/color][color=#0000bb]next[/color][color=#007700]()); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"nhập năm sinh: "[/color][color=#007700]); [/color][color=#0000bb]setnamsinh[/color][color=#007700]([/color][color=#0000bb]nhapdulieu[/color][color=#007700].[/color][color=#0000bb]nextlong[/color][color=#007700]()); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"nhập mã thẻ: "[/color][color=#007700]); [/color][color=#0000bb]setmathe[/color][color=#007700]([/color][color=#0000bb]nhapdulieu[/color][color=#007700].[/color][color=#0000bb]next[/color][color=#007700]()); } public [/color][color=#0000bb]void output[/color][color=#007700]() { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"họ tên: " [/color][color=#007700]+ [/color][color=#0000bb]hoten[/color][color=#007700]); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"năm sinh: " [/color][color=#007700]+ [/color][color=#0000bb]namsinh[/color][color=#007700]); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"mã thẻ: " [/color][color=#007700]+ [/color][color=#0000bb]mathe[/color][color=#007700]); }} class [/color][color=#0000bb]sinhvien [/color][color=#007700]extends [/color][color=#0000bb]chuan [/color][color=#007700]{ private [/color][color=#0000bb]long nohocphi[/color][color=#007700]; public [/color][color=#0000bb]long getnohocphi[/color][color=#007700]() { return [/color][color=#0000bb]nohocphi[/color][color=#007700]; } public [/color][color=#0000bb]void setnohocphi[/color][color=#007700]([/color][color=#0000bb]long nohocphi[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]nohocphi [/color][color=#007700]= [/color][color=#0000bb]nohocphi[/color][color=#007700]; }} class [/color][color=#0000bb]giangvien [/color][color=#007700]extends [/color][color=#0000bb]chuan [/color][color=#007700]{ private [/color][color=#0000bb]long luong[/color][color=#007700]; public [/color][color=#0000bb]long getluong[/color][color=#007700]() { return [/color][color=#0000bb]luong[/color][color=#007700]; } public [/color][color=#0000bb]void setluong[/color][color=#007700]([/color][color=#0000bb]long luong[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]luong [/color][color=#007700]= [/color][color=#0000bb]luong[/color][color=#007700]; } public [/color][color=#0000bb]void inputgiangvien[/color][color=#007700]() { [/color][color=#0000bb]scanner nhapdulieu2 [/color][color=#007700]= new [/color][color=#0000bb]scanner[/color][color=#007700]([/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in[/color][color=#007700]); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print([/color][color=#dd0000]"nhập tiền lương hàng tháng: "[/color][color=#007700]); [/color][color=#0000bb]setluong[/color][color=#007700]([/color][color=#0000bb]nhapdulieu2[/color][color=#007700].[/color][color=#0000bb]nextlong[/color][color=#007700]()); } public [/color][color=#0000bb]void outputgiangvien[/color][color=#007700]() { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#dd0000]"tiền lương của giảng viên: " [/color][color=#007700]+ [/color][color=#0000bb]getluong[/color][color=#007700]()); }} class [/color][color=#0000bb]giamdoc [/color][color=#007700]extends [/color][color=#0000bb]chuan [/color][color=#007700]{ private [/color][color=#0000bb]long tientieu[/color][color=#007700]; public [/color][color=#0000bb]long gettientieu[/color][color=#007700]() { return [/color][color=#0000bb]tientieu[/color][color=#007700]; } public [/color][color=#0000bb]void settientieu[/color][color=#007700]([/color][color=#0000bb]long tientieu[/color][color=#007700]) { [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]tientieu [/color][color=#007700]= [/color][color=#0000bb]tientieu[/color][color=#007700]; }} public class [/color][color=#0000bb]quanly [/color][color=#007700]{ public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string[/color][color=#007700][] [/color][color=#0000bb]args[/color][color=#007700]) { [/color][color=#0000bb]giangvien a [/color][color=#007700]= new [/color][color=#0000bb]giangvien[/color][color=#007700](); [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]input[/color][color=#007700](); [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]inputgiangvien[/color][color=#007700](); [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]output[/color][color=#007700](); [/color][color=#0000bb]a[/color][color=#007700].[/color][color=#0000bb]outputgiangvien[/color][color=#007700](); }}[/color]
</div>
</div>
p/s: anh vjrus.ht và anh tiasangmoi92 cho em xin cái địa chỉ facebook ạ.
-
07-20-2013, 07:00 AM #5Junior Member
- Ngày tham gia
- Apr 2015
- Bài viết
- 0
quagn1998: fb thì thoải mái luôn, nhớ nhắn tin cho anh trc đấy :d <div class="bbcodeblock bbcodequote bbcmspoilerblock">
<div class="attribution type"><noscript>spoiler (move your mouse to the spoiler area to reveal the content)</noscript>
show spoiler
hide spoiler
</div>
<div class="quotecontent">
<div class="bbcm_spoiler_noscript"><blockquote> https://www.facebook.com/tiasangmoi
</blockquote></div>
</div>
</div>
-
07-22-2013, 07:00 AM #6Junior Member
- Ngày tham gia
- Mar 2015
- Bài viết
- 0
mình thấy hơi lạ là sau bài inheritance thì không thấy bài nào về polymorphism
<div class="bbcodeblock bbcodequote bbcmspoilerblock">
<div class="attribution type"><noscript>spoiler (move your mouse to the spoiler area to reveal the content)</noscript>
show spoiler
hide spoiler
</div>
<div class="quotecontent">
<div class="bbcm_spoiler_noscript"><blockquote>
Mã nguồn PHP:[color=#000000]
package basic[/color][color=#007700]; [/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]scanner[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]inputmismatchexception[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]arraylist[/color][color=#007700]; public class [/color][color=#0000bb]universities [/color][color=#007700]{ private static [/color][color=#0000bb]scanner input [/color][color=#007700]= new [/color][color=#0000bb]scanner[/color][color=#007700]( [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in [/color][color=#007700]);private static [/color][color=#0000bb]arraylist[/color][color=#007700]< [/color][color=#0000bb]string [/color][color=#007700]> [/color][color=#0000bb]idofpeople[/color][color=#007700];[/color][color=#ff8000]// ensure 2 different people haven't same id [/color][color=#007700]public [/color][color=#0000bb]universities[/color][color=#007700](){[/color][color=#0000bb]idofpeople [/color][color=#007700]= new [/color][color=#0000bb]arraylist[/color][color=#007700]< [/color][color=#0000bb]string [/color][color=#007700]>();} public class [/color][color=#0000bb]person[/color][color=#007700]{private [/color][color=#0000bb]string name[/color][color=#007700];private [/color][color=#0000bb]int birthyear[/color][color=#007700];private [/color][color=#0000bb]string id[/color][color=#007700]; public [/color][color=#0000bb]person[/color][color=#007700](){[/color][color=#0000bb]setname[/color][color=#007700]();[/color][color=#0000bb]setbirthyear[/color][color=#007700]();[/color][color=#0000bb]setid[/color][color=#007700]();} [/color][color=#ff8000]// get and set methods[/color][color=#007700]public [/color][color=#0000bb]string getname[/color][color=#007700](){return [/color][color=#0000bb]name[/color][color=#007700];} public [/color][color=#0000bb]void setname[/color][color=#007700](){[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print( [/color][color=#dd0000]"
enter name: " [/color][color=#007700]);[/color][color=#0000bb]name [/color][color=#007700]= [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700]();} public [/color][color=#0000bb]int getbirthyear[/color][color=#007700](){return [/color][color=#0000bb]birthyear[/color][color=#007700];} public [/color][color=#0000bb]void setbirthyear[/color][color=#007700](){[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]printf[/color][color=#007700]( [/color][color=#dd0000]"enter year of birth: " [/color][color=#007700]);[/color][color=#0000bb]boolean continueloop [/color][color=#007700]= [/color][color=#0000bb]true[/color][color=#007700];while ( [/color][color=#0000bb]continueloop [/color][color=#007700]){try{[/color][color=#0000bb]birthyear [/color][color=#007700]= [/color][color=#0000bb]integer[/color][color=#007700].[/color][color=#0000bb]parseint[/color][color=#007700]( [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700]() );if( [/color][color=#0000bb]birthyear [/color][color=#007700]< [/color][color=#0000bb]0 [/color][color=#007700])throw new [/color][color=#0000bb]illegalargumentexception[/color][color=#007700]();[/color][color=#0000bb]continueloop [/color][color=#007700]= [/color][color=#0000bb]false[/color][color=#007700];}catch ( [/color][color=#0000bb]inputmismatchexception [/color][color=#007700]| [/color][color=#0000bb]illegalargumentexception e [/color][color=#007700]){[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]( [/color][color=#dd0000]"invalid! please enter again!" [/color][color=#007700]);}}} public [/color][color=#0000bb]string getid[/color][color=#007700](){return [/color][color=#0000bb]id[/color][color=#007700];} [/color][color=#ff8000]// insert id into idofpeople when id isn't used[/color][color=#007700]public [/color][color=#0000bb]void setid[/color][color=#007700](){[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]printf[/color][color=#007700]( [/color][color=#dd0000]"enter id: " [/color][color=#007700]);[/color][color=#0000bb]boolean continueloop [/color][color=#007700]= [/color][color=#0000bb]true[/color][color=#007700];while ( [/color][color=#0000bb]continueloop [/color][color=#007700]){[/color][color=#0000bb]string str [/color][color=#007700]= [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700]();if ( [/color][color=#0000bb]idofpeople[/color][color=#007700].[/color][color=#0000bb]isempty[/color][color=#007700]() )[/color][color=#ff8000]// has not people[/color][color=#007700]{[/color][color=#0000bb]id [/color][color=#007700]= [/color][color=#0000bb]str[/color][color=#007700];[/color][color=#0000bb]idofpeople[/color][color=#007700].[/color][color=#0000bb]add[/color][color=#007700]( [/color][color=#0000bb]str [/color][color=#007700]);[/color][color=#0000bb]continueloop [/color][color=#007700]= [/color][color=#0000bb]false[/color][color=#007700];} else{[/color][color=#0000bb]boolean found [/color][color=#007700]= [/color][color=#0000bb]false[/color][color=#007700];[/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700];while( ![/color][color=#0000bb]found [/color][color=#007700]&& [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]idofpeople[/color][color=#007700].[/color][color=#0000bb]size[/color][color=#007700]() ){if ( [/color][color=#0000bb]str[/color][color=#007700].[/color][color=#0000bb]equals[/color][color=#007700]( [/color][color=#0000bb]idofpeople[/color][color=#007700].[/color][color=#0000bb]get[/color][color=#007700]( [/color][color=#0000bb]i [/color][color=#007700]) ) )[/color][color=#ff8000]// id is already used by anyone else[/color][color=#007700]{[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]str [/color][color=#007700]+ [/color][color=#dd0000]" is already used"[/color][color=#007700]);[/color][color=#0000bb]found [/color][color=#007700]= [/color][color=#0000bb]true[/color][color=#007700];}else[/color][color=#0000bb]i[/color][color=#007700]++;} if ( ![/color][color=#0000bb]found [/color][color=#007700]){[/color][color=#0000bb]id [/color][color=#007700]= [/color][color=#0000bb]str[/color][color=#007700];[/color][color=#0000bb]idofpeople[/color][color=#007700].[/color][color=#0000bb]add[/color][color=#007700]( [/color][color=#0000bb]str [/color][color=#007700]);[/color][color=#0000bb]continueloop [/color][color=#007700]= [/color][color=#0000bb]false[/color][color=#007700];}else[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]printf[/color][color=#007700]( [/color][color=#dd0000]"please enter id again: " [/color][color=#007700]);}}} @[/color][color=#0000bb]override[/color][color=#007700]public [/color][color=#0000bb]string tostring[/color][color=#007700](){return [/color][color=#0000bb]string[/color][color=#007700].[/color][color=#0000bb]format[/color][color=#007700]( [/color][color=#dd0000]"
" [/color][color=#007700]+ [/color][color=#0000bb]name [/color][color=#007700]+ [/color][color=#dd0000]"( born: " [/color][color=#007700]+ [/color][color=#0000bb]birthyear [/color][color=#007700]+ [/color][color=#dd0000]" )" [/color][color=#007700]+ [/color][color=#dd0000]" has id " [/color][color=#007700]+ [/color][color=#0000bb]id [/color][color=#007700]);}} [/color][color=#ff8000]// class student is subclass of class person[/color][color=#007700]public class [/color][color=#0000bb]student [/color][color=#007700]extends [/color][color=#0000bb]person[/color][color=#007700]{private [/color][color=#0000bb]float tuitiondebt[/color][color=#007700]; [/color][color=#ff8000]// constructor[/color][color=#007700]public [/color][color=#0000bb]student[/color][color=#007700](){[/color][color=#0000bb]super[/color][color=#007700]();[/color][color=#0000bb]settuitiondebt[/color][color=#007700]();} public [/color][color=#0000bb]float gettuitiondebt[/color][color=#007700](){return [/color][color=#0000bb]tuitiondebt[/color][color=#007700];} public [/color][color=#0000bb]void settuitiondebt[/color][color=#007700](){[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print( [/color][color=#dd0000]"enter tuition debt: " [/color][color=#007700]);[/color][color=#0000bb]tuitiondebt [/color][color=#007700]= [/color][color=#0000bb]inputmoneyhandling[/color][color=#007700]();} @[/color][color=#0000bb]override[/color][color=#007700]public [/color][color=#0000bb]string tostring[/color][color=#007700](){return [/color][color=#0000bb]string[/color][color=#007700].[/color][color=#0000bb]format[/color][color=#007700]( [/color][color=#dd0000]"
" [/color][color=#007700]+ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]getname[/color][color=#007700]() + [/color][color=#dd0000]"( born: " [/color][color=#007700]+ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]getbirthyear[/color][color=#007700]() + [/color][color=#dd0000]" )" [/color][color=#007700]+ [/color][color=#dd0000]" has id " [/color][color=#007700]+ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]getid[/color][color=#007700]() +[/color][color=#dd0000]" and has $" [/color][color=#007700]+ [/color][color=#0000bb]tuitiondebt [/color][color=#007700]+ [/color][color=#dd0000]" als debt in tuiton fees"[/color][color=#007700]);}} [/color][color=#ff8000]// class lecturer is subclass of person[/color][color=#007700]public class [/color][color=#0000bb]lecturer [/color][color=#007700]extends [/color][color=#0000bb]person[/color][color=#007700]{private [/color][color=#0000bb]float salary[/color][color=#007700]; public [/color][color=#0000bb]lecturer[/color][color=#007700](){[/color][color=#0000bb]super[/color][color=#007700]();[/color][color=#0000bb]setsalary[/color][color=#007700]();} public [/color][color=#0000bb]float getsalary[/color][color=#007700](){return [/color][color=#0000bb]salary[/color][color=#007700];} public [/color][color=#0000bb]void setsalary[/color][color=#007700](){[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print( [/color][color=#dd0000]"enter salary: " [/color][color=#007700]);[/color][color=#0000bb]salary [/color][color=#007700]= [/color][color=#0000bb]inputmoneyhandling[/color][color=#007700]();} @[/color][color=#0000bb]override[/color][color=#007700]public [/color][color=#0000bb]string tostring[/color][color=#007700](){return [/color][color=#0000bb]string[/color][color=#007700].[/color][color=#0000bb]format[/color][color=#007700]( [/color][color=#dd0000]"
" [/color][color=#007700]+ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]getname[/color][color=#007700]() + [/color][color=#dd0000]"( born: " [/color][color=#007700]+ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]getbirthyear[/color][color=#007700]() + [/color][color=#dd0000]" )" [/color][color=#007700]+ [/color][color=#dd0000]" has id - " [/color][color=#007700]+ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]getid[/color][color=#007700]() +[/color][color=#dd0000]" and has $" [/color][color=#007700]+ [/color][color=#0000bb]salary [/color][color=#007700]+ [/color][color=#dd0000]" als monthly salary"[/color][color=#007700]);}} public class [/color][color=#0000bb]director [/color][color=#007700]extends [/color][color=#0000bb]person[/color][color=#007700]{private [/color][color=#0000bb]float paid[/color][color=#007700]; public [/color][color=#0000bb]director[/color][color=#007700](){[/color][color=#0000bb]super[/color][color=#007700]();[/color][color=#0000bb]setpaid[/color][color=#007700]();} public [/color][color=#0000bb]float getpaid[/color][color=#007700](){return [/color][color=#0000bb]paid[/color][color=#007700];} public [/color][color=#0000bb]void setpaid[/color][color=#007700](){[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print( [/color][color=#dd0000]"enter monthly paid money: " [/color][color=#007700]);[/color][color=#0000bb]paid [/color][color=#007700]= [/color][color=#0000bb]inputmoneyhandling[/color][color=#007700]();} @[/color][color=#0000bb]override[/color][color=#007700]public [/color][color=#0000bb]string tostring[/color][color=#007700](){return [/color][color=#0000bb]string[/color][color=#007700].[/color][color=#0000bb]format[/color][color=#007700]( [/color][color=#dd0000]"
" [/color][color=#007700]+ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]getname[/color][color=#007700]() + [/color][color=#dd0000]"( born: " [/color][color=#007700]+ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]getbirthyear[/color][color=#007700]() + [/color][color=#dd0000]" )" [/color][color=#007700]+ [/color][color=#dd0000]" has id " [/color][color=#007700]+ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]getid[/color][color=#007700]() +[/color][color=#dd0000]" and has $" [/color][color=#007700]+ [/color][color=#0000bb]paid [/color][color=#007700]+ [/color][color=#dd0000]" als monthly paid money"[/color][color=#007700]);}} [/color][color=#ff8000]// ensure entered money is greater than 0[/color][color=#007700]private static [/color][color=#0000bb]float inputmoneyhandling[/color][color=#007700](){[/color][color=#0000bb]float temp [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700];[/color][color=#0000bb]boolean continueloop [/color][color=#007700]= [/color][color=#0000bb]true[/color][color=#007700];while( [/color][color=#0000bb]continueloop [/color][color=#007700]){try{[/color][color=#0000bb]temp [/color][color=#007700]= [/color][color=#0000bb]integer[/color][color=#007700].[/color][color=#0000bb]parseint[/color][color=#007700]( [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700]() );if ( [/color][color=#0000bb]temp [/color][color=#007700]< [/color][color=#0000bb]0 [/color][color=#007700])throw new [/color][color=#0000bb]illegalargumentexception[/color][color=#007700]();[/color][color=#0000bb]continueloop [/color][color=#007700]= [/color][color=#0000bb]false[/color][color=#007700];}catch ( [/color][color=#0000bb]inputmismatchexception [/color][color=#007700]| [/color][color=#0000bb]illegalargumentexception e [/color][color=#007700]){[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]( [/color][color=#dd0000]"invalid! please enter again!" [/color][color=#007700]);}} return [/color][color=#0000bb]temp[/color][color=#007700];} public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string[/color][color=#007700][] [/color][color=#0000bb]args[/color][color=#007700]) {[/color][color=#0000bb]universities newtest [/color][color=#007700]= new [/color][color=#0000bb]universities[/color][color=#007700]();[/color][color=#0000bb]person su1 [/color][color=#007700]= [/color][color=#0000bb]newtest[/color][color=#007700].new [/color][color=#0000bb]student[/color][color=#007700]();[/color][color=#0000bb]person lec1 [/color][color=#007700]= [/color][color=#0000bb]newtest[/color][color=#007700].new [/color][color=#0000bb]lecturer[/color][color=#007700]();[/color][color=#0000bb]director di1 [/color][color=#007700]= [/color][color=#0000bb]newtest[/color][color=#007700].new [/color][color=#0000bb]director[/color][color=#007700](); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]( [/color][color=#0000bb]su1[/color][color=#007700].[/color][color=#0000bb]tostring[/color][color=#007700]() );[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]( [/color][color=#0000bb]lec1[/color][color=#007700].[/color][color=#0000bb]tostring[/color][color=#007700]() );[/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]( [/color][color=#0000bb]di1[/color][color=#007700].[/color][color=#0000bb]tostring[/color][color=#007700]() );} }[/color]
</div>
</div>
-
07-22-2013, 07:00 AM #7Junior Member
- Ngày tham gia
- Jan 2015
- Bài viết
- 0
Gửi bởi phongdt
p/s: bạn có thể chèn code dễ nhìn hơn được không?
-
07-22-2013, 07:00 AM #8Junior Member
- Ngày tham gia
- Aug 2015
- Bài viết
- 0
Gửi bởi tiasangmoi92
-
07-22-2013, 07:00 AM #9Junior Member
- Ngày tham gia
- Feb 2015
- Bài viết
- 0
Gửi bởi phongdt
-
07-22-2013, 07:00 AM #10Junior Member
- Ngày tham gia
- Feb 2015
- Bài viết
- 0
hình như là do mình không dùng toolbar mà tự gõ bb code
<div class="bbcodeblock bbcodequote bbcmspoilerblock">
<div class="attribution type"><noscript>spoiler (move your mouse to the spoiler area to reveal the content)</noscript>
show spoiler
hide spoiler
</div>
<div class="quotecontent">
<div class="bbcm_spoiler_noscript"><blockquote>
Mã nguồn PHP:[color=#000000]
package basic[/color][color=#007700]; [/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]scanner[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]inputmismatchexception[/color][color=#007700];[/color][color=#0000bb]import java[/color][color=#007700].[/color][color=#0000bb]util[/color][color=#007700].[/color][color=#0000bb]arraylist[/color][color=#007700]; public class [/color][color=#0000bb]universities [/color][color=#007700]{ private static [/color][color=#0000bb]scanner input [/color][color=#007700]= new [/color][color=#0000bb]scanner[/color][color=#007700]( [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]in [/color][color=#007700]); private static [/color][color=#0000bb]arraylist[/color][color=#007700]< [/color][color=#0000bb]string [/color][color=#007700]> [/color][color=#0000bb]idofpeople[/color][color=#007700]; [/color][color=#ff8000]// ensure 2 different people haven't same id [/color][color=#007700]public [/color][color=#0000bb]universities[/color][color=#007700]() { [/color][color=#0000bb]idofpeople [/color][color=#007700]= new [/color][color=#0000bb]arraylist[/color][color=#007700]< [/color][color=#0000bb]string [/color][color=#007700]>(); } public class [/color][color=#0000bb]person [/color][color=#007700]{ private [/color][color=#0000bb]string name[/color][color=#007700]; private [/color][color=#0000bb]int birthyear[/color][color=#007700]; private [/color][color=#0000bb]string id[/color][color=#007700]; public [/color][color=#0000bb]person[/color][color=#007700]() { [/color][color=#0000bb]setname[/color][color=#007700](); [/color][color=#0000bb]setbirthyear[/color][color=#007700](); [/color][color=#0000bb]setid[/color][color=#007700](); } [/color][color=#ff8000]// get and set methods [/color][color=#007700]public [/color][color=#0000bb]string getname[/color][color=#007700]() { return [/color][color=#0000bb]name[/color][color=#007700]; } public [/color][color=#0000bb]void setname[/color][color=#007700]() { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print( [/color][color=#dd0000]"
enter name: " [/color][color=#007700]); [/color][color=#0000bb]name [/color][color=#007700]= [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); } public [/color][color=#0000bb]int getbirthyear[/color][color=#007700]() { return [/color][color=#0000bb]birthyear[/color][color=#007700]; } public [/color][color=#0000bb]void setbirthyear[/color][color=#007700]() { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]printf[/color][color=#007700]( [/color][color=#dd0000]"enter year of birth: " [/color][color=#007700]); [/color][color=#0000bb]boolean continueloop [/color][color=#007700]= [/color][color=#0000bb]true[/color][color=#007700]; while ( [/color][color=#0000bb]continueloop [/color][color=#007700]) { try { [/color][color=#0000bb]birthyear [/color][color=#007700]= [/color][color=#0000bb]integer[/color][color=#007700].[/color][color=#0000bb]parseint[/color][color=#007700]( [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700]() ); if( [/color][color=#0000bb]birthyear [/color][color=#007700]< [/color][color=#0000bb]0 [/color][color=#007700]) throw new [/color][color=#0000bb]illegalargumentexception[/color][color=#007700](); [/color][color=#0000bb]continueloop [/color][color=#007700]= [/color][color=#0000bb]false[/color][color=#007700]; } catch ( [/color][color=#0000bb]inputmismatchexception [/color][color=#007700]| [/color][color=#0000bb]illegalargumentexception e [/color][color=#007700]) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]( [/color][color=#dd0000]"invalid! please enter again!" [/color][color=#007700]); } } } public [/color][color=#0000bb]string getid[/color][color=#007700]() { return [/color][color=#0000bb]id[/color][color=#007700]; } [/color][color=#ff8000]// insert id into idofpeople when id isn't used [/color][color=#007700]public [/color][color=#0000bb]void setid[/color][color=#007700]() { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]printf[/color][color=#007700]( [/color][color=#dd0000]"enter id: " [/color][color=#007700]); [/color][color=#0000bb]boolean continueloop [/color][color=#007700]= [/color][color=#0000bb]true[/color][color=#007700]; while ( [/color][color=#0000bb]continueloop [/color][color=#007700]) { [/color][color=#0000bb]string str [/color][color=#007700]= [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700](); if ( [/color][color=#0000bb]idofpeople[/color][color=#007700].[/color][color=#0000bb]isempty[/color][color=#007700]() ) [/color][color=#ff8000]// has not people [/color][color=#007700]{ [/color][color=#0000bb]id [/color][color=#007700]= [/color][color=#0000bb]str[/color][color=#007700]; [/color][color=#0000bb]idofpeople[/color][color=#007700].[/color][color=#0000bb]add[/color][color=#007700]( [/color][color=#0000bb]str [/color][color=#007700]); [/color][color=#0000bb]continueloop [/color][color=#007700]= [/color][color=#0000bb]false[/color][color=#007700]; } else { [/color][color=#0000bb]boolean found [/color][color=#007700]= [/color][color=#0000bb]false[/color][color=#007700]; [/color][color=#0000bb]int i [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; while( ![/color][color=#0000bb]found [/color][color=#007700]&& [/color][color=#0000bb]i [/color][color=#007700]< [/color][color=#0000bb]idofpeople[/color][color=#007700].[/color][color=#0000bb]size[/color][color=#007700]() ) { if ( [/color][color=#0000bb]str[/color][color=#007700].[/color][color=#0000bb]equals[/color][color=#007700]( [/color][color=#0000bb]idofpeople[/color][color=#007700].[/color][color=#0000bb]get[/color][color=#007700]( [/color][color=#0000bb]i [/color][color=#007700]) ) ) [/color][color=#ff8000]// id is already used by anyone else [/color][color=#007700]{ [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]([/color][color=#0000bb]str [/color][color=#007700]+ [/color][color=#dd0000]" is already used"[/color][color=#007700]); [/color][color=#0000bb]found [/color][color=#007700]= [/color][color=#0000bb]true[/color][color=#007700]; } else [/color][color=#0000bb]i[/color][color=#007700]++; } if ( ![/color][color=#0000bb]found [/color][color=#007700]) { [/color][color=#0000bb]id [/color][color=#007700]= [/color][color=#0000bb]str[/color][color=#007700]; [/color][color=#0000bb]idofpeople[/color][color=#007700].[/color][color=#0000bb]add[/color][color=#007700]( [/color][color=#0000bb]str [/color][color=#007700]); [/color][color=#0000bb]continueloop [/color][color=#007700]= [/color][color=#0000bb]false[/color][color=#007700]; } else [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]printf[/color][color=#007700]( [/color][color=#dd0000]"please enter id again: " [/color][color=#007700]); } } } @[/color][color=#0000bb]override [/color][color=#007700]public [/color][color=#0000bb]string tostring[/color][color=#007700]() { return [/color][color=#0000bb]string[/color][color=#007700].[/color][color=#0000bb]format[/color][color=#007700]( [/color][color=#dd0000]"
" [/color][color=#007700]+ [/color][color=#0000bb]name [/color][color=#007700]+ [/color][color=#dd0000]"( born: " [/color][color=#007700]+ [/color][color=#0000bb]birthyear [/color][color=#007700]+ [/color][color=#dd0000]" )" [/color][color=#007700]+ [/color][color=#dd0000]" has id " [/color][color=#007700]+ [/color][color=#0000bb]id [/color][color=#007700]); } } [/color][color=#ff8000]// class student is subclass of class person [/color][color=#007700]public class [/color][color=#0000bb]student [/color][color=#007700]extends [/color][color=#0000bb]person [/color][color=#007700]{ private [/color][color=#0000bb]float tuitiondebt[/color][color=#007700]; [/color][color=#ff8000]// constructor [/color][color=#007700]public [/color][color=#0000bb]student[/color][color=#007700]() { [/color][color=#0000bb]super[/color][color=#007700](); [/color][color=#0000bb]settuitiondebt[/color][color=#007700](); } public [/color][color=#0000bb]float gettuitiondebt[/color][color=#007700]() { return [/color][color=#0000bb]tuitiondebt[/color][color=#007700]; } public [/color][color=#0000bb]void settuitiondebt[/color][color=#007700]() { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print( [/color][color=#dd0000]"enter tuition debt: " [/color][color=#007700]); [/color][color=#0000bb]tuitiondebt [/color][color=#007700]= [/color][color=#0000bb]inputmoneyhandling[/color][color=#007700](); } @[/color][color=#0000bb]override [/color][color=#007700]public [/color][color=#0000bb]string tostring[/color][color=#007700]() { return [/color][color=#0000bb]string[/color][color=#007700].[/color][color=#0000bb]format[/color][color=#007700]( [/color][color=#dd0000]"
" [/color][color=#007700]+ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]getname[/color][color=#007700]() + [/color][color=#dd0000]"( born: " [/color][color=#007700]+ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]getbirthyear[/color][color=#007700]() + [/color][color=#dd0000]" )" [/color][color=#007700]+ [/color][color=#dd0000]" has id " [/color][color=#007700]+ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]getid[/color][color=#007700]() + [/color][color=#dd0000]" and has $" [/color][color=#007700]+ [/color][color=#0000bb]tuitiondebt [/color][color=#007700]+ [/color][color=#dd0000]" als debt in tuiton fees"[/color][color=#007700]); } } [/color][color=#ff8000]// class lecturer is subclass of person [/color][color=#007700]public class [/color][color=#0000bb]lecturer [/color][color=#007700]extends [/color][color=#0000bb]person [/color][color=#007700]{ private [/color][color=#0000bb]float salary[/color][color=#007700]; public [/color][color=#0000bb]lecturer[/color][color=#007700]() { [/color][color=#0000bb]super[/color][color=#007700](); [/color][color=#0000bb]setsalary[/color][color=#007700](); } public [/color][color=#0000bb]float getsalary[/color][color=#007700]() { return [/color][color=#0000bb]salary[/color][color=#007700]; } public [/color][color=#0000bb]void setsalary[/color][color=#007700]() { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print( [/color][color=#dd0000]"enter salary: " [/color][color=#007700]); [/color][color=#0000bb]salary [/color][color=#007700]= [/color][color=#0000bb]inputmoneyhandling[/color][color=#007700](); } @[/color][color=#0000bb]override [/color][color=#007700]public [/color][color=#0000bb]string tostring[/color][color=#007700]() { return [/color][color=#0000bb]string[/color][color=#007700].[/color][color=#0000bb]format[/color][color=#007700]( [/color][color=#dd0000]"
" [/color][color=#007700]+ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]getname[/color][color=#007700]() + [/color][color=#dd0000]"( born: " [/color][color=#007700]+ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]getbirthyear[/color][color=#007700]() + [/color][color=#dd0000]" )" [/color][color=#007700]+ [/color][color=#dd0000]" has id - " [/color][color=#007700]+ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]getid[/color][color=#007700]() + [/color][color=#dd0000]" and has $" [/color][color=#007700]+ [/color][color=#0000bb]salary [/color][color=#007700]+ [/color][color=#dd0000]" als monthly salary"[/color][color=#007700]); } } public class [/color][color=#0000bb]director [/color][color=#007700]extends [/color][color=#0000bb]person [/color][color=#007700]{ private [/color][color=#0000bb]float paid[/color][color=#007700]; public [/color][color=#0000bb]director[/color][color=#007700]() { [/color][color=#0000bb]super[/color][color=#007700](); [/color][color=#0000bb]setpaid[/color][color=#007700](); } public [/color][color=#0000bb]float getpaid[/color][color=#007700]() { return [/color][color=#0000bb]paid[/color][color=#007700]; } public [/color][color=#0000bb]void setpaid[/color][color=#007700]() { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].print( [/color][color=#dd0000]"enter monthly paid money: " [/color][color=#007700]); [/color][color=#0000bb]paid [/color][color=#007700]= [/color][color=#0000bb]inputmoneyhandling[/color][color=#007700](); } @[/color][color=#0000bb]override [/color][color=#007700]public [/color][color=#0000bb]string tostring[/color][color=#007700]() { return [/color][color=#0000bb]string[/color][color=#007700].[/color][color=#0000bb]format[/color][color=#007700]( [/color][color=#dd0000]"
" [/color][color=#007700]+ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]getname[/color][color=#007700]() + [/color][color=#dd0000]"( born: " [/color][color=#007700]+ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]getbirthyear[/color][color=#007700]() + [/color][color=#dd0000]" )" [/color][color=#007700]+ [/color][color=#dd0000]" has id " [/color][color=#007700]+ [/color][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]getid[/color][color=#007700]() + [/color][color=#dd0000]" and has $" [/color][color=#007700]+ [/color][color=#0000bb]paid [/color][color=#007700]+ [/color][color=#dd0000]" als monthly paid money"[/color][color=#007700]); } } [/color][color=#ff8000]// ensure entered money is greater than 0 [/color][color=#007700]private static [/color][color=#0000bb]float inputmoneyhandling[/color][color=#007700]() { [/color][color=#0000bb]float temp [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]boolean continueloop [/color][color=#007700]= [/color][color=#0000bb]true[/color][color=#007700]; while( [/color][color=#0000bb]continueloop [/color][color=#007700]) { try { [/color][color=#0000bb]temp [/color][color=#007700]= [/color][color=#0000bb]integer[/color][color=#007700].[/color][color=#0000bb]parseint[/color][color=#007700]( [/color][color=#0000bb]input[/color][color=#007700].[/color][color=#0000bb]nextline[/color][color=#007700]() ); if ( [/color][color=#0000bb]temp [/color][color=#007700]< [/color][color=#0000bb]0 [/color][color=#007700]) throw new [/color][color=#0000bb]illegalargumentexception[/color][color=#007700](); [/color][color=#0000bb]continueloop [/color][color=#007700]= [/color][color=#0000bb]false[/color][color=#007700]; } catch ( [/color][color=#0000bb]inputmismatchexception [/color][color=#007700]| [/color][color=#0000bb]illegalargumentexception e [/color][color=#007700]) { [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]( [/color][color=#dd0000]"invalid! please enter again!" [/color][color=#007700]); } } return [/color][color=#0000bb]temp[/color][color=#007700]; } public static [/color][color=#0000bb]void main[/color][color=#007700]([/color][color=#0000bb]string[/color][color=#007700][] [/color][color=#0000bb]args[/color][color=#007700]) { [/color][color=#0000bb]universities newtest [/color][color=#007700]= new [/color][color=#0000bb]universities[/color][color=#007700](); [/color][color=#0000bb]person su1 [/color][color=#007700]= [/color][color=#0000bb]newtest[/color][color=#007700].new [/color][color=#0000bb]student[/color][color=#007700](); [/color][color=#0000bb]person lec1 [/color][color=#007700]= [/color][color=#0000bb]newtest[/color][color=#007700].new [/color][color=#0000bb]lecturer[/color][color=#007700](); [/color][color=#0000bb]director di1 [/color][color=#007700]= [/color][color=#0000bb]newtest[/color][color=#007700].new [/color][color=#0000bb]director[/color][color=#007700](); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]( [/color][color=#0000bb]su1[/color][color=#007700].[/color][color=#0000bb]tostring[/color][color=#007700]() ); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]( [/color][color=#0000bb]lec1[/color][color=#007700].[/color][color=#0000bb]tostring[/color][color=#007700]() ); [/color][color=#0000bb]system[/color][color=#007700].[/color][color=#0000bb]out[/color][color=#007700].[/color][color=#0000bb]println[/color][color=#007700]( [/color][color=#0000bb]di1[/color][color=#007700].[/color][color=#0000bb]tostring[/color][color=#007700]() ); } }[/color]
</div>
</div>
"căn do xuất tinh ngược này do lượng đường cao dẫn tới thương tổn dây thần kinh tại các cơ vòng khiến người bệnh bị xuất tinh ngược. thường ngày, khi xuất tinh thì cơ thắt cổ bọng đái đóng lại để...
Nguyên nhân và giải pháp cho tình trạng không xuất tinh ở phái mạnh?