1.接口 In the abstract class, essentially, the abstract idea is the define the standard of the interface. To make sure all the subclass has the same achievement of the interface. IF an abstract class does not has any value but all the abstract methods. We will change the abstract class into an interface. Some key tips: the interface will use “extend” to inherit another interface. One class can inherit from multiple interfaces. 非常重要: another important characteristic of the interface is the default , this method can allow us to add a new method without revising his subclasses.
2.Static Instance field will have its own space in each instance, but the static field will share the sharing space for all the instances.
public class Main {
public static void main(String[] args) {
Person ming = new Person("Xiao Ming", 12);
Person hong = new Person("Xiao Hong", 15);
ming.number = 88;
System.out.println(hong.number);
hong.number = 99;
System.out.println(ming.number);
}
}
class Person {
public String name;
public int age;
public static int number;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
result:
88
99
2.静态方法 静态方法属于class而不属于实例,因此在静态方法内部,无法访问this变量也无法访问实例field 静态方法常用于工具类: Arrays.sort();Math.random()
注意!!! Because the interface is a pure abstract class, so it can not define the instance field. However, interface can have stactic field . However, the field should be and only be "public static final". 关于final的用法
1、final类不能被继承,因此final类的成员方法没有机会被覆盖,默认都是final的。在设计类时候,如果这个类不需要有子类,类的实现细节不允许改变,并且确信这个类不会再被扩展,那么就设计为final类。 final方法不能被子类的方法覆盖,但可以被继承。 注意:final类中的成员变量可以根据需要设为final,但是要注意final类中的所有成员方法都会被隐式地指定为final方法。
2.final方法 如果一个类不允许其子类覆盖某个方法,则可以把这个方法声明为final方法。 使用final方法的原因有二: 第一、把方法锁定,防止任何继承类修改它的意义和实现。 第二、高效。编译器在遇到调用final方法时候会转入内嵌机制,大大提高执 行效率。(已经被淘汰了) 3.对于一个final变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改;如果是引用类型的变量,则在对其初始化之后便不能再让其指向另一个对象。 比较复杂,例子参见:https://www.cnblogs.com/dolphin0520/p/3736238.html
package Test_内存;
public class TestMem {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
String s4 = s1+s2;
System.out.println(s3==s4);
System.out.println(s3=="hello"+"world");
System.out.println(s3.hashCode());
System.out.println(s4.hashCode());
System.out.println(System.identityHashCode(s3));
System.out.println(System.identityHashCode(s4));
System.out.println(System.identityHashCode("helloworld"));
}
}
false
true
-1524582912
-1524582912
305808283
2111991224
305808283
|