Java
Java
}
}
3. Class
* Access Modifiers
- public, private, protected
- default: Default class chỉ có thể truy cập trong cùng một package.
* Non-Access Modifiers
- final:
public class Main {
final int x = 10;
final double PI = 3.14;
- abstract:
class InnerClass {
int y = 5;
}
}
public class Main {
public static void main(String[] args) {
OuterClass myOuter = new OuterClass();
OuterClass.InnerClass myInner = myOuter.new InnerClass();
System.out.println(myInner.y + myOuter.x);
}
}
// Outputs 15 (5 + 10)
* Java Interface
VD: interface Animal {
public void animalSound(); // interface method (does not have a body)
public void sleep(); // interface method (does not have a body)
}
class Main {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}
4. Java Date and Time
Java does not have a built-in Date class, but we can import the java.time package
to work with the date and time API. The package includes many date and time
classes. For example:
Class Description
LocalDate Represents a date (year, month, day (yyyy-MM-dd))
LocalTime Represents a time (hour, minute, second and nanoseconds (HH-mm-ss-
ns))
LocalDateTime Represents both a date and a time (yyyy-MM-dd-HH-mm-ss-ns)
DateTimeFormatte Formatter for displaying and parsing date-time objects