第一章:java语言基础;
//第一个程序HelloWorld.java
package foxe;
public class HelloWorld {
/**
* @param args
*/
public static void main(String[] args)
{
System.out.println("Hello,World!");
// TODO Auto-generated method
stub
}
}
//第二个程序HelloWorld_Applet .java
package foxe;
import java.awt.Graphics;
import
javax.swing.JApplet;
public class HelloWorld_Applet extends JApplet{
public void paint(Graphics g) {
g.drawString("Hello World!", 25,
25);
}
}
//for ,while,do-while sum.java
package foxe;
public class Sum {
/**
* @param args
*/
public static void main(String[] args)
{
int i=1,n=10,sum1=0,sum2=0,sum3=0;
//for
circulation
for(i=1;i<=n;i++){
sum1=sum1+i;
}
System.out.println("for
sum is
:"+sum1);
i=1;
while(i<=n){
sum2=sum2+i;
i++;
}
System.out.println("while
sum is
:"+sum2);
i=1;
do{
sum3=sum3+i;
i++;
}while(i<=n);
System.out.println("do-while
sum is :"+sum3);
}
}
第二章:类和对象
//参数传递ParameterTest .java
package foxe;
class Point{
int x=70;
int y=80;
}
class Circle{
int
x=5,y=6;
Point point ;
void setXY(int x1,int
y1){
x1=x;
y1=y;
}
void setPoint (Point
ref){
ref.x=x;
ref.y=y;
}
}
public class ParameterTest
{
//pass parameter test
/**
* @param args
*/
public static void main(String[] args) {
Circle p=new
Circle();
int xValue=-1,yValue=-1;
System.out.println("value
pass:");
p.setXY(xValue,
yValue);
System.out.println("xValue="+xValue+",yValue="+yValue);
Point
point=new Point();
p.setPoint(point);
System.out.println("address
pass:");
System.out.print("point.x="+point.x+",point.y="+point.y);
}
}
//类变量和类方法与实例变量和实例方法;
package foxe;
class Member{
static int classVar;//类变量
int
instanceVar;//实例变量
static void setClassVar(int i){
classVar=i;
//instanseVar=i;类方法不能访问实例变量
}
static int
getClassVar(){//类方法
return classVar;
}
void setInstanceVar(int
i){//实例方法
classVar=i;//实例方法可以访问类变量
instanceVar=i;
}
int
getInstanceVar()
{
return instanceVar;
}
}
public class MemberTest {
public static void main(String[]
args) {
Member m1=new Member();
Member m2=new Member();
m1.setClassVar(1);
m2.setClassVar(2);
System.out.println("m1.classVar="+m1.getClassVar()+",m2.classVar="+m2.getClassVar());
m1.setInstanceVar(11);
m2.setInstanceVar(22);
System.out.println("m1.InstanceVar="+m1.getInstanceVar()+",m2.InstanceVar="+m2.getInstanceVar());
Member.classVar=3;
System.out.println("m1.ClassVar="+m1.getClassVar()+",m2.ClassVar="+m2.getClassVar());
System.out.println("ClassVar="+Member.getClassVar());
}
}