职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 1495|回复: 0

张孝祥 -- 《Java就业培训教程》书中原码 3章

[复制链接]
weisheng 发表于 2006-11-14 07:20 | 显示全部楼层 |阅读模式
《Java就业培训教程》P81的Java原代码
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的Java原代码
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的Java原代码
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的Java原代码
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的Java原代码
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的Java原代码
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的Java原代码
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的Java原代码
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的Java原代码
class Container
{
        Component comp;
        public void addComponent()
        {
                comp = new Component(this);//将this作为对象引用传递
        }
}
class Component
{
        Container myContainer;
        public Component(Container c)
        {
                myContainer = c;
        }
}
《Java就业培训教程》P100的Java原代码
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的Java原代码
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的Java原代码
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的Java原代码
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的Java原代码
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的Java原代码
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();
        }
}
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-5-11 17:13 , Processed in 0.125211 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表