1 Using a class
- The operator “.” is called the member access operator
- String is a kind of object and has its own methods, feo example
// Return character value at the index
public char charAt(int index);
// Return the length of the string
public int length();
// Return a new string with the string argument(concatenating the string in the() // to the back of the string before "." )
public String concat(String str);
被static修饰的method可以直接通过classname.methodname()来调用,之前不需要创建实例对象(object),他们在整个程序运行开始时就被分配了内存,直到程序结束时才被释放;但是无static修饰的method只能在通过已经 创建的object来使用。
2 Defining a Class
- Access Midifiers: private, public 这种就是access modifier
- class中的method想要访问class中private variable是不需要传递参数的
- private variable虽然不能直接访问但是可以通过public method来访问
public class PersonInfo {
private int numKids; // private variable
// following 3 methods directly change the value of numKids without passing it into the method
public void setNumKids(int setPersonsKids) {
numKids = setPersonsKids;
}
public void incNumKids() {
numKids = numKids + 1;
}
public int getNumKids() {
return numKids;
}
3 Mutator, accessors, and private helper
mutator和accessor是以某种method在class中发挥的作用来决定的:
- mutator:改变class中的value
- accessor:获得class中的value
private helper:帮助public method完成task的method,users不能访问
4 Initialization and constructors
Initialization:
- class中的field就是变量,field initialization 就是在class中对变量初始化
- initiaization 和 decalration 不一样,initializaion必须要给它赋值
Constructors:
- 是一个和class同名且无返回值的public method,是用来在create class的时候进行一些操作的
public class Restaurant {
private String name;
private int rating;
public Restaurant() { // Default constructor
name = "NoName"; // Default name: NoName indicates name was not set
rating = -1; // Default rating: -1 indicates rating was not set
}
public void setName(String restaurantName) {
name = restaurantName;
}
public void setRating(int userRating) {
rating = userRating;
}
public void print() {
System.out.println(name + " -- " + rating);
}
}
- 一个class可以有多个constructor但是不能是一样的argument不然complier不能分辨他们, 若constructor无arguments,叫做default constructor
- create一个object的时候会自动的调用constructor,具体调用哪一个根据传入的变量区分,这叫做 constructor overloading
5 Constructor Overloading
当programmer主动定义constructor时,complier不再自动创建defualt constructor;declare class的时候一定要按照构建好的constructor
6 The ‘this’ implicit parameter
this在java中有两个作用:
区分class中的 local number 和 外部输入parameter(如果两者重名);
public class CablePlan {
private int numDays;
// FIXME: Define setNumDays() method, using "this" implicit parameter.
public void setNumDays(int numDays) {
// 这里就是使用this来标志local member
this.numDays = numDays;
}
public int getNumDays() {
return numDays;
}
}
第二是在defualt constructor 中重新调用overload constructor里面的赋值步骤
public class ElapsedTime {
private int hours;
private int minutes;
// Overloaded constructor definition
public ElapsedTime(int timeHours, int timeMins) {
hours = timeHours;
minutes = timeMins;
}
// Default constructor definition
public ElapsedTime() {
this(0, 0);
}
9 Static fields and methods
-
class中所有的static class member 在运行整个程序的时候就会被加载,不需要new ClassName()来创建,所以可以通过ClassName.method()或者ClassName.variable来访问;static variable又叫做 class variables
-
非static class mebmer 只有在new 创建object的时候才会被加载进入内存,只能通过ObjectName.method()和ObjectName.variable来访问;非static variable又叫做 instance variable
-
对所有同一类的object来说,他们公用static field,包括其中variable和method;
-
static method 是不可以访问 non static field的,因为non static field随着object存在而存在,可能根本没有
10 Classes and ArrayList
ArrayList 使用注意事项
import java.util.ArrayList // the class must be imported
ArrayList<ClassName> ArrayListName = new ArrayList<ClassName>(); // how to declare and initialize an ArrayList
ArrayListName.add(ClassObject); // 添加一个 ClassName 类型的 object