Professional Documents
Culture Documents
Language (JAVA)
(面向对象程序设计)
李建欣(Jianxin Li)
lijx@act.buaa.edu.cn
lijianxin@gmail.com
School of Computer,
Beihang University
© Beihang University
Chapter 3 Review
Primitive Data Types
Statement
Array
Reminding: http://graduate.buaa.edu.cn
Selecting your lesson firstly.
2
© Beihang University
Exercise (for everyone)
1. Writing a program to output followings:
1
12
123
1234
12345
123456
2. The results?
3
© Beihang University
A Question?
4
© Beihang University
Chapter-4
5
© Beihang University
Some key points in Java OOP
6
© Beihang University
Basic Concepts in OOP
Three key features of OOP
Encapsulation ”封装 ”
form of software reusability
Polymorphism ”多态 ”
Write programs in a general fashion
to handle a wide variety of classes
Inheritance ” 继承 ”
7
© Beihang University
Class and Object
A class defines the variables and the methods
common to all objects of a certain kind.
Primitive data type
The abstracted date type can be realized with Java
class
8
© Beihang University
An Example of Java Class & Object
Class Definition:
Class EmpInfo{
String name;
String designation;
String department;
void print( ){ System.out.println(name+“is”+
designation+“at”+department);}
}
Object initialization:…
EmpInfo employee = new EmpInfo( );
employee.name = “ Robert Javaman”;
employee.designation = “Manager”;
employee.department = “Coffee shop”;
employee.print( ); ...
9
© Beihang University
The Lifecycle of Object
Creating an object
Using an object
Destroying an object
10
© Beihang University
Object Creation
e.g.,:
Point origin_one ;
origin_one = new Point(23, 94);
Rectangle rect_one = new Rectangle(origin_one, 100, 200);
Rectangle rect_two = new Rectangle(50, 100);
11
© Beihang University
Object Instantiation Procedure
Constructing and initializing an object
Invoking new XXX();
Creating memory space (variable, method body, and
implemented code) and initializing variables member of
class
numerical value :0;
boolean: false;
String: null;
Explicit initialization: the variable declaration of class
includes some simple assignment expression
public class Initialized{
private int x = 5 ;
private String name = “Fred”;
…
Construction function
} execution
12
© Beihang University
Garbage Collection
Garbage Collection: Java Running Environment
If an object will not been used, and delete it
If an object have no reference to it, it can be
collected as a garbage
Garbage Collector: A process involved in the JRE,
and the allocated memory of non-used object can
be released periodically.
13
© Beihang University
Garbage Collection
Java :A a=new A( ); a b c
A b=a; b c
A c=a;
// delete it
a= null ;
14
© Beihang University
Creating an object
Creation of Class
15
© Beihang University
Constructor Function
Note:
•The name of method must be the same as
the name of class.
•No return type
16
© Beihang University
Default Constructor
If there is no constructor function, a default
constructor will be invovled during the Java codes
compiling. For example, public Employee( ){ };
Once you declared a new constructor function, the
default constructor will not be involved into the
source code.
17
© Beihang University
Class Function Member
Format
<modifiers><return_type><name>
([<argument_list>])[throws <exception>]{<block>}
18
© Beihang University
Example:
19
© Beihang University
Encapsulation of Data
21
© Beihang University
Example
22
© Beihang University
Why Encapsulation is Important
A real life example.
class BankAccounts{
public float AccountBalance;
…}
--------------------------------------------------------
myBankAccount.AccountBalance=-100; // without encapsulation
----------------------------------------------------------
Bool setAccountBalance(float amount) // with encapsulation
{
if (amount>=0 && someOtherConditions)
AccountBalance = amount;
return true;
else
return false;
}
23
© Beihang University
Keyword : this
24
© Beihang University
Keyword: super
Super is used to point to the superclass。
25
© Beihang University
Subclass
26
© Beihang University
Extends
Keyword extends is used.
public class Employee { public class Manager extends Employee
String name ; {
Date hireDate ;
String department ;
Date dateofBirth ;
String jobTitle ; Employee [ ] subordinates;
int grade ;
… }
}
28
© Beihang University
Invoking function of Superclass
29
© Beihang University
(重载)Overloading
In a class, one method can be defined many
times.
classScreen {
public void print( int i){ … }
public void print( float i){ … }
public void print( String str ){ … }
}
Principals:
• the list of parameter must be different to
distinguish difference function implementation.
•The return type and access modifier may be
same or different.
30
© Beihang University
(重写)Overriding
A subclass can modify the behaviors inherited from
super class
The return type, method name, parameter list must
be same with the methods defined in the super class
31
© Beihang University
Overriding Example
33
© Beihang University
Polymorphism
34
© Beihang University
Polymorphism
35
© Beihang University
Polymorphism
Overloaded methods are methods with the same name
signature but either a different number of parameters or
different types in the parameter list. By defining a method for
handling each type of parameter you achieve the effect that
you want.
Overridden methods are methods that are redefined within
an inherited or subclass. They have the same signature and
the subclass definition is used.
Don't confuse the concepts of overloading and overriding.
Overloading deals with multiple methods in the same class with the same
name but different signatures.
Overriding deals with two methods, one in a parent class and one in a child
class, that have the same signature.
Overloading lets you define a similar operation in different ways for different
data.
Overriding lets you define a similar operation in different ways for different
object types.
36
© Beihang University
Polymorphism
Polymorphism is the capability of an action or method to do
different things based on the object that it is acting upon. This
is the third basic principle of object oriented programming.
Overloading and overriding are two types of polymorphism .
dynamic method binding.
Assume that three subclasses (Cow, Dog and Snake)
have been created based on the Animal abstract class,
each having their own speak() method.
Notice that although each method reference was to an
Animal (but no animal objects exist), the program is able to
resolve the correct method related to the subclass object
at runtime. This is known as dynamic (or late) method
binding.
37
© Beihang University
Polymorphism Example
public class AnimalReference{
public static void main(String args[])
Animal ref; // set up var for an Animal
Cow aCow = new Cow("Bossy"); // makes specific objects
Dog aDog = new Dog("Rover");
Snake aSnake = new Snake("Earnie");
// now reference each as an Animal
ref = aCow;
ref.speak();
ref = aDog;
ref.speak();
ref = aSnake;
ref.speak();
}
38
© Beihang University
Object class
Some methods can be overriding in the subclass of
Object
clone
equals
finalize
toString
39
© Beihang University
Object clone
aCloneableObject.clone();
aCloneableObject must implement Cloneable interface
Object.clone() is shallow copy, and not is deep copy
40
© Beihang University
Object.clone
41
© Beihang University
Object.toString()
Return the string of an object representation .
E.g.,:
System.out.println(Thread.currentThread().toString());
Thread[main,5,main]
42
© Beihang University
Hashcode & equal
43
© Beihang University
Preparation
Package
Static
Abstract, Interface
44
© Beihang University
Following Chapters
Chapter 5
Essential JAVA Language
45
© Beihang University
46
© Beihang University