计算机毕竟是死的,虽然现在的语言已经为开发人员提供了足够的功能几近于变成傻瓜式编程。但是个人认为,了解代码在计算机内部的运行过程对程序员是很有帮助的。下面发个测试代码。
//父类:
public class Person {
public Person() {
System.out.println( "父类无参数构造器...........6");
}
public Person(String name) {
this();
System.out.println( "父类有参数构造器...........7");
}
public static void main(String[] args) {
System.out.println( "父类main方法.............8");
} //
//static关键字+“{ 游离块 }”
static {
System.out.println( "父类静态游离块...........9");
}
{
System.out.println( "父类一般游离块...........10");
}
}
public class Person {
public Person() {
System.out.println( "父类无参数构造器...........6");
}
public Person(String name) {
this();
System.out.println( "父类有参数构造器...........7");
}
public static void main(String[] args) {
System.out.println( "父类main方法.............8");
} //
//static关键字+“{ 游离块 }”
static {
System.out.println( "父类静态游离块...........9");
}
{
System.out.println( "父类一般游离块...........10");
}
}
//子类:
public class Student extends Person {
static String name; //private String name;
public Student() {
super("");
System.out.println( "子类无参数构造器..........1");
}
public Student(String name) {
this();
this.name = name;
System.out.println( "子类有参数构造器...........2");
}
public static void main(String[] args) {
Student s = new Student( "zhangsan");
System.out.println( "子类main方法...........3");
} //
//static关键字+“{ 游离块 }”
static {
System.out.println( "子类静态游离块...........4");
} //静态游离块只被执行一次;
{
System.out.println( "子类一般游离块...........5");
}
}
public class Student extends Person {
static String name; //private String name;
public Student() {
super("");
System.out.println( "子类无参数构造器..........1");
}
public Student(String name) {
this();
this.name = name;
System.out.println( "子类有参数构造器...........2");
}
public static void main(String[] args) {
Student s = new Student( "zhangsan");
System.out.println( "子类main方法...........3");
} //
//static关键字+“{ 游离块 }”
static {
System.out.println( "子类静态游离块...........4");
} //静态游离块只被执行一次;
{
System.out.println( "子类一般游离块...........5");
}
}
“== and equals”:
“==”比较对象的内存地址;简单类型内容;
“equals”比较对象的内容
public
class TestEquals {
public int id;
public TestEquals( int id) {
this.id = id;
}
//重写equals方法
public boolean equals(Object obj) {
if(obj == null) {
return false;
}
if( this == obj) {
return true;
}
if( this.getClass() != obj.getClass()) {
return false;
}
TestEquals s = (TestEquals)obj;
return this.id == s.id;
}
}
class Test {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
// Student s2 = s1;
System.out.println(s1.equals(s2));
//封装
Integer a = new Integer(2);
Integer b = new Integer(4);
System.out.println(a.equals(b));
//简单
int x = 3;
int y = 3;
System.out.println(x == y);
} //
}
public int id;
public TestEquals( int id) {
this.id = id;
}
//重写equals方法
public boolean equals(Object obj) {
if(obj == null) {
return false;
}
if( this == obj) {
return true;
}
if( this.getClass() != obj.getClass()) {
return false;
}
TestEquals s = (TestEquals)obj;
return this.id == s.id;
}
}
class Test {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
// Student s2 = s1;
System.out.println(s1.equals(s2));
//封装
Integer a = new Integer(2);
Integer b = new Integer(4);
System.out.println(a.equals(b));
//简单
int x = 3;
int y = 3;
System.out.println(x == y);
} //
}
转载于:https://blog.51cto.com/java0note/104093