首页 > 代码库 > 类继承,定义了一个点类point,然后线条类line继承了point类,正方形类square继承line类
类继承,定义了一个点类point,然后线条类line继承了point类,正方形类square继承line类
类继承,定义了一个点类point,然后线条类line继承了point类,正方形类Suare继承line类
正方形四个角坐标关系如图
1 /** 2 * 3 java继承实例。 4 5 6 定义了一个点类point,然后线条类line继承了point类,正方形类square继承line类。 7 8 */ 9 10 //点类 11 class PointDemo 12 { 13 private int x; 14 private int y; 15 private static int pCount=0; 16 17 //默认初始化坐标点(0,0),坐标点数计数+1 18 PointDemo(){ 19 this.x=0; 20 this.y=0; 21 pCount++; 22 } 23 24 //带有x,y坐标参数构造函数,坐标点数计数+1 25 PointDemo(int x, int y){ 26 this.x=x; 27 this.y=y; 28 pCount++; 29 } 30 31 //设置坐标x,y值 32 public void setPoint(int x,int y){ 33 this.x=x; 34 this.y=y; 35 } 36 37 //返回获取X,Y坐标值 38 public int getX(){return x;} 39 public int getY(){return y;} 40 41 //返回程序累积坐标计数 42 public static int getPCount(){return pCount;} 43 44 } 45 46 //行类继承点类 47 class LineDemo extends PointDemo 48 { 49 private PointDemo po1; 50 private PointDemo po2; 51 private float range; 52 private static int lCount=0; 53 54 //返回程序累积线数计数 55 public static int getLCount(){return lCount;} 56 57 //默认空参构造函数,默认坐标(0,0),(4,3),初始化两坐标之间的长度,线数计数+1 58 LineDemo(){ 59 po1=new PointDemo(); 60 po2=new PointDemo(4,3); 61 setRange(); 62 lCount++; 63 } 64 65 //两坐标参数构造函数,初始化两坐标和坐标之间的长度,线数计数+1 66 LineDemo(PointDemo po1,PointDemo po2){ 67 this.po1=po1; 68 this.po2=po2; 69 setRange(); 70 lCount++; 71 } 72 73 //返回获取两点坐标值 74 public PointDemo getPo1(){return po1;} 75 public PointDemo getPo2(){return po2;} 76 77 //设置两点之间的长度 78 public void setRange(){ 79 range=(float)Math.sqrt(Math.pow(Math.abs(po2.getX()-po1.getX()),2)+Math.pow(Math.abs(po2.getY()-po1.getY()),2));; 80 } 81 public float getRange(){return range;} 82 83 //输出两点坐标及两点之间的距离 84 public void out(){ 85 System.out.println("___________________________"); 86 System.out.println("("+po1.getX()+","+po1.getY()+")"); 87 System.out.println("("+po2.getX()+","+po2.getY()+")"); 88 System.out.println("range="+range); 89 System.out.println("==========================="); 90 } 91 } 92 93 //正方型类 94 /* 95 (X1,Y1),(X2,Y2),(X3,Y3),(X4,Y4) 96 A = X2-X1, B = Y2-Y1; 97 X3 = X1+(A-B) = X1+(X2-X1-Y2+Y1) = X2-Y2+Y1; 98 Y3 = Y1+A+B = Y1+X2-X1+Y2-Y1 = X2-X1+Y2; 99 X4 = -(X2-(A-B)) = -(X2+Y2-Y1-X2+X1) = Y1-X1-Y2; 100 Y4 = Y2+(A-B) = Y2+X2-X1-Y2+Y1 = X2-X1+Y1; 101 102 */ 103 class Square extends LineDemo 104 { 105 private LineDemo[] line = new LineDemo[6]; 106 private PointDemo[] point = new PointDemo[4]; 107 private static int sCount=0; 108 109 //返回累积正方形个数计数 110 public static int getSCount(){return sCount;} 111 112 //空参构造函数,默认初始化正方形四个角坐标及边长和周长的长度,正方形个数计数+1 113 Square(){ 114 super(); 115 setSquarePoint(); 116 setSquareLine(); 117 sCount++; 118 } 119 120 //两坐标点参数构造函数,初始化正方形四个角坐标及边长和周长的长度,正方形个数计数+1 121 Square(PointDemo po1,PointDemo po2){ 122 super(po1,po2); 123 point[0]=po1; 124 point[1]=po2; 125 setSquarePoint(); 126 setSquareLine(); 127 sCount++; 128 } 129 130 //一条边长做参数的构造函数,初始化正方形四个角坐标及边长和周长的长度,正方形个数计数+1 131 Square(LineDemo line){ 132 super(line.getPo1(),line.getPo2()); 133 point[0]=line.getPo1(); 134 point[1]=line.getPo2(); 135 setSquarePoint(); 136 setSquareLine(); 137 sCount++; 138 } 139 140 //设置正方形四个角坐标 141 private void setSquarePoint(){ 142 int x1=point[0].getX(); 143 int y1=point[0].getY(); 144 int x2=point[1].getX(); 145 int y2=point[1].getY(); 146 point[2] = new PointDemo(x2-y2+y1,x2-x1+y2); 147 point[3] = new PointDemo(y1-x1-y2,x2-x1+y1); 148 } 149 150 //设置正方形所有边长和对角线坐标及长度 151 private void setSquareLine(){ 152 for (int i=0,k=0;i<point.length ;i++ ) 153 { 154 for (int j=i+1;j<point.length ;j++,k++ ) 155 { 156 line[k] =new LineDemo(point[i],point[j]); 157 } 158 } 159 160 } 161 162 //输出所有边长和对角线坐标及长度 163 public void outAllLine(){ 164 for (LineDemo li : line) 165 { 166 li.out(); 167 } 168 } 169 170 //输出正方形四个角坐标 171 public void outPoint(){ 172 for (int i=0;i<point.length ;i++ ) 173 { 174 System.out.println("point["+i+"] = ("+point[i].getX()+","+point[i].getY()+")"); 175 } 176 } 177 } 178 179 class ExtendsDemo 180 { 181 public static void main(String[] args) 182 { 183 //测试点 184 PointDemo[] pArr = new PointDemo[10]; 185 for (int i=0,j=10;i<pArr.length ;i++,j-- ) 186 { 187 pArr[i]=new PointDemo(i,j); 188 } 189 for (PointDemo po: pArr ) 190 { 191 System.out.println("("+po.getX()+","+po.getY()+")"); 192 } 193 System.out.println("pointCount="+PointDemo.getPCount()+"\n"); 194 195 //测试线 196 LineDemo line = new LineDemo(); 197 line.out(); 198 System.out.println("lineCount="+LineDemo.getLCount()+"\n"); 199 LineDemo line1 = new LineDemo(pArr[0],pArr[9]); 200 line1.out(); 201 System.out.println("lineCount="+LineDemo.getLCount()+"\n"); 202 System.out.println("pointCount="+LineDemo.getPCount()+"\n"); 203 204 //测试正方形 205 Square su = new Square(line1); 206 System.out.println("显示所有坐标"); 207 su.outPoint(); //显示所有坐标 208 System.out.println("显示所有边坐标及长度"); 209 su.outAllLine(); //显示所有边坐标及长度 210 System.out.println("SquareCount="+Square.getSCount()+"\n"); 211 } 212 }
类继承,定义了一个点类point,然后线条类line继承了point类,正方形类square继承line类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。