目录
8. Accessors, Mutators, Static methods, Static methods in Diver class
1. Object
Def ( big java p25): objects are entities in the program that are manipulated by calling methods.
An object is characterized by its state and behavior.
2. Classes
Def ( barron p93): a class is a software blueprint for implementing objects of a given type.
An object is a single instance of the class
3. Methods
Def ( barron p93): the methods of the class provide both the behaviors exhibited by the object and the operations that manipulate the object.
Combining an object's data and methods into a single unit called a class is known as encapsulation.
4. Varible
Def ( big java p26): a varible is a storage location in a computer program.
5. Public and Private ( barron p94 )
public -- usable for all programs
private -- usable only in its package
6. Static ( barron p94 )
A static variable contains a value that is shared by all instances of the class.
Static -- memory allocation happens once
final -- constant / = the value can not be changed
7. Constructor
参考:Default Constructor in Java – Class Constructor Example
A constructor creates an object of the class.
always have the same name with the class
- no return type
- default constructor
no-argument constructor:
public class library {
String bookname;
int ISBN;
//library constructor
//no-argument constructor
public library(){
bookname="Harry Potter";
ISBN=123;
}
public static void main(String args[]){
library a = new library();
System.out.println(a.ISBN);
}
}
输出结果:
parameterized coonstructor:
(1) without this
public class library {
String bookname;
int ISBN;
//library constructor
//parameterized constructor
public library(String bookname, int ISBN){
bookname=bookname;
ISBN=ISBN;
}
public static void main(String args[]){
library a = new library("Harry Potter", 123);
System.out.println(a.ISBN);
}
}
输出结果:
(2) with this
public class library {
String bookname;
int ISBN;
//library constructor
//parameterized constructor
public library(String bookname, int ISBN){
this.bookname=bookname;
this.ISBN=ISBN;
}
public static void main(String args[]){
library a = new library("Harry Potter", 123);
System.out.println(a.ISBN);
}
}
输出结果:
this is a key word refers to the current object in a method or constructor
8. Accessors, Mutators, Static methods, Static methods in Diver class (barron p96,97)
accessors -- An accessor meMutatorsthod accesses a class object without altering the object.
mutators -- A mutator method changes the state of an object by modifying at least one of its in stance variables.
static methods -- A method that performs an operation for the entire class, not its individual objects.
Static methods in Diver class -- often a class that contains the main() method is used as a driver program to test other classes.
9. method overloading (barron p99)
overloaded methods are two or more methods in the same class that have the same name but different parameter lists.
10. instance variable (在C++中称之为member variable)
- 类里面的方法
-
Instance variables are declared in a class, but outside a method, constructor or any block.