|
《Java就业培训教程》P81源码 class Compare { public static void main(String[] args) { String str1 = new String("abc"); String str2 = new String("abc"); String str3 = str1; if(str1==str2) System.out.println("str1==str2"); else System.out.println("str1!=str2"); if(str1==str3) System.out.println("str1==str3"); else System.out.println("str1!=str3"); } }
《Java就业培训教程》P82源码 class Compare { public static void main(String[] args) { String str1 = new String("abc"); String str2 = new String("abc"); String str3 = str1; if(str1.equals(str2)) System.out.println("str1 equal str2"); else System.out.println("str1 not equal str2"); if(str1.equals(str3)) System.out.println("str1 equal str3"); else System.out.println("str1 not equal str3"); } }
《Java就业培训教程》P86源码 class Person { private int age; public void setAge(int i) { if(i<0 || i>130) return; age = i; } public int getAge() { return age; } } public class TestPerson { public static void main(String args[]) { Person p1 = new Person(); p1.setAge(3); p1.setAge(-6); System.out.println(p1.getAge()); } }
《Java就业培训教程》P88源码 class Person { public Person() { System.out.println("the constructor 1 is calling!"); } private int age = 10; public void shout() { System.out.println("age is "+age); } } class TestPerson { public static void main(String[] args) { Person p1=new Person(); p1.shout(); Person p2=new Person(); p2.shout(); Person p3=new Person(); p3.shout(); } }
《Java就业培训教程》P90源码 class Person { private String name="unknown"; private int age = -1; public Person() { System.out.println("constructor1 is calling"); } public Person(String n) { name = n; System.out.println("constructor2 is calling"); System.out.println("name is "+name); } public Person(String n,int a) { name = n; age = a; System.out.println("constructor3 is calling"); System.out.println("name and age is "+name+";"+age); } public void shout() { System.out.println("listen to me!!"); } } class TestPerson { public static void main(String[] args) { Person p1=new Person(); P1.shout(); Person p2=new Person("Jack"); P2.shout(); Person p3=new Person("Tom",18); P3.shout(); } } 《Java就业培训教程》P94源码 class Person { private Person() { System.out.println("the constructor 1 is calling!"); } } class TestPerson { public static void main(String[] args) { Person p1=new Person(); } } 《Java就业培训教程》P95源码 class A { String name; public A(String x) { name = x; } public void func1() { System.out.println("func1 of " + name +" is calling"); } public void func2() { A a2 = new A("a2"); a2.func1(); } } class TestA { public static void main(String [] args) { A a1 = new A("a1"); a1.func2(); } } 《Java就业培训教程》P96源码 class A { String name; public A(String x) { name = x; } public void func1() { System.out.println("func1 of " + name +" is calling"); } public void func2() { A a2 = new A("a2"); this.func1();//使用this关键字调用func1方法 a2.func1(); } } 《Java就业培训教程》P99源码 class Container { Component comp; public void addComponent() { comp = new Component(this);//将this作为对象引用传递 } } class Component { Container myContainer; public Component(Container c) { myContainer = c; } } 《Java就业培训教程》P100源码 public class Person { String name; int age; public Person(String name) { this.name = name; } public Person(String name,int age) { this(name); this.age = age; } } 《Java就业培训教程》P101源码 class Person { public void finalize() { System.out.println("the object is going!"); } public static void main(String [] args) { new Person(); new Person(); new Person(); System.out.println("the program is ending!"); } } 《Java就业培训教程》P103源码 class PassValue { public static void main(String [] args) { int x = 5; change(x); System.out.println(x); } public static void change(int x) { x = 3; } } class PassRef { int x ; public static void main(String [] args) { PassRef obj = new PassRef(); obj.x = 5; change(obj); System.out.println(obj.x); } public static void change(PassRef obj) { obj.x=3; } }
《Java就业培训教程》P108源码 class Chinese { static String country="中国"; String name; int age; void singOurCountry() { System.out.println("啊!,亲爱的" + country); //类中的成员方法也可以直接访问静态成员变量 } } class TestChinese { public Static void main(String [] args) { System.out.println("Chinese country is " + Chinese.country); //上面的程序代码直接使用了"类名.成员"的格式 Chinese ch1 = new Chinese(); System.out.println("Chines country is " + ch1.country); //上面的程序代码直接使用了"对象名.成员"的格式 ch1.singOurCountry(); } } 《Java就业培训教程》P111源码 class StaticCode { static String country; static { country = "china"; System.out.println("StaticCode is loading"); } } class TestStaticCode { static { System.out.println("TestStaticCode is loading"); } public static void main(String [] args) { System.out.println("begin executing main method"); new StaticCode(); new StaticCode(); } } 《Java就业培训教程》P115源码 class Outer { int outer_i = 100; void test() { Inner in = new Inner(); in.display(); } class Inner { void display() { System.out.println("display: outer_i = " + outer_i); } } } class InnerClassDemo { public static void main(String[] args) { Outer outer = new Outer(); outer.test(); } }
|