1 public class Text1{ 2 public static void main(String[] args){ 3 Outter o=new Outter(); 4 //第一题-----上面一句代码是外部类的对象实例化 5 System.out.println("第一题:"); 6 Outter.B b=o.new B(); 7 b.showA(); 8 b.showB(); 9 //第二题 10 System.out.println("第二题:"); 11 Outter.Staff s=o.new Staff(); 12 s.name="李四"; 13 System.out.println(s.name); 14 //第三题-----多态---1 方法重载 2 方法重写 3 对象多态,这里是对象多态 15 System.out.println("第三题:"); 16 Outter.Car car=o.new Aodi(); 17 car.changeSpeed(); 18 car=o.new Benchi(); 19 car.changeSpeed(); 20 //第四题-----多态 21 System.out.println("第四题:"); 22 Outter.Rect r=o.new PlaintRect(20,10,10,10); 23 System.out.println("周长为:"+r.perimeter()); 24 System.out.println("周长为:"+r.area()); 25 System.out.println("坐标"+(r.isInside(10,20)==true?"有效":"无效")); 26 } 27 } 28 //外部类 29 class Outter{ 30 //第一题 31 //父类A 32 class A{ 33 public void showA(){ 34 char a[]=new char[26]; 35 for(int i=0,b=‘a‘;i<a.length;i++,b++){ 36 a[i]=(char)b; 37 System.out.print(a[i]+" "); 38 } 39 System.out.println(); 40 } 41 } 42 //子类B继承父类 43 class B extends A{ 44 public void showB(){ 45 char a[]=new char[26]; 46 for(int i=‘A‘;i<a.length+‘A‘;i++){ 47 a[i-‘A‘]=(char)i; 48 System.out.print(a[i-‘A‘]+" "); 49 } 50 System.out.println(); 51 } 52 } 53 //第二题 54 //父类Penson 55 class Penson{ 56 protected String name; 57 protected String address; 58 protected String telphone; 59 protected String mail; 60 } 61 //Penson的子类Employee 62 class Employee extends Penson{ 63 protected String office; 64 protected double wage; 65 protected String hiredate; 66 } 67 //Employee的子类Faculty 68 class Faculty extends Employee{ 69 protected String degree; 70 protected String level; 71 } 72 //Employee的子类Staff 73 class Staff extends Employee{ 74 protected String duty; 75 } 76 //第三题 77 //主类Car 78 class Car{ 79 protected String pinPai; 80 protected void changeSpeed(){ 81 System.out.println("呜呜呜......"); 82 } 83 } 84 //子类Aodi继承Car 85 class Aodi extends Car{ 86 private double price; 87 private String xingHao; 88 public double getPrice(){ 89 return price; 90 } 91 public String getXingHao(){ 92 return xingHao; 93 } 94 public void changeSpeed(){ 95 System.out.println("变速卡卡卡......"); 96 } 97 } 98 //子类Benchi继承Car 99 class Benchi extends Car{ 100 private double price; 101 private String xingHao; 102 public double getPrice(){ 103 return price; 104 } 105 public String getXingHao(){ 106 return xingHao; 107 } 108 public void changeSpeed(){ 109 System.out.println("变速嘟嘟嘟......"); 110 } 111 } 112 //第四题 113 //矩形类Rect 114 abstract class Rect{ 115 protected int width; 116 protected int height; 117 Rect(){ 118 width=0; 119 height=0; 120 } 121 Rect(int width,int height){ 122 this.width=width; 123 this.height=height; 124 } 125 protected int area(){ 126 return width*height; 127 } 128 protected int perimeter(){ 129 return 2*(width+height); 130 } 131 public abstract boolean isInside(int x,int y); 132 } 133 //带坐标的PlaintRect类继承自Rect 134 class PlaintRect extends Rect{ 135 private int startX; 136 private int startY; 137 PlaintRect(){ 138 super(); 139 startX=0; 140 startY=0; 141 } 142 PlaintRect(int width,int height,int startX,int startY){ 143 super(width,height); 144 this.startX=startX; 145 this.startY=startY; 146 } 147 public void setStartX(int startX){ 148 this.startX=startX; 149 } 150 public void setStartY(int startY){ 151 this.startY=startY; 152 } 153 public int getStartX(){ 154 return startX; 155 } 156 public int getStartY(){ 157 return startY; 158 } 159 public boolean isInside(int x,int y){ 160 if(x>=startX&&x<=(startX+width)&&y<startY&&y>=(startY-height)) 161 return true; 162 return false; 163 } 164 } 165 }
时间: 2024-11-02 14:56:31