类和对象
教材内容
1: Introduction to Objects
2:Everything is an Object 2: Everything is an Object
g g
3: Controlling Program Flow 4: Initialization & Cleanup
R dM RoadMap
Class / Object / Instances
Object Creation
Object Creation
Object Initialization
Object Delete
Object Compare
Object Compare
Final keyword
St ti k d
Static keyword
Main method
l class
Objects, Instances, and Classes
I t V i bl Instance Variables
L l V i bl Local Variables
R dM RoadMap
Class / Object / Instances
Object Creation
Object Creation
Object Initialization
Object Delete
Object Compare
Object Compare
Final keyword
St ti k d
Static keyword
Main method
About Object Creation
Ab t Obj t C ti
Distinguish object instance and object reference
Obj t R f Object Reference
使用关键字new
String s = new String(“asdf”);
Object Instances vs. Object Reference
通过句柄操纵对象
句柄不等于对象
句柄保存在堆栈里面, 对象保存到堆里面
对于句柄,一种安全的方法是创建一个句柄时就进行初始化
C t t
Constructors
A class is instantiated by "new" and initialized by invoking
the constructor:
the constructor:
Student c = new Student();
Constructors always have the same name as the class
Constructors always have the same name as the class, have no return a type, but they may vary in the number and type of parameters they accept:
yp p y p
Student c = new Student();
Student c = new Student(" Mr. Smith");
Student c = new Student(" Mr. Smith", 47);
Student c = new Student( 6002372, 47);
C t t Constructors
class Rock {
Rock() { // This is the constructor
Rock(){//This is the constructor
System.out.println("Creating Rock");
}
}
public class SimpleConstructor {
public class SimpleConstructor{
public static void main(String[] args) {
for(int i = 0; i < 10; i++)
R k()
new Rock();
}
}
D f lt C t t
Default Constructor
If you do not write a constructor then the compiler creates one for you (the default constructo r) which
takes no parameters and does nothing to the
instance variables.An Example:
class Bird {
int i;
}
public class DefaultConstructor {
p(g[]g){
public static void main(String[] args) {
Bird nc = new Bird(); // default!
}
}
O l di
Overloading
What is overloading?
String substring( int beginIndex) String substring( int beginIndex, int endIndex) Constructors are frequently overloaded, for example, q y ,p ,Student:
Student()
Student( String name)
Student( String name, int age)Student(int studentNumber Student( int studentNumber
A E l f Cl An Example of Class
Using the Exam Class
U i th E Cl
Once defined and compiled the class can be used:
be used:
Exam e1 = new Exam();
Exam e2 = new Exam(" VAJ");
Exam e2=new Exam("VAJ");
e1. Score();
String[] testQuestions = e2. getQuestions();
St i[]t tQ ti2tQ ti()
W rappers
A Wrapper Example: Integer Class
Transforming int / Integer / String
There are two constructors and a class method:
I t(St i)//St i t I t
Integer( String s); // String to Integer
Integer( int i); // primitive to Integer
Integer. valueOf( 4); // String to Integer
Integer valueOf("4");//String to Integer
It is easy to convert to and from a primitive int: int i =4;;
Integer objInt = new Integer( i);
int j = objInt. intValue(); // Integer to primitive
String s = objInt. toString(); // Integer to String
int k = Integer. parseInt(" 4"); // String to primitive