Java程序设基础教程-杨佩理(源程序PDF课件).docVIP

  • 9
  • 0
  • 约2.13万字
  • 约 22页
  • 2017-03-09 发布于贵州
  • 举报

Java程序设基础教程-杨佩理(源程序PDF课件).doc

Java程序设基础教程-杨佩理(源程序

Java程序设计基础教程-杨佩理(源程序PDF课件) 例4-1 class Abc static int num 0; void count num ++; System.out.println This is object + num ; public static void main String[] args Abc a new Abc ; Abc b new Abc ; Abc c new Abc ; a.count ; b.count ; c.count ; System.out.println Abc.num ; 例4-2 class Circle double r; double getL return 2 * Math.PI * r; //Math.PI为圆周率 double getS return Math.PI * r * r; 例4-3 class MyCal static int getSum int n int sum 0; for int i 1;i n;i++ sum + i; return sum; static long getFac int n long fac 1; for int i 1;i n; i++ fac * i; return fac; class Test public static void main String[] aa System.out.println MyCal.getSum 100 +\t+MyCal.getFac 10 ; 例4-4 class Person int sex; int salary; void employed System.out.print Work state: ; if salary 0 System.out.println no job ; else System.out.println job ; class Test public static void main String[] aa Person ps new Person ; ps.salary 300; ps.employed ; ps.salary 0; ps.employed ; 例4-5 class ReferenceTest public static void main String[] args Person p1 new Person ; //设对象为A,p1指向A Person p2 new Person ; //设对象为B,p2指向B p1.salary 1; p2.salary 2; System.out.println p1.salary+\t+p2.salary ; p1 p2; // p1重定向,也指向了p2指向的B System.out.println p1.salary + \t + p2.salary ; p1.salary 8; System.out.println p1.salary + \t + p2.salary ; 例4-6 class Star static void print char c,int n for int i 0; i n; i++ System.out.print c ; public static void main String[] args for int i 0;i 5;i++ print , 10-i ; //输出10-i个空格 print *,2*i + 1 ; //输出2i+1个星号 System.out.println ; 例4-7 class TestCircle public static void main String[] args Circle cc new Circle ; cc.r 3.5; System.out.println cc.getL + \t\t + cc.getS ; 例4-8 class TestOL int x; void func System.out.println no arg ; String func int i return arg is + i; void func String s System.out.println s ; public static void main String[] a TestOL c new TestOL ; c.func ; c.fun

文档评论(0)

1亿VIP精品文档

相关文档