《Java面向对象经典案例10个(6页).doc》由会员分享,可在线阅读,更多相关《Java面向对象经典案例10个(6页).doc(6页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、-Java面向对象经典案例10个-第 6 页1class Anthropoid /类人猿private int n=100;void crySpeak(String s)System.out.println(s);class People extends Anthropoidvoid computer(int a,int b)int c=a*b;System.out.println(c);void crySpeak(String s)System.out.println(*+s+*);public class Monkeypublic static void main(String args)
2、Anthropoid monkey=new People(); /monkey是People对象的上转型对象/puter(10,10); /非法monkey.crySpeak(我喜欢这个运动);People people=(People)monkey; /把上转型对象强制转化为子类的对象puter(10,10);2class ManyArea public double area(double radius) return Math.PI*radius*radius; public double area(double len,double width) return len*width; p
3、ublic double area(int len,int width) return len*width; public double area(double len,double width,double height) return len*width*height; public class OverLoad public static void main(String args) ManyArea ob=new ManyArea(); System.out.println(半径为3.0的圆的面积:+ob.area(3.0); System.out.println(长2.0、宽3.0的
4、面积:+ob.area(2.0,3.0); System.out.println(长2、宽3的面积:+ob.area(2,3); System.out.println(立方体的面积:+ob.area(2.0,3.0,4.0);3class Animal public void shout() class Dog extends Animal public void newDog()System.out.println(Dog的新特性);public void shout() System.out.println(汪);class Cat extends Animal public void s
5、hout() System.out.println(喵);class Testpublic void animalshout(Animal a) a.shout();public class PolyExpublic static void main(String args)Animal d=new Dog();/(1)Dog d1= (Dog)d;/(3)父类对象强制转换成子类对象d1.newDog();/d.newDog(); d.shout();Test t=new Test();t.animalshout(d);/(2)t.animalshout(d1);4class ArrayEx
6、public int subarray(int a,int start,int end) int subarr = new intend-start; for(int i=0,j=start;jend;i+,j+) subarri = aj; return subarr;public class Test public static void main(String args) ArrayEx arrex = new ArrayEx(); int arr = new int10; for(int i = 0;iarr.length;i+) arri = i+10; int sub = arre
7、x.subarray(arr,2,6); for(int temp:sub) System.out.println(temp);5class Box int length; int width; int height; void set(int len,int wid,int hei) length = len; width = wid; height = hei;class ShowBox void show(Box b) System.out.println(b.length+ +b.width+ +b.height);class TestTwo public static void ma
8、in(String args) Box a = new Box(); a.set(3,4,5); ShowBox sbox = new ShowBox(); sbox.show(a);6.class One int a = 5; void showB() int a = 3; int b = this.a; System.out.println(b = +b);public class ThisOne public static void main(String args) One test = new One(); test.showB();7.class Mystatic private
9、int x=3; public static void showx() System.out.println(x=+x); public static int add(int m) return m+x; class UseMystatic public static void main(String args) Mystatic.showx(); System.out.println(add=+Mystatic.add(2);8.class Point int x; int y; Point() x=0; y=0; /this(1,1); Point(int a,int b) x=a; y=
10、b; void show() System.out.println(x=+x+ y=+y);public class UsePoint public static void main(String args) Point p = new Point(); p.show();9. class Point private int x,y;Point() x=1; y=3; void showPoint(Point t) System.out.println(x=+t.x+ y=+t.y);void seeit() showPoint(this);public class UsePointThisp
11、ublic static void main(String args)Point p=new Point();p.seeit();10class Point static int x=2; int y=0; public class UseStatic public static void main(String args) System.out.println(利用类调用静态变量); System.out.println(x=+Point.x); /System.out.println(y=+Point.y); Point p1=new Point(); System.out.println(利用对象调用); System.out.println(x=+p1.x); System.out.println(y=+p1.y); Point p2=new Point(); p2.y=3; System.out.println(对象p1中y的值+y=+p1.y); System.out.println(对象p2中y的值+y=+p2.y); p1.x=6; System.out.println(对象p1中x的值+x=+p1.x); System.out.println(对象p2中x的值+x=+p2.x);
限制150内