Example4_1
class XiyoujiRenwu{ float height,weight; String head,ear; void speak(String s){ System.out.println(s); } } public class Example4_1 { public static void main (String args[]){ XiyoujiRenwu zhubajie,sunwukong; zhubajie = new XiyoujiRenwu(); sunwukong = new XiyoujiRenwu(); } }
Example4_2
public class Examole4_2 { public static void main(String args[]){ XiyoujiRenwu zhubajie = null,sunwukong = null; zhubajie=new XiyoujiRenwu(); sunwukong=new XiyoujiRenwu(); zhubajie.name=PersonName.八戒; zhubajie.height=1.83f; zhubajie.weight=86f; zhubajie.head="猪头"; sunwukong.nasme=PersonName.悟空; sunwukong.height=1.66f; sunwukong.weight=1000f; sunwukong.head="猴头"; System.out.println(zhubajie.name+"的身高:"+zhubajie.height); System.out.println(zhubajie.name+"的体重:"+zhubajie.weight); System.out.println(zhubajie.name+"的头:"+zhubajie.head); System.out.println(sunwukong.name+"的身高:"+sunwukong.height); System.out.println(sunwukong.name+"的体重:"+sunwukong.weight); System.out.println(sunwukong.name+"的头:"+sunwukong.head); zhubajie.speak(zhubajie.name+"我想娶媳妇"); System.out.println(zhubajie.name+"现在的头:"+zhubajie.head); sunwukong.speak(sunwukong.name+"我重"+ sunwukong.weight+"公斤,想骗八戒背我"); System.out.println(sunwukong.name+"现在的头:"+zhubajie.head); } }
Example4_3
class Point{ int x,y; void setXY(int m,int n){ x = m; y = n; } } public class Example4_3 { public static void main(String args[]){ Point p1,p2; p1 = new Point(); p2 = new Point(); System.out.println("p1的引用:"+p1); System.out.println("p2的引用:"+p2); p1.setXY(1111,2222); p2.setXY(-100,-200); System.out.println("p1的x,y坐标:"+p1.x+","+p1.y); System.out.println("p2的x,y坐标:"+p2.x+","+p2.y); p1 = p2; System.out.println("将p2的引用赋给p1后:"); System.out.println("p1的引用:"+p1); System.out.println("p2的引用:"+p2); System.out.println("p1的x,y坐标:"+p1.x+","+p1.y); System.out.println("p2的x,y坐标:"+p2.x+","+p2.y); } }
Example4_4
public class Example4_4 { public static void main(String args[]){ Rect rect=new Rect(); double w=12.76,h=25.28; rect.setWidth(w); rect.setHeight(h); System.out.println("矩形对象的宽:"+rect.getWidth()+"高:"+rect,getHeight()); System.out.println("矩形的面积:"+rect.getArea()); System.out.println("更改向对象的方法参数传递值的w、h变量的值为100和256"); w=100; h=256; System.out.println("矩形对象的宽:"+rect.getWidth()+"高:"+rect.getHeight()); } }
Example4_6
public class Example4_6 { public static void main(String args[]){ Computer computer=new Computer(); double result=computer.getResult(1.0/3,10,20,30); System.out.println("10,20,30的平均数:"+result); result=computer.getResult(1.0/6,66,12,5,89,2,51); System.out.println("66,12,5,89,2,51的平均数:"+result); } }
Example4_8
public class Example4_8 { public static void main(String args[]){ Ladder.下底=100; Ladder ladderOne = new Ladder(); Ladder ladderTwo = new Ladder(); ladderOne.设置上底(28); ladderTwo.设置上底(66); System.out.println("ladderOne的上底:"+ladderOne.获取上底()); System.out.println("ladderOne的下底:"+ladderOne.获取下底()); System.out.println("ladderTwo的上底:"+ladderTwo.获取上底()); System.out.println("ladderTwo的下底:"+ladderTwo.获取下底()); } }
Example4_9
class A{ int x,y,z; static int getContinueSum(int start,int end){ int sum=0; for (int i=start;i<=end;i++){ sum=sum+i; } return sum; } } public class Example4_9 { public static void main(String args[]){ int result=A.getContinueSum(0,100); System.out.println(result); } }
Example4_10
package tom.jiafei; public class Example4_10 { public static void main(String args[]){ Student stu=new Student(10201); stu.speak(); System.out.println("主类的包名也是tom.jiafei"); } }
Example4_16
lass AAA{ private int money; private int getMoney(){ return money; } } class E{ public static void main(String args[]){ AAA exa=new AAA(); exa.money=3000; int m=exa.getMoney(); System.out.println("money="+m); } }
Example4_17
public class Example4_17 { public static void main(String args[]){ Student zhang=new Student(); Student geng=new Student(); zhang.setAge(23); System.out.println("zhang的年龄:"+zhang.getAge()); geng.setAge(25); //"zhang.age=23;或"geng.age=25;"都是非法的,因为zhang和geng已经不在Student类中 System.out.println("geng的年龄:"+geng.getAge()); } }
习题5_1
class AAA{ static int m ; static { m = 888; } } public class E{ public static void main(String args[]){ AAA a= null; System.out.println("%d:%d",AAA.m,a.m); } }
习题5_2
class CPU{ int speed; int getSpeed(){ return speed; } public void setSpeed(int speed){ this.speed=speed; } } class HardDisk{ int amount; public int getAmount() { return amount; } public void setAmount(int amount){ this.amount=amount; } } class PC{ CPU cpu; HardDisk HD; void setCpu(CPU cpu){ this.cpu=cpu; } void setHardDisk(HardDisk HD){ this.HD=HD; } void show(){ System.out.println("CPU速度:"+cpu.getSpeed()); System.out.println("硬盘容量:"+HD.getAmount()); } } class Test{ public static void main(String args[]){ CPU cpu=new CPU(); HardDisk HD=new HardDisk(); cpu.getSpeed(2200); HD.setAmount(200); PC pc=new PC(); pc.setCpu(cpu); pc.setHardDisk(HD); pc.show(); } }