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; }
相关推荐: