More on Classes
从方法中return
在void修饰的方法里也可以出现return,但是是用来从方法中退出的,而非返回一个值
return;
当返回一个对象的时候,应返回符合返回类型的对象,也可以返回这一类的子类
使用this关键词
在方法和构建器里使用this指代当前的对象
使用的原因:
- a field is shadowed by a method or constructor parameter.如下示例。
public class Point { public int x = 0; public int y = 0; //constructor public Point(int x, int y) { this.x = x; this.y = y; } }
- 在构造器里使用this()来指代另一个构造器,这称为显示构造器调用,也是种复用代码的方法
public class Rectangle { private int x, y; private int width, height; public Rectangle() { this(0, 0, 1, 1); } public Rectangle(int width, int height) { this(0, 0, width, height); } public Rectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } }
对类的成员的访问权限
官方文档: Controlling Access to Members of a Class.
理解类方法 类字段 常量
某些时候,想要一个所有对象都一样通用的变量,每个实例共享一个类字段,该字段位于一个固定的内存地址;与类直接联系,而非对象,虽然也可以用对象访问类字段 类方法,但这样体现不出来static修饰的作用。
举个例子,可以搞一个静态字段static int i;每次构造器初始化对象的时候就++i,来记录类的对象数(包含子类)
常量可以是static final 这两个一起来修饰,final说明不会改变。
static final double PI = 3.141592653589793;
初始化字段
当然可以直接在声明的时候直接赋值,但是也许初始化需要一些复杂点的逻辑,如异常处理或者循环操作。
- 类字段可以用static initialization blocks如下所示,也可以用一个private类方法来初始化(可以在之后复用),方法的名字无所谓,毕竟不是构建器。
放在哪,当运行到该处就被执行static { // whatever code is needed for initialization goes here }
- 实例变量可以用构造器初始化,也可以用Initializer blocks
放在哪,当运行到该处就被执行{ // whatever code is needed for initialization goes here }
Questions and Exercises: Classes
Consider the following class:
public class IdentifyMyParts {
public static int x = 7;
public int y = 3;
}
-
What are the class variables?
x -
What are the instance variables?
y -
What is the output from the following code:
IdentifyMyParts a = new IdentifyMyParts(); IdentifyMyParts b = new IdentifyMyParts(); a.y = 5; b.y = 6; a.x = 1; b.x = 2; System.out.println("a.y = " + a.y); System.out.println("b.y = " + b.y); System.out.println("a.x = " + a.x); System.out.println("b.x = " + b.x); System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x); //output: // a.y = 5 // b.y = 6 // a.x = 2 // b.x = 2
Exercises
1.
package practice;
public class Practice {
public static void main(String[] args) {
Card card = new Card('1',"Peach");
System.out.println(card);
}
}
class Card {
public char rank;
public String suit;
public Card(char rank, String suit) {
this.rank = rank;
this.suit = suit;
}
}
2
package practice;
public class Practice {
public static void main(String[] args) {
Cards cards = new Cards();
System.out.println(cards);
}
}
class Card {
public String rank;
public String suit;
public Card(String rank, String suit) {
this.rank = rank;
this.suit = suit;
}
}
class Cards {
public static Card[][] fullCards;//这里只是声明,将fullcards这个变量名,与Card类对象关联起来
static final String[] RANK = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
static final String[] SUIT = {"Heart","Spade","Diamond","Club"};
public Cards() {
fullCards = new Card[12][4];//这里理解还是有问题,漏了这一句,这句是实例化,而不是初始化,Caed[12][4]也不是调用方法,而是实例化,是new的作用,分配内存并返回一个句柄,这不跟C指针和数组一样嘛,malloc
for(int i=0;i<12;i++) {
for(int j =0;j<4;j++) {
fullCards[i][j] = new Card(RANK[i],SUIT[j]);//这里其实是初始化,或者说是赋值,值是new出来的Card对象的句柄
}
}
}
}