按书的页码排列 如果代码有误,欢迎评论区指正!
- p14
public class J_HelloJava
{
public static void main(String args[ ])
{
System.out.println("Java语言,您好!");
System.out.println("我将成为优秀的Java程序员!");
}
}
- p18
import java.awt.Graphics;
import javax.swing.JApplet;
public class J_HelloApplet extends JApplet
{
public void paint(Graphics g)
{
g.clearRect(0, 0, getWidth( ), getHeight( ));
g.drawString("小应用程序,您好!", 10, 20);
}
}
- p25
public class J_Identifier
{
public static void main(String args[ ])
{
char c = '猫';
if (Character.isJavaIdentifierStart(c))
System.out.println("字符'"+c+"'可以做标识符的首字符");
else
System.out.println("字符'"+c+"'不可以做标识符的首字符");
if (Character.isJavaIdentifierPart(c))
System.out.println("字符'"+c
+"'可以做标识符除首字符外的组成字符");
else
System.out.println("字符'"+c
+"'不可以做标识符除首字符外的组成字符");
}
}
- p33
public class J_CastExample
{
public static void main(String args[ ])
{
short a= 100;
long b= a;
System.out.println("类型转换: 短整数" + a + "变成长整数" + b);
b= 123456789L;
a= (short)b;
System.out.println("类型转换: 长整数" + b + "变成短整数" + a);
}
}
- p37
public class J_Boolean
{
public static void main(String args[ ])
{
int month=8;
int day=1;
if ((month==8) || (++day<15))
System.out.println("Month=" + month + ", Day=" + day);
if ((month==8) | (++day<15))
System.out.println("Month=" + month + ", Day=" + day);
}
}
- p41
public class J_Swap
{
public static void main(String args[ ])
{
int a = 123;
int b = 321;
System.out.println("a=" + a + ", b=" + b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println("a=" + a + ", b=" + b);
}
}
- p52
public class J_Break
{
public static void main(String args[ ])
{
int i = 0;
aBreakBlock:
{
System.out.println("在break语句之前");
if (i<=0)
break aBreakBlock;
System.out.println("在if和break语句之后");
}
System.out.println("在aBreakBlock语句块之后");
}
}
- p54
public class J_ContinueLoopSingle
{
public static void main(String args[ ])
{
for (int i=0; i< 10; i++)
{
if (1<i && i<8)
continue;
System.out.println("i=" + i);
}
}
}
- p56
public class J_ContinueLoopNested
{
public static void main(String args[ ])
{
aContinueBlock:
for (int i=0; i< 4; i++)
{
for (int j=0; j< 2; j++)
{
if (0<i && i<3)
continue aContinueBlock;
System.out.println("i=" + i + ", j=" +j);
}
}
}
}
- p57
public class J_Factorial
{
public static void main(String args[ ])
{
int i;
int result;
result= 1;
for (i=1; i<= 10; i++)
result*=i;
System.out.println("10!=" + result);
}
}
- p68
class J_Book
{
public int m_id;
public J_Book( int i )
{
m_id = i;
}
protected void finalize( )
{
switch (m_id)
{
case 1:
System.out.print( "《飘》" );
break;
case 2:
System.out.print( "《Java程序设计教程》" );
break;
case 3:
System.out.print( "《罗马假日》" );
break;
default:
System.out.print( "未知书籍" );
break;
}
System.out.println( "所对应的实例对象存储单元被回收" );
}
}
public class J_Finalize
{
public static void main(String args[ ])
{
J_Book book1= new J_Book( 1 );
new J_Book( 2 );
new J_Book( 3 );
System.gc( );
}
}
- p71
class J_Employee
{
public int m_workYear;
public J_Employee( )
{
m_workYear = 1;
}
}
public class J_Teacher extends J_Employee
{
public int m_classHour;
public J_Teacher( )
{
m_classHour = 96;
}
public void mb_printInfo( )
{
System.out.println("该教师的工作年限为" + m_workYear);
System.out.println("该教师授课的课时为" + m_classHour);
}
public static void main(String args[ ])
{
J_Teacher tom = new J_Teacher( );
tom.mb_printInfo( );
}
}
- p75
public class J_Student
{
public int m_id;
public int m_age;
public J_Student( )
{
mb_setData(2008010400, 19);
}
public J_Student( int id, int age )
{
mb_setData(id, age);
}
public void mb_setData( int id, int age )
{
m_id = id;
m_age = age;
}
public void mb_setData( int id )
{
m_id = id;
}
public static void main(String args[ ])
{
J_Student jack = new J_Student( );
jack.mb_setData(2008010408);
J_Student lisa = new J_Student( );
lisa.mb_setData(2008010428, 18);
System.out.print("Jack的学号是" + jack.m_id);
System.out.println(",年龄是" + jack.m_age);
System.out.print("Lisa的学号是" + lisa.m_id);
System.out.println(",年龄是" + lisa.m_age);
}
}
- p77
class J_Employee
{
public int m_workYear;
public J_Employee( )
{
m_workYear = 1;
}
public void mb_printInfo( )
{
System.out.println("该职工的工作年限为" + m_workYear);
}
}
public class J_Teacher extends J_Employee
{
public int m_classHour;
public J_Teacher( )
{
m_classHour = 96;
}
public void mb_printInfo( )
{
System.out.println("该教师的工作年限为" + m_workYear);
System.out.println("该教师授课的课时为" + m_classHour);
}
public static void main(String args[ ])
{
J_Employee a = new J_Employee( );
a.mb_printInfo( );
a = new J_Teacher( );
a.mb_printInfo( );
}
}
- p82
package cn.edu.tsinghua.universityOrganization;
public class J_Employee
{
public int m_workYear;
public J_Employee( )
{
m_workYear = 1;
}
public void mb_printInfo( )
{
System.out.println("该职工的工作年限为" + m_workYear);
}
}
- p83
package cn.edu.tsinghua.universityOrganization;
import cn.edu.tsinghua.universityOrganization.J_Employee;
public class J_Teacher extends J_Employee
{
public int m_classHour;
public J_Teacher( )
{
m_classHour = 96;
}
public void mb_printInfo( )
{
System.out.println("该教师的工作年限为" + m_workYear);
System.out.println("该教师授课的课时为" + m_classHour);
}
}
- p83
import cn.edu.tsinghua.universityOrganization.J_Employee;
import cn.edu.tsinghua.universityOrganization.J_Teacher;
public class J_University
{
public static void main(String args[ ])
{
J_Employee a = new J_Employee( );
a.mb_printInfo( );
a = new J_Teacher( );
a.mb_printInfo( );
}
}
- p90
public class J_Book
{
public int m_id;
public static int m_bookNumber = 0;
public J_Book( )
{
m_bookNumber ++;
}
public void mb_info( )
{
System.out.println( "当前书的编号是:" + m_id);
}
public static void mb_infoStatic( )
{
System.out.println( "书的总数是:" + m_bookNumber);
}
public static void main(String args[ ])
{
J_Book a = new J_Book( );
J_Book b = new J_Book( );
a.m_id = 1101;
b.m_id = 1234;
System.out.print( "变量a对应的");
a.mb_info( );
System.out.print( "变量b对应的");
b.mb_info( );
J_Book.mb_infoStatic( );
System.out.println( "比较(a.m_bookNumber==J_Book.m_bookNumber)"
+ "的结果是:" + (a.m_bookNumber==J_Book.m_bookNumber));
System.out.println( "比较(b.m_bookNumber==J_Book.m_bookNumber)"
+ "的结果是:" + (b.m_bookNumber==J_Book.m_bookNumber));
}
}
- p98
class J_Test
{
int m_dataOuter = 1;
static int m_dataOuterStatic = 2;
class J_Inner
{
int m_data;
static final int m_dataStatic = 4;
public J_Inner( )
{
m_data = 3;
}
public void mb_method( )
{
System.out.println( "m_dataOuter=" + m_dataOuter );
System.out.println( "m_dataOuterStatic="
+ m_dataOuterStatic );
System.out.println( "m_data=" + m_data );
System.out.println( "m_dataStatic=" + m_dataStatic );
mb_methodOuter( );
}
}
public void mb_methodOuter( )
{
System.out.println( "mb_methodOuter" );
}
}
public class J_InnerTest
{
public static void main(String args[ ])
{
J_Test a = new J_Test( );
J_Test.J_Inner b = a.new J_Inner( );
b.mb_method( );
}
}
持续更新中。。。
|