Java面向对象经典案例10个

2026/1/16 5:46:31

1

class Anthropoid //类人猿 { }

private int n=100; void crySpeak(String s) { }

System.out.println(s);

class People extends Anthropoid {

void computer(int a,int b) { } {

int c=a*b;

System.out.println(c);

void crySpeak(String s)

System.out.println(\ } }

public class Monkey { }

public static void main(String args[]) { }

Anthropoid monkey=new People(); //monkey是People对象的上转型对象 //monkey.computer(10,10); //非法 monkey.crySpeak(\我喜欢这个运动\People people=(People)monkey; //把上转型对象强制转化为子类的对象 people.computer(10,10);

2

class ManyArea{

public double area(double radius){ return Math.PI*radius*radius; }

public double area(double len,double width){ return len*width; }

public 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的圆的面积:\ System.out.println(\长2.0、宽3.0的面积:\ System.out.println(\长2、宽3的面积:\ System.out.println(\立方体的面积:\ }

}

3

class Animal { } {

public void shout() { }

class Dog extends Animal

public void newDog() {

System.out.println(\的新特性\

}

public void shout() {

System.out.println(\汪\ } }

class Cat extends Animal {

public void shout()

{

System.out.println(\喵\ } } class Test {

public void animalshout(Animal a) {

a.shout();

}

}

public class PolyEx { }

public 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);

4

class ArrayEx{

public int[] subarray(int a[],int start,int end){

int subarr[] = new int[end-start];

for(int i=0,j=start;j

}

return subarr; } }

public class Test{

public static void main(String args[]){

int sub[] = arrex.subarray(arr,2,6); for(int temp:sub){ System.out.println(temp); } }

}

ArrayEx arrex = new ArrayEx(); int arr[] = new int[10];

for(int i = 0;i

arr[i] = i+10; }

5

class Box{ int length; int width; int height;

void set(int len,int wid,int hei){ length = len; width = wid; height = hei; }


Java面向对象经典案例10个.doc 将本文的Word文档下载到电脑
搜索更多关于: Java面向对象经典案例10个 的文档
相关推荐
相关阅读
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 10

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xuecool-com QQ:370150219