You are on page 1of 13

ĐỊNH NGHĨA CLASSES

Lập trình Java


NỘI DUNG

Class Definitions

Encapsulation

Overloading

Constructors

Lập trình Java


CLASS DEFINITIONS
Simple class

public class DateFirstTry {


public String month;
Variables
public int day;
public int year;
methods
public void writeOutput()
{
System.out.println(month + " " + day + ", " + year);
}
}

Lập trình Java


CLASS DEFINITIONS(2)
Instance of the class
DateFirstTry date1, date2;

class object of the class

Mỗi Object của cùng một class sẽ có vùng dữ liệu khác


nhau, nhưng cùng kiểu dữ liệu cho mỗi biến và có cùng các
phương thức.

date1 = new DateFirstTry();

creates a new object

Lập trình Java


CLASS DEFINITIONS(3)
Local variables / Blocks
 Biến được khai báo bên trong một phương thức hoặc
block được gọi là local variable.
for(int i = 0; i++ < 5;)
{
a = a * i;
}

 Trong Java không tồn tại global variables.

automatic type casting


byte ➔ short ➔ int ➔ long ➔ float ➔ double

Lập trình Java


CLASS DEFINITIONS(4)
Parameter vs Argument

public void setDate( int newMonth,int newDay,int newYear )


{
month = monthString(newMonth );
day = newDay;
year = newYear;
}

Lập trình Java


ENCAPSULATION
Định nghĩa
Encapsulation là data và method được kết hợp vào một
class, người dùng chỉ thấy và tương tác những thành phần
data1
được cho phép.

data2
method2
Programmer
data
who
method1
method3 method uses the
class
method4 data3

Program
Lập trình Java
ENCAPSULATION(2)
public & private

all instance
variables private
private data1

public data public class DateFirstTry {


private String month;
private method1 private int day;
private int year;

public method public void writeOutput() {


System.out.println(year);
}
Program }

Lập trình Java


ENCAPSULATION(3)
accessor & mutator

mutator public int getDay(){


public method return day;
}

public void setDay(int newDay){


day = (newDay > 0 && newDay < 32) ?
newDay : 1;
private data1
}

accessor
public method

Lập trình Java


OVERLOADING
Định nghĩa
Hai hay nhiều phương thức có cùng tên trong một class.
public void setDate(int month,int day, int year)

public void setDate(String month,int day, int year)

public void setDate(int year)

different
parameter list

Lập trình Java


OVERLOADING(2)
method signature
public int computeSomething(int n1,double x1, String name);

Signature

computeSomething(int,double,double,String)

Cannot Overload
Based on the Type
Returned

Lập trình Java


CONSTRUCTORS
Định nghĩa
1. Được dùng để khởi tạo giá trị cho các biến trong class.
2. Được thực hiện tại thời điểm gọi lệnh new
public DateFirstTry()
{
day = 1;
year = 2000;
month = "Một";
} date1 = new DateFirstTry();

public DateFirstTry(int iDay, int iYear, String iMonth)


{
day = iDay;
year = iYear;
month = iMonth;
} date2 = new DateFirstTry(11, 2013, "Muoi");
Lập trình Java
TẠO VÀ SỬ DỤNG JAR TRONG ECLIPSE

You must not create any library, if you use the same workspace.
Option 1: Just use the source
In the properties of the project which has the dependencies you can add another
source-folder:
Properties > Java Build Path > Tab: Source > Add Folder...
In the Project Tab you can "add" the whole project to the other project, too. There
are many ways to achieve your goal.
Option 2: Create and add the library to the Build-Path
Adding an existing Jar (your own library):
If it is in the workspace:
Properties > Java Build Path > Tab: Libraries > Add JARs...
If it is somewhere on the drive:
Properties > Java Build Path > Tab: Libraries > Add External JARs...
Exporting a source folder as a library:
Context Menu of Source Folder > Export > Jar File
There are two types: Executable Jars and "normal" Jars. You don't need an
executable Jar.

Lập trình Java

You might also like