• Use this refer to any member of the current object from within an instance method or a constructor by using this... The Access Modifiers1Access modifiers control which classes may use
Trang 1Session 04 More on Classes and Nested Classes
(http://docs.oracle.com/javase/tutorial/java/javaOO/more.html)
Trang 2• Returning a Value from a Method
• Using the this Keyword
• Controlling Access to Members of a Class
• Understanding Class Members
Trang 3Returning a Value from a Method
• A method returns to the code that invoked it when it:
– completes all the statements in the method,
– reaches a return statement, or
– throws an exception (covered later),
• A method can return a primitive values or
reference value such as class or interface.
Trang 4Using the this Keyword
• Within an instance method or a constructor, this is
a reference to the current object.
• Use this refer to any member of the current object from within an instance method or a constructor by using this.
Point(int x, int y)
{ this.x = x; this.y =
y; }
public Point() { this(0, 0);
}
Trang 5The Access Modifiers(1)
Access modifiers control which classes may use a feature:
Features:
• The class itself
• Its member variables
• Its methods and constructors
• Its nested classes
Trang 6The Access Modifiers(2)
• The access modifiers are
Trang 7non-The Access Modifiers(3)
• A feature may have at most one access
modifier If a feature has no access modifier
we call default access modifier.
Trang 9A Sample of Access Level
Trang 10Tips on Choosing an Access Level
• Use the most restrictive access level that
makes sense for a particular member
Use private unless you have a good reason not to.
• Avoid public fields except for constants Public fields tend to link you to a particular
implementation and limit your flexibility in
changing your code.
Trang 11Understanding Class Members (1)
• Class Variables
• Fields that have the static modifier in their
declaration
• References by class name itself.
public class Bicycle {
// add an instance variable for the object ID
private int id;
Trang 12Understanding Class Members (2)
• Class Methods:
– Static methods: will be invoked by using class name
• ClassName.methodName(args) – Can access class variables and class methods directly.
– cannot access instance variables or instance methods
directly—they must use an object reference
– cannot use the this keyword as there is no instance for this to refer to.
Trang 13Understanding Class Members (3)
– The static modifier, in combination with
the final modifier, is also used to define
constants The final modifier indicates that the value of this field cannot change.
static final double PI = 3.141592653589793;
Trang 16Initializing Fields (2)
Trang 17Initializing Fields (3)
• Initializing Instance Members
– Will be copied initializer blocks into every constructor – Can be used to share a block of code between multiple constructors.
– Initializer blocks for instance variables look just like static initializer blocks, but without the static keyword:
{
// whatever code is needed for initialization goes here }
Trang 18Nested Classes
• Define a class within another class Such a
class is called a nested class:
Trang 19Why Use Nested Classes?
• It is a way of logically grouping classes that are only used in one place.
• It increases encapsulation.
• It can lead to more readable and maintainable code.
Trang 20public void makeInner() {
InnerOne anInner = new InnerOne();
anInner.innerMethod();
}
Trang 21Create an instance of an inner class in
an unrelated class.
InnerOne();
i.innerMethod();
public static void main(String args[]) {
OuterOne o = new OuterOne();
OuterOne.InnerOne i = o.new InnerOne();
i.innerMethod();
}
Trang 22Static Inner Class Sample
public class MyOuter {
public static class MyInner {
}
public static void main(String [] args) {
MyInner aMyInner = new
MyOuter.MyInner();
}
}
Trang 23Shadowing (2)
• Declaration of a type in a particular scope has the same name as another declaration in the enclosing scope, then the
declaration shadows the declaration of the
enclosing scope.
Trang 24}
public static void main(String args) {
ShadowTest st = new ShadowTest();
ShadowTest.FirstLevel fl = st.new FirstLevel();
Trang 25Anonymous Classes (1)
• Enable you to make your code more concise
• Enable you to declare and instantiate a class at the same time
• They are like local classes except that they do not have a name
• Use them if you need to use a local class only once.
Trang 26Anonymous Classes (2)
HelloWorld englishGreeting = new EnglishGreeting();
HelloWorld frenchGreeting = new HelloWorld() {
String name = "tout le monde";
public void greet() {
Trang 27Anonymous Classes (3)
public class HelloWorldAnonymousClasses {
interface HelloWorld {
public void greet();
public void greetSomeone(String someone);
}
public void sayHello() {
class EnglishGreeting implements HelloWorld {
String name = "world";
public void greet() {
Trang 28Enum Types (1)
• An enum type is a special data type that
enables for a variable to be a set of
predefined constants.
• We use enum types any time you need to represent a fixed set of constants.
Trang 29case SATURDAY: case SUNDAY:
System.out.println("Weekends are best."); break;
Trang 30public static void main(String[] args) {
EnumTest firstDay = new EnumTest(Day.MONDAY); firstDay.tellItLikeItIs();
}
}
Trang 31• Returning a Value from a Method
• Using the this Keyword
• Controlling Access to Members of a Class
• Understanding Class Members
Trang 32• https://
docs.oracle.com/javase/tutorial/java/javaOO/ QandE/nested-questions.html
programming.guide/java/overloading-overridi ng-shadowing-hiding-obscuring.html
www.math.uni-hamburg.de/doc/java/tutorial/ java/javaOO/QandE/nested-answers.html
Trang 34• Hãy giải thích 5 khái niệm sau đây trong java: Overloading, overriding, shadowing, hiding, and obscuring
• Dùng ví dụ để giải thích
• Gửi mail trước 23pm về hunghh12@fe.edu.vn
• (Dùng email fpt của SV)