📄 sourcecode.txt
字号:
{
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();
}
}
--------------------------------------------------------------------------------
《Java就业培训教程》 作者:张孝祥 书中原代码
《Java就业培训教程》P127的Java原代码
程序清单:Student.java
class Person
{
public String name;
public int age;
public Person(String name,int age)
{
this.name=name;
this.age=age;
}
public Person() //如果不写这个构造函数,看看对类Student有什么影响。
{
}
public void getInfo()
{
System.out.println(name);
System.out.println(age);
}
}
class Student extends Person
{
public void study()
{
System.out.println("Studding");
}
public static void main(String[] args)
{
Person p=new Person();
p.name="person";
p.age=30;
p.getInfo();
Student s=new Student();
s.name="student";
s.age=16;
s.getInfo();
s.study();
}
}
《Java就业培训教程》P135的Java原代码
interface Animal extends Runner
{
void breathe();
}
class Fish implements Animal
{
public void run()
{
System.out.println("fish is swimming");
}
public void breathe()
{
System.out.println("fish is bubbling");
}
}
abstract LandAnimal implements Animal
{
public void breathe()
{
System.out.println("LandAnimal is breathing");
}
}
《Java就业培训教程》P138的Java原代码
程序清单:C.java
class A
{
public void func1()
{
System.out.println("A func1 is calling");
}
public void func2()
{
func1();
}
}
class B extends A
{
public void func1()
{
System.out.println("B func1 is calling");
}
public void func3()
{
System.out.println("B func3 is calling");
}
}
class C
{
public static void main(String [] args)
{
B b=new B();
A a = b;
callA(a);
callA(new B());
}
public static void callA(A a)
{
a.func1();
a.func2();
}
}
《Java就业培训教程》P141的Java原代码
程序清单:Student.java
class Student
{
String name;
int age;
boolean equals(Object obj)
{
Student st=null;
if(obj instanceof Student)
st = (Student)obj;
else
return false;
if(st.name==this.name && st.age==this.age)
return true;
else
return false;
}
public static void main(String[] args)
{
Student p=new Student();
Student q=new Student();
p.name="xyz";
p.age=13;
q.name="xyz";
q.age=13;
if(p.equals(q))
System.out.println("p 与 q 相等");
else
System.out.println("p 与 q 不等");
}
}
《Java就业培训教程》P144的Java原代码
程序清单:Interface.java
interface PCI
{
void start();
void stop();
}
class NetworkCard implements PCI
{
public void start()
{
System.out.println("Send ...");
}
public void stop()
{
System.out.println("Network Stop.");
}
}
class SoundCard implements PCI
{
public void start()
{
System.out.println("Du du...");
}
public void stop()
{
System.out.println("Sound Stop.");
}
}
class MainBoard
{
public void usePCICard(PCI p)
{
p.start();
p.stop();
}
}
class Assembler
{
public static void main(String [] args)
{
MainBoard mb=new MainBoard();
NetworkCard nc=new NetworkCard();
mb.usePCICard(nc);
SoundCard sc=new SoundCard();
mb.usePCICard(sc);
}
}
《Java就业培训教程》P149的Java原代码
public class TestException
{
public static void main(String [] args)
{
try
{
int reslut = new Test().devide( 3, 0 );
System.out.println("the result is" + reslut );
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
System.out.println("program is running here ,that is normal !");
}
}
class Test
{
public int devide(int x, int y)
{
int result = x/y;
return x/y;
}
}
《Java就业培训教程》P154的Java原代码
public class TestException
{
public static void main(String [] args)
{
try
{
int result = new Test().devide( 3, 0 );
//int result = new Test().devide( 3, -1 );
//int result = new Test().devide( 3, 1 );
System.out.println("the result is " + result );
}
catch(DevideByMinusException e)
{
System.out.println("program is running into"+
"DevideByMinusException");
System.out.println(e.getMessage());
System.out.println("the devisor is " +
e. getDevisor());
}
catch(ArithmeticException e)
{
System.out.println("program is running into"+
"DevideByMinusException");
System.out.println(e.getMessage());
}
catch(Exception e)
{
System.out.println("program is running into"+
"other unknowned Exception");
System.out.println(e.getMessage());
}
System.out.println("program is running here ,that is normal !");
}
}
《Java就业培训教程》P158的Java原代码
package org.it315;
public class TestPackage
{
public static void main(String [] args)
{
new Test().print();
}
}
class Test
{
public void print()
{
System.out.println("the program is demostrating how to using package!");
}
}
--------------------------------------------------------------------------------
《Java就业培训教程》 作者:张孝祥 书中的Java原代码
《Java就业培训教程》P177的Java原代码
程序清单:ThreadDemo1.java
public class ThreadDemo1
{
public static void main(String args[])
{
new TestThread().run();
while(true)
{
System.out.println("main thread is running");
}
}
}
class TestThread
{
public void run()
{
while(true)
{
System.out.println(Thread.currentThread().getName() +
" is running");
}
}
}
《Java就业培训教程》P180的Java原代码
程序清单:ThreadDemo3.java
public class ThreadDemo3
{
public static void main(String args[])
{
//new TestThread ().start();
TestThread tt= new TestThread();//创建TestThread类的一个实例
Thread t= new Thread(tt);//创建一个Thread类的实例
t.start();//使线程进入Runnable状态
while(true)
{
System.out.println("main thread is running");
}
}
}
class TestThread implements Runnable //extends Thread
{
public void run()//线程的代码段,当执行start()时,线程从此出开始执行
{
while(true)
{
System.out.println(Thread.currentThread().getName() +
" is running");
}
}
}
《Java就业培训教程》P181的Java原代码
程序清单:ThreadDemo4.java
public class ThreadDemo4
{
public static void main(String [] args)
{
ThreadTest t=new ThreadTest();
t.start();
t.start();
t.start();
t.start();
}
}
class ThreadTest extends Thread
{
private int tickets=100;
public void run()
{
while(true)
{
if(tickets>0)
System.out.println(Thread.currentThread().getName() +
" is saling ticket " + tickets--);
}
}
}
《Java就业培训教程》P186的Java原代码
程序清单:JoinThread.java
public class JoinThread
{
public static void main(String[] args)
{
ThreadTest t=new ThreadTest();
Thread pp=new Thread(t);
pp.start();
int i=0;
while(true)
{
if(i==100)
{
try
{
pp.join();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
System.out.println("main Thread "+i++);
}
}
}
class ThreadTest implements Runnable
{
public void run()
{
String str=new String();
int i=0;
while(true)
{
System.out.println(Thread.currentThread().getName()+" "+i++);
}
}
}
《Java就业培训教程》P195的Java原代码
程序清单:ThreadDemo6.java
public class ThreadDemo6
{
public static void main(String [] args)
{
ThreadTest t=new ThreadTest();
new Thread(t).start();//这个线程调用同步代码块
t.str=new String("method");
new Thread(t).start();//这个线程调用同步函数
}
}
class ThreadTest implements Runnable
{
private int tickets=100;
String str = new String ("");
public void run()
{
if(str.equals("method"))
{
while(true)
{
sale();
}
}
else
{
while(true)
{
synchronized(str)
{
if(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
System.out.println(Thread.currentThread().getName()+
" is saling ticket " + tickets--);
}
}
}
}
}
public synchronized void sale()
{
if(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
System.out.println(Thread.currentThread().getName()+
" is saling ticket " + tickets--);
}
}
}
《Java就业培训教程》P200的Java原代码
程序清单:Deadlock.java
class A
{
synchronized void foo(B b)
{
String name=Thread.currentThread().getName();
System.out.println(name+ " entered A.foo ");
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
System.out.println(name+ " trying to call B.last()");
b.last();
}
synchronized void last()
{
System.out.println("inside A.last");
}
}
class B
{
synchronized void bar(A a)
{
String name=Thread.currentThread().getName();
System.out.println(name + " entered B.bar");
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
System.out.println(name + " trying to call A.last()");
a.last();
}
synchronized void last()
{
System.out.println("inside A.last");
}
}
class Deadlock implements Runnable
{
A a=new A();
B b=new B();
Deadlock()
{
Thread.currentThread().setName("MainThread");
new Thread(this).start();
a.foo(b); //get lock on a in this thread.
System.out.println("back in main thread");
}
public void run()
{
Thread.currentThread().setName("RacingThread");
b.bar(a); //get lock on a in other thread.
System.out.println("back in other thread");
}
public static void main(String[] args)
{
new Deadlock();
}
}
《Java就业培训教程》P204的Java原代码
程序清单:ThreadCommunation.java
class Producer implements Runnable
{
Q q=null;
public Producer(Q q)
{
this.q=q;
}
public void run()
{
int i=0;
while(true)
{
if(i==0)
{
q.name="张孝祥";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -