第一范文网 - 专业文章范例文档资料分享平台

Java面向对象经典案例10个

来源:用户分享 时间:2025/8/26 10:22:11 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

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个 的文档
Java面向对象经典案例10个.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c9tpc822xbb6h1tx45fgo_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top