命名规范:使用与类相同的名称
分类
注意点
构造器重载
规则
<!--比如,树作为类,有树苗和百年古树,百年古树则使用有参的构造方法,参数为树高;树苗则使用无参的构造方法,因为一般没有高度属性-->
注意点
规则
如果传入的参数类型大于重载方法声明的类型,则窄化转换
public class TestOverride {
void f1(int f){
System.out.println(getType(f));
System.out.println(f);
}
public static String getType(Object object){
String typeName=object.getClass().getName();
int length= typeName.lastIndexOf(".");
String type =typeName.substring(length+1);
return type;
}
public static void main(String[] args) {
TestOverride testOverride = new TestOverride();
testOverride.f1(100);
// 窄化转换
testOverride.f1((int) 3000.08);
}
}/* output
Integer
100
Integer
3000
*/
如果传入的参数类型小于重载方法声明的类型,则提升数据类型
public class TestOverride {
void f1(double f){
System.out.println(getType(f));
System.out.println(f);
}
public static String getType(Object object){
String typeName=object.getClass().getName();
int length= typeName.lastIndexOf(".");
String type =typeName.substring(length+1);
return type;
}
public static void main(String[] args) {
TestOverride testOverride = new TestOverride();
testOverride.f1(100);
testOverride.f1(3000.08);
}
}/* output
Double
100.0
Double
3000.08
*/
无法使用,原因是如果不获取返回值,仅仅使用f()方法,是无法区分的
规则
用途
规则
规则
Test
class TestThis {
String s = "initial value";
int i;
// 有参构造器
TestThis(String s,int i){
System.out.println("Build s and i");
}
// 无参构造器使用this调用
TestThis(){
this("This",32);
}
public static void main(String[] args) {
TestThis testThis = new TestThis();
}
}
用途
实现不创建对象的前提下,通过类本身来调用static方法
规则
Finalize工作原理
注意点
回收流程
垃圾回收只与内存有关,java虚拟机未出现内存耗尽的情况时,不会浪费内存来执行垃圾回收
finalize不是进行清理的合理方法,finalize调用存在不确定性,可能还未执行就已经被回收
在JDK9中,finalize已经标记为过时
缺点
当循环引用时,始终无法释放内存
<!--ab对象皆为null,但是由于循环引用,无法实现垃圾回收-->
Class TestA{
public TestB b;
}
Class TestB{
public TestA a;
}
public static void main(String[] args){
TestA a = new TestA();
TestB b = new TestB();
a.b = b;
b.a = a;
a = null;
b = null;
}
流程
停止-复制——标记-清扫
缺点
规则
方法
自动初始化将在构造器被调用之前发生,i先置为0,然后赋值为7
Class Counter{
int i;
Counter(){
i = 7;
}
}
静态初始化时间
public class Car {
// 两个静态字符串域,一个在定义处初始化,另一个在静态块中初始化
static String string1 = "defInBegin";
static String string2;
static {
string2 = "defInStatic";
print("显式的静态初始化");
}
static void printMed(){
print(string1);
print(string2);
}
Car(){
print("Car()");
}
public static void main(String[] args) {
// 首次生成对象
new Car();
}
}/* output
显式的静态初始化
Car()
*/
与静态初始化区别
使用方法
// 定义
int[] i;
// 固定成员
i.length;
// 数组大小
// 初始化时定义
int[] a = new int[10];
// 由随机数决定
Random rand = new Random(57);
int[] b = new int[rand.nextInt(20)];
// 初始化
// 方法一
a[i] = rand.nextInt(20);
// 方法二
Integer[] a = {
new Integer(1),
new Integer(2),
3,
};
// 方法三
Integer[] b = new Integer[]{
new Integer(1),
new Integer(2),
3,
}
注意
创建类的对象引用数组,并不会调用类的构造方法,原因是仅实例化了数组,并没有实例化类
public class Book {
Book(String s){
print("Initial");
print(s);
}
public static void main(String[] args) {
Book[] books = new Book[10];
}
}/* output
为空,因为并未实例化类,仅仅实例化了数组
*/
方法
实现
public class TestObjectArray {
static void printArray(Object[] args){
for (Object s : args){
System.out.println(s);
}
}
// 方法二
static void f(int... args){
System.out.println(args.length);
}
public static void main(String[] args) {
// 方法一
printArray(new Object[]{
new Integer(35),new Double(23.34),new String("dsaf")
});
f(2,4,6,4);
}
}/* output
35
23.34
dsaf
4
*/
使用方法
public enum MoneyValue{
ONE, FIVE, TEN, TWENTY, FIFTY, HUNDRED;
}
enum和switch搭配使用
package TwentyTwo;
public class Money {
public enum MoneyValue{
ONE, FIVE, TEN, TWENTY, FIFTY, HUNDRED;
}
public static void main(String[] args) {
for (MoneyValue mv: MoneyValue.values()
) {
switch (mv){
case ONE:
System.out.println("This is smallest");
System.out.println(mv.ordinal());
break;
case FIVE:
System.out.println("This is second");
break;
case TEN:
System.out.println("This is third");
break;
case TWENTY:
System.out.println("This is forth");
break;
case FIFTY:
System.out.println("This is fifth");
break;
case HUNDRED:
System.out.println("This is largest");
break;
}
}
}
}
在未加入枚举之前,常量合集创建方式;加入后的创建方式,枚举更加简单安全
public class Test {
public static final int A = 1;
public static final int B = 2;
public static final int C = 3;
public static final int D = 4;
public static final int E = 5;
}
public class Test {
public enum Grade{
A,B,C,D,E;
};
}